Created
July 21, 2020 00:09
-
-
Save codemonkey76/b8a2fc2e436131896576b8fab7db8772 to your computer and use it in GitHub Desktop.
Fusion API Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use App\Cdr; | |
use Carbon\Carbon; | |
use Illuminate\Contracts\Config\Repository; | |
use Illuminate\Database\QueryException; | |
use Illuminate\Support\Facades\Http; | |
class FusionApiService extends \Illuminate\Support\Facades\Facade | |
{ | |
private $base_url; | |
private $user_name; | |
private $password; | |
private $token; | |
public function __construct(Repository $config) | |
{ | |
$this->base_url = $config->get('fusionapi.api_base_url'); | |
$this->user_name = $config->get('fusionapi.api_user'); | |
$this->password = $config->get('fusionapi.api_password'); | |
} | |
private function login() | |
{ | |
$response = Http::post($this->base_url . '/login', [ | |
'email' => $this->user_name, | |
'password' => $this->password | |
]); | |
$this->token = $response->header('token'); | |
} | |
public function getCdrs($date = null, $hours = 2) | |
{ | |
if (!$date) { | |
$date = now(); | |
} else { | |
$date = Carbon::parse($date); | |
} | |
$this->login(); | |
$response = Http::get($this->base_url . '/cdrs', [ | |
'date' => $date, | |
'hours' => $hours, | |
'token' => $this->token | |
]); | |
$models = collect(); | |
collect($response->json())->each(function ($model) use ($models){ | |
try { | |
$model = Cdr::create($model); | |
$models->push($model); | |
} catch (QueryException $e) { } | |
}); | |
return $models; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment