o = $o; $this->_columns = collect(); } /** * Connect and pull down traffic data * * @return Collection */ public function fetch(): Collection { if ($x=$this->mustPause()) { Log::notice(sprintf('%s:API Throttle, waiting [%s]...',self::LOGKEY,$x),['m'=>__METHOD__]); sleep($x); } Log::debug(sprintf('%s:Supplier [%d], fetch data for [%s]...',self::LOGKEY,$this->o->id,$this->o->stats_lastupdate),['m'=>__METHOD__]); $result = Cache::remember('Supplier:'.$this->o->id.$this->o->stats_lastupdate,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->format('Y-m-d'), ], /* '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 AND $api_reset) { Log::notice(sprintf('%s:API Throttle [%d].',self::LOGKEY,$api_reset),['m'=>__METHOD__]); Cache::put('api_throttle',$api_reset,now()->addSeconds($api_reset)); } // Assume the supplier provides an ASCII output for text/html if (preg_match('#^text/html;#',$x=Arr::get($result->getHeader('Content-Type'),'0'))) { return collect(explode("\n",$result->getBody()->getContents()))->filter(); } else { Log::error(sprintf('%s:Havent handled header type [%s]',self::LOGKEY,$x),['m'=>__METHOD__]); throw new \Exception('Unhandled Content Type'); } }); Log::debug(sprintf('%s:Supplier [%d], records returned [%d]...',self::LOGKEY,$this->o->id,$result->count()),['m'=>__METHOD__]); 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 */ public function getColumns(string $line,Collection $expect): Collection { $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()).' got: '.join('|',$fields)); } return $fields->intersect($this->_columns); } /** * Return the key ID for a column * * @param string $key * @return mixed */ public function getColumnKey(string $key) { return $this->_columns->search($key); } public function header(): array { return static::$header; } /** * If the supplier has API throttling... * * @return mixed */ protected function mustPause() { return Cache::get('api_throttle'); } }