Created
November 15, 2018 01:32
-
-
Save filljoyner/17f6bebf30a7db96613d57d2e2c55fd5 to your computer and use it in GitHub Desktop.
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\Abilities\Capsules; | |
use GuzzleHttp\Client; | |
class DatasetApi | |
{ | |
protected $key; | |
protected $conn; | |
protected $api_token; | |
public function __construct($key, $conn) | |
{ | |
$this->key = $key . '_api_token'; | |
$this->conn = $conn; | |
} | |
public function boot() | |
{ | |
if($this->shouldRefreshToken()) { | |
$this->refreshToken(); | |
} | |
} | |
public function get($endpoint = '', $args = []) | |
{ | |
return $this->client()->get($this->clientUrl() . $endpoint, $this->patchHeaders($args)); | |
} | |
public function post($endpoint, $args = []) | |
{ | |
return $this->client()->post($this->clientUrl() . $endpoint, $this->patchHeaders($args)); | |
} | |
protected function client() | |
{ | |
return new Client(); | |
} | |
protected function clientUrl() | |
{ | |
return $this->conn['url']; | |
} | |
protected function clientId() | |
{ | |
return $this->conn['client_id']; | |
} | |
protected function clientSecret() | |
{ | |
return $this->conn['client_secret']; | |
} | |
protected function token() | |
{ | |
if(cache()->has($this->key)) { | |
return cache($this->key); | |
} | |
return null; | |
} | |
protected function setToken($token) | |
{ | |
cache([$this->key => $token], now()->addMonths(6)); | |
} | |
protected function shouldRefreshToken() | |
{ | |
// if token not cached, cache it | |
if(!$this->token()) { | |
return true; | |
} else { | |
return $this->touchCheck(); | |
} | |
} | |
protected function touchCheck() | |
{ | |
$response = $this->get('/api/touch'); | |
$result = json_decode((string) $response->getBody(), true); | |
if(!empty($result['status'])) { | |
if($result['status'] == 'success') { | |
return true; | |
} | |
} | |
return false; | |
} | |
protected function refreshToken() | |
{ | |
$this->setToken($this->getToken()); | |
} | |
protected function getToken() | |
{ | |
$response = $this->post('/oauth/token', [ | |
'form_params' => [ | |
'grant_type' => 'client_credentials', | |
'client_id' => $this->clientId(), | |
'client_secret' => $this->clientSecret() | |
] | |
]); | |
$result = json_decode((string) $response->getBody(), true); | |
if(!empty($result['access_token'])) { | |
return $result['access_token']; | |
} | |
throw new \Exception('Failed to receive access token.'); | |
} | |
protected function patchHeaders($args) | |
{ | |
$headers = [ | |
'Accept' => 'application/json', | |
'Authorization' => 'Bearer ' . $this->token() | |
]; | |
if(!empty($args['headers'])) { | |
$args['headers'] = array_merge($headers, $args['headers']); | |
} else { | |
$args['headers'] = $headers; | |
} | |
return $args; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment