Skip to content

Commit

Permalink
Merge bearer token into request options if it's set
Browse files Browse the repository at this point in the history
  • Loading branch information
nosnickid committed Oct 9, 2023
1 parent 9b2e30a commit 91c402c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Http/RequestHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@ trait RequestHandlerTrait

protected ?string $accessToken = null;

private function mergeBearerToken(array $options): array {
if ($this->accessToken !== null) {
return array_merge(
$options,
[
'headers' => array_merge(
["Authorization" => "Bearer {$this->accessToken}"],
$options['headers'] ?? [],
),
],
);
}
return $options;
}

public function get(string $uri, array $options = []): ResponseInterface
{
try {
$options = $this->mergeBearerToken($options);
$response = $this->httpClient->get($uri, $options);
return $response;
} catch (BadResponseException $exc) {
Expand All @@ -27,6 +43,7 @@ public function get(string $uri, array $options = []): ResponseInterface
public function post(string $uri, array $options = []): ResponseInterface
{
try {
$options = $this->mergeBearerToken($options);
$response = $this->httpClient->post($uri, $options);
return $response;
} catch (BadResponseException $exc) {
Expand All @@ -37,6 +54,7 @@ public function post(string $uri, array $options = []): ResponseInterface
public function put(string $uri, array $options = []): ResponseInterface
{
try {
$options = $this->mergeBearerToken($options);
$response = $this->httpClient->put($uri, $options);
return $response;
} catch (BadResponseException $exc) {
Expand All @@ -47,6 +65,7 @@ public function put(string $uri, array $options = []): ResponseInterface
public function delete(string $uri, array $options = []): ResponseInterface
{
try {
$options = $this->mergeBearerToken($options);
$response = $this->httpClient->delete($uri, $options);
return $response;
} catch (BadResponseException $exc) {
Expand Down

0 comments on commit 91c402c

Please sign in to comment.