From 8b304eab5aa923dea97db02b4602f1135cdba0ef Mon Sep 17 00:00:00 2001 From: Sebastian Brown Date: Fri, 17 Oct 2025 15:18:31 -0500 Subject: [PATCH 1/2] Versioning broke the user-agent header. Getting token in constructor. --- .gitignore | 1 + src/Api/Client.php | 57 +++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 8c109a9..cf092c6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock phpunit.xml tasks.md +/.idea diff --git a/src/Api/Client.php b/src/Api/Client.php index 2a04f7e..e6456e2 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -23,11 +23,21 @@ 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, + protected string $useragent = "", ) { $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(); + } + $this->useragent = 'SPINEN/' . trim((string) $this->getVersion()); } /** @@ -139,24 +149,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' => $this->useragent, ], - 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); // } @@ -185,7 +195,7 @@ public function refreshToken(): Token 'Accept' => '*/*', 'Authorization' => 'Bearer'.' '.$this->configs['jwt'], 'Content-Type' => 'text/plain', - 'User-Agent' => 'SPINEN/'.$this->getVersion(), + 'User-Agent' => $this->useragent, 'X-ACCESS-EXPIRY-OVERRIDE' => $this->configs['override']['access'].'s', 'X-REFRESH-EXPIRY-OVERRIDE' => $this->configs['override']['refresh'].'s', ], @@ -221,12 +231,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' => 'Bearer'.' '.$this->configs['jwt'], + 'User-Agent' => $this->useragent, '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'), From e7e96767e13f20d4339f325efe3eb31c677d7343 Mon Sep 17 00:00:00 2001 From: Sebastian Brown Date: Fri, 17 Oct 2025 15:21:25 -0500 Subject: [PATCH 2/2] Removed useragent from constructor as this was only for development testing. Fixed Content-Type header on requestToken. --- src/Api/Client.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Api/Client.php b/src/Api/Client.php index e6456e2..3c1ff39 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -25,7 +25,6 @@ public function __construct( protected Guzzle $guzzle = new Guzzle, protected Token|string|null $token = null, protected bool $debug = false, - protected string $useragent = "", ) { $this->setConfigs($configs); @@ -37,7 +36,6 @@ public function __construct( elseif (!empty($configs['jwt'])) { $this->requestToken(); } - $this->useragent = 'SPINEN/' . trim((string) $this->getVersion()); } /** @@ -154,13 +152,13 @@ public function request(?string $path, ?array $data = [], ?string $method = 'GET json: $this->guzzle->request( method: $method, options: [ - 'debug' => $this->debug, + 'debug' => $this->debug, 'headers' => [ 'Authorization' => (string) $this->getToken(), - 'Content-Type' => 'application/json', - 'User-Agent' => $this->useragent, + 'Content-Type' => 'application/json', + 'User-Agent' => 'SPINEN/' . trim((string) $this->getVersion()), ], - 'body' => empty($data) ? null : json_encode($data), + 'body' => empty($data) ? null : json_encode($data), ], uri: $this->uri($path), ) @@ -195,7 +193,7 @@ public function refreshToken(): Token 'Accept' => '*/*', 'Authorization' => 'Bearer'.' '.$this->configs['jwt'], 'Content-Type' => 'text/plain', - 'User-Agent' => $this->useragent, + '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', ], @@ -232,8 +230,8 @@ public function requestToken(): Token 'debug' => $this->debug, 'headers' => [ 'Authorization' => 'Bearer'.' '.$this->configs['jwt'], - 'Content-Type' => 'Bearer'.' '.$this->configs['jwt'], - 'User-Agent' => $this->useragent, + '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' ],