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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php": "^8.3",
"illuminate/console": "^12.0|^13.0",
"illuminate/support": "^12.0|^13.0",
"php-skir/runtime": "^0.1@dev|^0.2@dev",
"php-skir/runtime": "^0.2@dev",
"saloonphp/saloon": "^4.0",
"symfony/process": "^6.4|^7.0|^8.0"
},
Expand Down
1 change: 1 addition & 0 deletions config/skir-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
return [
'base_url' => env('SKIR_CLIENT_BASE_URL'),
'endpoint' => env('SKIR_CLIENT_ENDPOINT', '/'),
'format' => env('SKIR_CLIENT_FORMAT', 'dense'),

'generator' => [
'node' => env('SKIR_CLIENT_NODE', 'node'),
Expand Down
17 changes: 16 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ $client = new SkirClient('https://api.example.test', '/api/skir');

## Wire format

The client speaks the standard SkirRPC protocol, the same one upstream Skir clients use. Each call is a `POST` whose body is `MethodName:methodNumber::denseJsonRequest`, and the server answers with the response in dense JSON. Any SkirRPC server works, whether it is `php-skir/server` or a service written in another language.
The client speaks the standard SkirRPC protocol, the same one upstream Skir clients use, so any SkirRPC server works, whether it is `php-skir/server` or a service written in another language. Each call is a `POST` whose body is `MethodName:methodNumber:format:request`.

By default requests and responses use dense JSON, which the Skir docs recommend for APIs. Set `SKIR_CLIENT_FORMAT=readable` to exchange readable JSON instead, for example while debugging:

```dotenv
SKIR_CLIENT_FORMAT=readable
```

```php
use Skir\Client\SkirClient;
use Skir\Client\WireFormat;

$client = new SkirClient('https://api.example.test', '/api/skir', WireFormat::Readable);
```

Readable JSON is meant for humans and is not safe for persistence. Binary is not part of the SkirRPC wire protocol; generated classes offer `toBytes()` and `fromBytes()` when you need it outside RPC.

See [Generating clients](generating-clients.md) for the typed generation workflow and [Error handling and testing](error-handling-and-testing.md) for transport failure handling and mocks.
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ SKIR_CLIENT_ENDPOINT=/api/skir

`SKIR_CLIENT_BASE_URL` is required. `SKIR_CLIENT_ENDPOINT` defaults to `/`; do not duplicate the
endpoint path in the base URL.

`SKIR_CLIENT_FORMAT` is `dense` (default) or `readable`; readable JSON is for debugging only.
5 changes: 5 additions & 0 deletions src/Exceptions/SkirClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static function missingBaseUrl(): self
{
return new self('Set [skir-client.base_url] (SKIR_CLIENT_BASE_URL) to the SkirRPC server URL.');
}

public static function unsupportedFormat(string $format): self
{
return new self("Skir client format [{$format}] is not supported. Use [dense] or [readable].");
}
}
13 changes: 12 additions & 1 deletion src/Http/SkirRpcRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasStringBody;
use Skir\Client\WireFormat;
use Skir\Runtime\DenseJson;
use Skir\Runtime\MethodDescriptor;
use Skir\Runtime\ReadableJson;

final class SkirRpcRequest extends Request implements HasBody
{
Expand All @@ -21,6 +23,7 @@ public function __construct(
public readonly MethodDescriptor $descriptor,
public readonly mixed $request,
private readonly string $endpoint = '/',
public readonly WireFormat $format = WireFormat::Dense,
) {}

public function resolveEndpoint(): string
Expand All @@ -42,7 +45,15 @@ protected function defaultHeaders(): array

protected function defaultBody(): string
{
$requestJson = DenseJson::toJson($this->descriptor->requestType, $this->request);
$requestType = $this->descriptor->requestType;

if ($this->format === WireFormat::Readable) {
$requestJson = ReadableJson::toJson($requestType, $this->request);

return "{$this->descriptor->name}:{$this->descriptor->number}:readable:{$requestJson}";
}

$requestJson = DenseJson::toJson($requestType, $this->request);

return "{$this->descriptor->name}:{$this->descriptor->number}::{$requestJson}";
}
Expand Down
3 changes: 2 additions & 1 deletion src/SkirClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class SkirClient
public function __construct(
string $baseUrl,
private readonly string $endpoint = '/',
private readonly WireFormat $format = WireFormat::Dense,
) {
$this->connector = new SkirConnector($baseUrl);
}
Expand All @@ -33,7 +34,7 @@ public function withMockClient(MockClient $mockClient): self
public function invoke(MethodDescriptor $descriptor, mixed $request): mixed
{
$response = $this->connector->send(
new SkirRpcRequest($descriptor, $request, $this->endpoint),
new SkirRpcRequest($descriptor, $request, $this->endpoint, $this->format),
);

if ($response->failed()) {
Expand Down
8 changes: 7 additions & 1 deletion src/SkirClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public function register(): void
throw SkirClientException::missingBaseUrl();
}

return new SkirClient($baseUrl, (string) config('skir-client.endpoint'));
$format = (string) config('skir-client.format');

return new SkirClient(
$baseUrl,
(string) config('skir-client.endpoint'),
WireFormat::tryFrom($format) ?? throw SkirClientException::unsupportedFormat($format),
);
});
}

Expand Down
11 changes: 11 additions & 0 deletions src/WireFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Skir\Client;

enum WireFormat: string
{
case Dense = 'dense';
case Readable = 'readable';
}
1 change: 1 addition & 0 deletions tests/Feature/BoostAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function it_keeps_client_guidance_anchored_to_the_released_package(): voi
[
'SKIR_CLIENT_BASE_URL',
'SKIR_CLIENT_ENDPOINT',
'SKIR_CLIENT_FORMAT',
'SKIR_CLIENT_ROOT',
'SKIR_CLIENT_SKIR_BIN',
'SKIR_CLIENT_NODE',
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/SkirClientServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Skir\Client\Http\SkirRpcRequest;
use Skir\Client\SkirClient;
use Skir\Client\Tests\TestCase;
use Skir\Client\WireFormat;
use Skir\Runtime\MethodDescriptor;
use Skir\Runtime\Type;

Expand Down Expand Up @@ -51,4 +52,36 @@ public function it_requires_a_base_url_when_resolving_the_client(): void

app(SkirClient::class);
}

#[Test]
public function it_resolves_the_configured_wire_format(): void
{
config()->set('skir-client.base_url', 'https://example.com/api');
config()->set('skir-client.format', 'readable');

$mockClient = new MockClient([
SkirRpcRequest::class => MockResponse::make('25', 200),
]);

$client = app(SkirClient::class);
$client->withMockClient($mockClient);
$client->invoke(new MethodDescriptor('Square', 1001, Type::float32(), Type::float32()), 5.0);

$mockClient->assertSent(function (SkirRpcRequest $request): bool {
return $request->format === WireFormat::Readable
&& $request->body()->all() === 'Square:1001:readable:5';
});
}

#[Test]
public function it_rejects_an_unsupported_wire_format(): void
{
config()->set('skir-client.base_url', 'https://example.com/api');
config()->set('skir-client.format', 'binary');

$this->expectException(SkirClientException::class);
$this->expectExceptionMessage('Skir client format [binary] is not supported. Use [dense] or [readable].');

app(SkirClient::class);
}
}
37 changes: 37 additions & 0 deletions tests/Feature/SkirClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Skir\Client\Http\SkirRpcRequest;
use Skir\Client\SkirClient;
use Skir\Client\Tests\TestCase;
use Skir\Client\WireFormat;
use Skir\Runtime\Field;
use Skir\Runtime\MethodDescriptor;
use Skir\Runtime\Type;
Expand Down Expand Up @@ -124,4 +125,40 @@ public function it_localises_invalid_dense_json_responses(): void
5.0,
);
}

#[Test]
public function it_can_exchange_readable_json(): void
{
$mockClient = new MockClient([
SkirRpcRequest::class => MockResponse::make("{\n \"id\": 42,\n \"name\": \"Ruben\"\n}", 200, [
'Content-Type' => 'application/json',
]),
]);

$client = new SkirClient('https://example.com/api', format: WireFormat::Readable);
$client->withMockClient($mockClient);

$userType = Type::struct([
Field::value('id', 0, Type::int32()),
Field::value('name', 1, Type::string()),
]);

$result = $client->invoke(
new MethodDescriptor('RenameUser', 1002, $userType, $userType),
[
'id' => 42,
'name' => 'Maxim',
],
);

$this->assertSame([
'id' => 42,
'name' => 'Ruben',
], $result);

$mockClient->assertSent(function (Request $request): bool {
return $request instanceof SkirRpcRequest
&& $request->body()->all() === "RenameUser:1002:readable:{\n \"id\": 42,\n \"name\": \"Maxim\"\n}";
});
}
}
Loading