Skip to content
Merged
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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
27 changes: 26 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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');
}
Expand Down Expand Up @@ -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}";
Expand Down
42 changes: 40 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{string, string, string}>
*/
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
Expand Down
Loading