From 547f89a9e8ee0396c87953c40e8a13ce20922421 Mon Sep 17 00:00:00 2001 From: Nikolay Zhidenko Date: Thu, 19 Nov 2026 17:22:19 +0100 Subject: [PATCH] fix: base_url defaults to the API and ignores a trailing /v1 The API reference prints https://api.proofage.net/v1, and an integration that passed it as base_url called /v1/v1/... and got 404 on every request: the client appends the version itself. Production showed coding agents giving up on the SDK at that point and writing their own Guzzle client. A trailing /{version} is now dropped (a proxy prefix before it is kept), and base_url defaults to https://api.proofage.net, as in the Node.js and Python SDKs. An explicitly empty base_url is still an error. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 ++-- CHANGELOG.md | 10 ++++++++++ README.md | 2 +- src/Client.php | 27 ++++++++++++++++++++++++++- tests/ClientTest.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0b61e50..4270c0b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -15,8 +15,8 @@ All requests send `X-API-Key` and `X-HMAC-Signature` to `{base_url}/{version}/{p `X-ProofAge-Sdk: php/{Client::VERSION}` (a wrapper's `sdk_tokens` go first, e.g. `laravel/0.9.0 php/0.3.0`) and, unless one is set, `User-Agent: ProofAge-PHP/{version} (PHP {PHP_VERSION})`; neither is signed. The -`base_url` config key is required and has no default — use `https://api.proofage.net`, with no -path; `version` defaults to `v1`. +`base_url` config key defaults to `https://api.proofage.net`; a trailing `/{version}` on it is +ignored, because the client appends the version itself; `version` defaults to `v1`. ## Auth / HMAC diff --git a/CHANGELOG.md b/CHANGELOG.md index 72f5ef2..d51907f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +### Changed + +- `base_url` is optional and defaults to `https://api.proofage.net`. Passing `null` also means the + default; an explicit empty string is still a configuration error. +- A trailing `/{version}` on `base_url` is ignored. `https://api.proofage.net/v1`, the address the API + reference prints, used to make every call go to `/v1/v1/...` and fail with 404. A path prefix in + front of the version (a proxy) is kept. + ## 0.13.0 - 2026-11-18 ### Added diff --git a/README.md b/README.md index 8aec3c8..a4e77fc 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ says which SDK sent it: `X-ProofAge-Sdk: php/0.3.0` and `User-Agent: ProofAge-PH |---|---|---| | `api_key` | required | Workspace API key | | `secret_key` | required | Workspace secret key, used only to sign | -| `base_url` | required | `https://api.proofage.net`; must have no path component | +| `base_url` | `https://api.proofage.net` | The API origin. A trailing `/v1` (the address the API reference prints) is ignored, since the client adds the version | | `version` | `v1` | API version segment | | `timeout` | `30` | Seconds per attempt; a positive integer (sub-second timeouts are not supported) | | `retry_attempts` | `3` | Attempts for interactive requests; see [Retries](#retries) | diff --git a/src/Client.php b/src/Client.php index bcfbf7e..43e8322 100644 --- a/src/Client.php +++ b/src/Client.php @@ -40,6 +40,9 @@ class Client */ public const VERSION = '0.13.0'; + /** The API origin used when the config has no `base_url`. */ + public const DEFAULT_BASE_URL = 'https://api.proofage.net'; + /** * Sent on every request: `name/version` tokens separated by single spaces, outermost * wrapper first and this SDK's `php/{VERSION}` last. Not part of the HMAC signature. @@ -90,6 +93,7 @@ class Client public function __construct(#[\SensitiveParameter] array $config, ?HttpClient $transport = null, ?ExceptionFactory $exceptions = null, ?callable $sleep = null) { $this->config = array_merge([ + 'base_url' => self::DEFAULT_BASE_URL, 'version' => 'v1', 'timeout' => 30, 'retry_attempts' => 3, @@ -345,6 +349,24 @@ public function __debugInfo(): array ]; } + /** + * The base URL without trailing slashes or a trailing `/{version}`. The client appends the + * version itself, so `https://api.proofage.net/v1`, the address the API reference and the + * OpenAPI `servers` entry print, would otherwise call `/v1/v1/...` and get a 404. A path + * prefix in front of the version (a proxy) is kept. + */ + private static function withoutVersionSuffix(string $baseUrl, string $version): string + { + $baseUrl = rtrim($baseUrl, '/'); + $suffix = '/'.trim($version, '/'); + + if ($suffix !== '/' && str_ends_with($baseUrl, $suffix)) { + $baseUrl = rtrim(substr($baseUrl, 0, -strlen($suffix)), '/'); + } + + return $baseUrl; + } + protected function validateConfig(): void { if (empty($this->config['api_key'])) { @@ -355,6 +377,9 @@ protected function validateConfig(): void throw $this->exceptions->configuration('Secret key is required'); } + // Absent or null means the default; an explicit empty string is a mistake worth reporting. + $this->config['base_url'] ??= self::DEFAULT_BASE_URL; + if (empty($this->config['base_url'])) { throw $this->exceptions->configuration('Base URL is required'); } @@ -494,8 +519,8 @@ static function (string $segment) use ($endpoint): string { explode('/', $endpoint), )); - $baseUrl = rtrim((string) $this->config['base_url'], '/'); $version = (string) $this->config['version']; + $baseUrl = self::withoutVersionSuffix((string) $this->config['base_url'], $version); $path = '/'.$version.'/'.$endpoint; $url = "{$baseUrl}/{$version}/{$endpoint}"; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 65cac0b..035e902 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -65,12 +65,50 @@ public function test_it_throws_exception_when_secret_key_is_missing(): void new Client(['api_key' => 'test-api-key', 'base_url' => 'https://api.test.com']); } - public function test_it_throws_exception_when_base_url_is_missing(): void + public function test_base_url_defaults_to_the_production_api(): void + { + $fake = new FakeHttpClient(['*' => FakeHttpClient::json([])]); + + (new Client(['api_key' => 'test-api-key', 'secret_key' => 'test-secret-key'], $fake))->makeRequest('GET', 'workspace'); + (new Client(['api_key' => 'test-api-key', 'secret_key' => 'test-secret-key', 'base_url' => null], $fake))->makeRequest('GET', 'workspace'); + + $this->assertSame('https://api.proofage.net/v1/workspace', $fake->sent()[0]->url); + $this->assertSame('https://api.proofage.net/v1/workspace', $fake->sent()[1]->url); + } + + public function test_an_explicitly_empty_base_url_is_still_an_error(): void { $this->expectException(ProofAgeException::class); $this->expectExceptionMessage('Base URL is required'); - new Client(['api_key' => 'test-api-key', 'secret_key' => 'test-secret-key']); + new Client(['api_key' => 'test-api-key', 'secret_key' => 'test-secret-key', 'base_url' => '']); + } + + /** + * The API reference prints `https://api.proofage.net/v1`; passing it must not call `/v1/v1`. + * + * @return iterable + */ + public static function baseUrlsWithAVersion(): iterable + { + yield 'trailing /v1' => ['https://api.test.com/v1', 'v1', 'https://api.test.com/v1/workspace']; + yield 'trailing /v1/' => ['https://api.test.com/v1/', 'v1', 'https://api.test.com/v1/workspace']; + yield 'another configured version' => ['https://api.test.com/v2', 'v2', 'https://api.test.com/v2/workspace']; + yield 'a proxy prefix is kept' => ['https://proxy.test.com/proofage/v1', 'v1', 'https://proxy.test.com/proofage/v1/workspace']; + yield 'a different version is not touched' => ['https://api.test.com/v1', 'v2', 'https://api.test.com/v1/v2/workspace']; + yield 'only a whole segment counts' => ['https://api.test.com/apiv1', 'v1', 'https://api.test.com/apiv1/v1/workspace']; + } + + #[DataProvider('baseUrlsWithAVersion')] + public function test_a_trailing_version_on_the_base_url_is_not_doubled(string $baseUrl, string $version, string $expectedUrl): void + { + $client = $this->client(['*' => FakeHttpClient::json([])], ['base_url' => $baseUrl, 'version' => $version], $fake); + + $client->makeRequest('GET', 'workspace'); + + $sent = $fake->sent()[0]; + $this->assertSame($expectedUrl, $sent->url); + $this->assertSame('/'.$version.'/workspace', $sent->path); } public function test_an_empty_string_counts_as_missing(): void