2020-05-27 06:09:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\External;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
abstract class Supplier
|
|
|
|
{
|
|
|
|
protected $o = NULL;
|
|
|
|
protected $_columns = [];
|
|
|
|
|
|
|
|
public function __construct(Model $o)
|
|
|
|
{
|
|
|
|
$this->o = $o;
|
|
|
|
$this->_columns = collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect and pull down traffic data
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-05-28 05:08:13 +00:00
|
|
|
public function connect(array $args=[])
|
2020-05-27 06:09:03 +00:00
|
|
|
{
|
|
|
|
if ($x=$this->mustPause()) {
|
|
|
|
Log::error('API Throttle, waiting .',['m'=>__METHOD__]);
|
|
|
|
sleep($x);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = Cache::remember('Supplier:'.$this->o->id,86400,function() {
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
$result = $client->request('POST',null,[
|
|
|
|
'query'=>[
|
|
|
|
$this->login_user_field=>$this->o->stats_username,
|
|
|
|
$this->login_pass_field=>$this->o->stats_password,
|
|
|
|
$this->date_field=>$this->o->stats_lastupdate,
|
|
|
|
],
|
|
|
|
/*
|
|
|
|
'headers' => [
|
|
|
|
'Accept'=>'application/json',
|
|
|
|
],
|
|
|
|
*/
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api_remain = Arr::get($result->getHeader('X-RateLimit-Remaining'),0);
|
|
|
|
$api_reset = Arr::get($result->getHeader('X-RateLimit-Reset'),0);
|
|
|
|
|
|
|
|
if ($api_remain == 0) {
|
|
|
|
Log::error('API Throttle.',['m'=>__METHOD__]);
|
|
|
|
Cache::put('api_throttle',$api_reset,now()->addSeconds($api_reset));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result->getBody()->getContents();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the API HTTP client
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
protected function getClient(): Client
|
|
|
|
{
|
|
|
|
return new Client(['base_uri'=>$this->o->stats_url]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the expected columns from a supplier traffic import
|
|
|
|
*
|
|
|
|
* @param string $line
|
|
|
|
* @param Collection $expect
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2020-05-28 05:08:13 +00:00
|
|
|
public function getColumns(string $line,Collection $expect): Collection
|
2020-05-27 06:09:03 +00:00
|
|
|
{
|
|
|
|
$fields = collect(explode(',',$line))->filter();
|
|
|
|
$this->_columns = $expect;
|
|
|
|
|
|
|
|
if ($this->_columns->diff($fields)->count()) {
|
|
|
|
abort('500','Missing columns in data: '.join('|',$this->_columns->diff($fields)->toArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields->intersect($this->_columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the key ID for a column
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-05-28 05:08:13 +00:00
|
|
|
public function getColumnKey(string $key)
|
2020-05-27 06:09:03 +00:00
|
|
|
{
|
|
|
|
return $this->_columns->search($key);
|
|
|
|
}
|
|
|
|
|
2020-05-28 05:08:13 +00:00
|
|
|
public function header(): array
|
|
|
|
{
|
|
|
|
return static::$header;
|
|
|
|
}
|
|
|
|
|
2020-05-27 06:09:03 +00:00
|
|
|
/**
|
|
|
|
* If the supplier has API throttling...
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function mustPause()
|
|
|
|
{
|
|
|
|
return Cache::get('api_throttle');
|
|
|
|
}
|
|
|
|
}
|