From f6751e7a63cdfa1492463ffbfee491155d187e72 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 2 Jul 2021 16:15:45 +1000 Subject: [PATCH] Fix Exetel Traffic import --- app/Classes/External/Supplier.php | 30 ++++++++----------- .../broadband_traffic_mismatch.blade.php | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/Classes/External/Supplier.php b/app/Classes/External/Supplier.php index 2049b2d..9a7d22d 100644 --- a/app/Classes/External/Supplier.php +++ b/app/Classes/External/Supplier.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; abstract class Supplier @@ -35,33 +36,29 @@ abstract class Supplier } 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() { + $key = 'Supplier:'.$this->o->id.$this->o->stats_lastupdate; + $result = Cache::remember($key,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', - ], - */ + $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'), ]); - $api_remain = Arr::get($result->getHeader('X-RateLimit-Remaining'),0); - $api_reset = Arr::get($result->getHeader('X-RateLimit-Reset'),0); + // @todo These API rate limiting is untested. + $api_remain = $response->header('X-RateLimit-Remaining'); + $api_reset = $response->header('X-RateLimit-Reset'); 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)); } + //dd($response->header('Content-Type'),$response->headers()); // 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(); + if (preg_match('#^text/html;#',$x=$response->header('Content-Type'))) { + return collect(explode("\n",$response->body()))->filter(); } else { Log::error(sprintf('%s:Havent handled header type [%s]',self::LOGKEY,$x),['m'=>__METHOD__]); @@ -94,7 +91,6 @@ abstract class Supplier { $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)); } diff --git a/resources/views/email/system/broadband_traffic_mismatch.blade.php b/resources/views/email/system/broadband_traffic_mismatch.blade.php index 198d6e5..4a141b0 100644 --- a/resources/views/email/system/broadband_traffic_mismatch.blade.php +++ b/resources/views/email/system/broadband_traffic_mismatch.blade.php @@ -6,7 +6,7 @@ The traffic import today, had mismatching records. A request to have those login @component('mail::table') | ID | | - | -@foreach ($aso->traffic_mismatch($date) as $o) +@foreach ($aso->trafficMismatch($date) as $o) | {{ $o->service }} @endforeach