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;
|
2021-07-02 06:15:45 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2020-05-27 06:09:03 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
abstract class Supplier
|
|
|
|
{
|
2021-02-17 13:22:50 +00:00
|
|
|
private const LOGKEY = 'AS-';
|
|
|
|
|
2020-05-27 06:09:03 +00:00
|
|
|
protected $o = NULL;
|
|
|
|
protected $_columns = [];
|
|
|
|
|
|
|
|
public function __construct(Model $o)
|
|
|
|
{
|
|
|
|
$this->o = $o;
|
|
|
|
$this->_columns = collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect and pull down traffic data
|
|
|
|
*
|
2021-02-17 13:22:50 +00:00
|
|
|
* @return Collection
|
2020-05-27 06:09:03 +00:00
|
|
|
*/
|
2021-02-17 13:22:50 +00:00
|
|
|
public function fetch(): Collection
|
2020-05-27 06:09:03 +00:00
|
|
|
{
|
|
|
|
if ($x=$this->mustPause()) {
|
2021-02-17 13:22:50 +00:00
|
|
|
Log::notice(sprintf('%s:API Throttle, waiting [%s]...',self::LOGKEY,$x),['m'=>__METHOD__]);
|
2020-05-27 06:09:03 +00:00
|
|
|
sleep($x);
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
Log::debug(sprintf('%s:Supplier [%d], fetch data for [%s]...',self::LOGKEY,$this->o->id,$this->o->stats_lastupdate),['m'=>__METHOD__]);
|
2021-07-02 06:15:45 +00:00
|
|
|
$key = 'Supplier:'.$this->o->id.$this->o->stats_lastupdate;
|
|
|
|
$result = Cache::remember($key,86400,function() {
|
2020-05-27 06:09:03 +00:00
|
|
|
$client = $this->getClient();
|
|
|
|
|
2021-07-02 06:15:45 +00:00
|
|
|
$response = Http::get($this->o->stats_url,[
|
|
|
|
$this->login_user_field => $this->o->stats_username,
|
|
|
|
$this->login_pass_field => $this->o->stats_password,
|
|
|
|
$this->date_field => $this->o->stats_lastupdate->format('Y-m-d'),
|
2020-05-27 06:09:03 +00:00
|
|
|
]);
|
|
|
|
|
2021-07-02 06:15:45 +00:00
|
|
|
// @todo These API rate limiting is untested.
|
|
|
|
$api_remain = $response->header('X-RateLimit-Remaining');
|
|
|
|
$api_reset = $response->header('X-RateLimit-Reset');
|
2020-05-27 06:09:03 +00:00
|
|
|
|
2020-05-29 04:10:10 +00:00
|
|
|
if ($api_remain === 0 AND $api_reset) {
|
2021-02-17 13:22:50 +00:00
|
|
|
Log::notice(sprintf('%s:API Throttle [%d].',self::LOGKEY,$api_reset),['m'=>__METHOD__]);
|
2020-05-27 06:09:03 +00:00
|
|
|
Cache::put('api_throttle',$api_reset,now()->addSeconds($api_reset));
|
|
|
|
}
|
|
|
|
|
2021-07-02 06:15:45 +00:00
|
|
|
//dd($response->header('Content-Type'),$response->headers());
|
2021-02-17 13:22:50 +00:00
|
|
|
// Assume the supplier provides an ASCII output for text/html
|
2021-07-02 06:15:45 +00:00
|
|
|
if (preg_match('#^text/html;#',$x=$response->header('Content-Type'))) {
|
|
|
|
return collect(explode("\n",$response->body()))->filter();
|
2020-05-27 06:09:03 +00:00
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
} else {
|
|
|
|
Log::error(sprintf('%s:Havent handled header type [%s]',self::LOGKEY,$x),['m'=>__METHOD__]);
|
|
|
|
throw new \Exception('Unhandled Content Type');
|
|
|
|
}
|
2020-05-27 06:09:03 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 13:22:50 +00:00
|
|
|
Log::debug(sprintf('%s:Supplier [%d], records returned [%d]...',self::LOGKEY,$this->o->id,$result->count()),['m'=>__METHOD__]);
|
|
|
|
|
2020-05-27 06:09:03 +00:00
|
|
|
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()) {
|
2020-05-29 04:10:10 +00:00
|
|
|
abort('500','Missing columns in data: '.join('|',$this->_columns->diff($fields)->toArray()).' got: '.join('|',$fields));
|
2020-05-27 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|