From 00ad4da34236c6877395ee26cd0d007cf35e44ef Mon Sep 17 00:00:00 2001 From: Lewis Larsen Date: Sat, 24 Aug 2024 15:30:13 +0100 Subject: [PATCH] adjusted how we handle custom urls --- src/MakesHttpRequests.php | 6 ++---- src/VanguardClient.php | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/MakesHttpRequests.php b/src/MakesHttpRequests.php index 39141dc..dee1435 100644 --- a/src/MakesHttpRequests.php +++ b/src/MakesHttpRequests.php @@ -49,11 +49,9 @@ public function put(string $uri, array $payload = []): mixed /** * Send a DELETE request to VanguardBackup API and return the response. * - * @param string $uri - * - * @throws Exception|GuzzleException + * @throws GuzzleException */ - public function delete($uri, array $payload = []): mixed + public function delete(string $uri, array $payload = []): mixed { return $this->request('DELETE', $uri, $payload); } diff --git a/src/VanguardClient.php b/src/VanguardClient.php index d839c11..f25eeb5 100644 --- a/src/VanguardClient.php +++ b/src/VanguardClient.php @@ -31,24 +31,22 @@ class VanguardClient /** * The base URL for the VanguardBackup API. */ - protected string $baseUrl = 'https://app.vanguardbackup.com/api/'; + protected string $baseUrl = 'https://app.vanguardbackup.com'; /** * Create a new VanguardClient instance. - * - * @return void */ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?HttpClient $httpClient = null) { - if (! is_null($baseUrl)) { + if ($baseUrl !== null) { $this->setBaseUrl($baseUrl); } - if (! is_null($apiKey)) { + if ($apiKey !== null) { $this->setApiKey($apiKey, $httpClient); } - if (! is_null($httpClient)) { + if ($httpClient !== null) { $this->httpClient = $httpClient; } } @@ -65,18 +63,16 @@ protected function transformCollection(array $collection, string $class, array $ /** * Set the API key and set up the HTTP client. - * - * @return $this */ public function setApiKey(string $apiKey, ?HttpClient $httpClient = null): static { $this->apiKey = $apiKey; - $this->httpClient = $httpClient ?: new HttpClient([ - 'base_uri' => $this->baseUrl, + $this->httpClient = $httpClient ?? new HttpClient([ + 'base_uri' => $this->getApiUrl(), 'http_errors' => false, 'headers' => [ - 'Authorization' => 'Bearer '.$this->apiKey, + 'Authorization' => "Bearer {$this->apiKey}", 'Accept' => 'application/json', 'Content-Type' => 'application/json', ], @@ -87,8 +83,6 @@ public function setApiKey(string $apiKey, ?HttpClient $httpClient = null): stati /** * Set the base URL for the VanguardBackup API. - * - * @return $this */ public function setBaseUrl(string $url): static { @@ -105,6 +99,14 @@ public function getBaseUrl(): string return $this->baseUrl; } + /** + * Get the full API URL, including the '/api/' path. + */ + public function getApiUrl(): string + { + return "{$this->baseUrl}/api"; + } + /** * Get an authenticated user instance. */