Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
composer.lock
phpunit.xml
tasks.md
/.idea
55 changes: 31 additions & 24 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ class Client
public function __construct(
protected array $configs,
protected Guzzle $guzzle = new Guzzle,
protected Token $token = new Token,
protected Token|string|null $token = null,
protected bool $debug = false,
) {
$this->setConfigs($configs);
$this->setToken($token);

// If a token was passed in explicitly, use that
if (!is_null($token)) {
$this->setToken($token);
}
// Otherwise, request token from jwt
elseif (!empty($configs['jwt'])) {
$this->requestToken();
}
}

/**
Expand Down Expand Up @@ -139,24 +147,24 @@ public function request(?string $path, ?array $data = [], ?string $method = 'GET
{
// TODO: Decide if going to do more than let the exception bubble up
// try {
return json_decode(
associative: true,
json: $this->guzzle->request(
method: $method,
options: [
'debug' => $this->debug,
'headers' => [
'Authorization' => (string) $this->getToken(),
'Content-Type' => 'application/json',
'User-Agent' => 'SPINEN/'.$this->getVersion(),
],
'body' => empty($data) ? null : json_encode($data),
return json_decode(
associative: true,
json: $this->guzzle->request(
method: $method,
options: [
'debug' => $this->debug,
'headers' => [
'Authorization' => (string) $this->getToken(),
'Content-Type' => 'application/json',
'User-Agent' => 'SPINEN/' . trim((string) $this->getVersion()),
],
uri: $this->uri($path),
)
->getBody()
->getContents(),
);
'body' => empty($data) ? null : json_encode($data),
],
uri: $this->uri($path),
)
->getBody()
->getContents(),
);
// } catch (GuzzleException $e) {
// $this->processException($e);
// }
Expand Down Expand Up @@ -185,7 +193,7 @@ public function refreshToken(): Token
'Accept' => '*/*',
'Authorization' => 'Bearer'.' '.$this->configs['jwt'],
'Content-Type' => 'text/plain',
'User-Agent' => 'SPINEN/'.$this->getVersion(),
'User-Agent' => 'SPINEN/' . trim((string) $this->getVersion()),
'X-ACCESS-EXPIRY-OVERRIDE' => $this->configs['override']['access'].'s',
'X-REFRESH-EXPIRY-OVERRIDE' => $this->configs['override']['refresh'].'s',
],
Expand Down Expand Up @@ -221,12 +229,11 @@ public function requestToken(): Token
options: [
'debug' => $this->debug,
'headers' => [
'Accept' => '*/*',
'Authorization' => 'Bearer'.' '.$this->configs['jwt'],
'Content-Type' => 'text/plain',
'User-Agent' => 'SPINEN/'.$this->getVersion(),
'Content-Type' => 'text/plain',
'User-Agent' => 'SPINEN/' . trim((string) $this->getVersion()),
'X-ACCESS-EXPIRY-OVERRIDE' => $this->configs['override']['access'].'s',
'X-REFRESH-EXPIRY-OVERRIDE' => $this->configs['override']['refresh'].'s',
'X-REFRESH-EXPIRY-OVERRIDE' => $this->configs['override']['refresh'].'s'
],
],
uri: $this->uri('auth/authenticate'),
Expand Down