diff --git a/composer.json b/composer.json index d4b9992..b9ee607 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/config/skir-client.php b/config/skir-client.php index f0c3fdd..0e85592 100644 --- a/config/skir-client.php +++ b/config/skir-client.php @@ -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'), diff --git a/docs/configuration.md b/docs/configuration.md index a25336e..6695dab 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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. diff --git a/resources/boost/skills/skir-client-development/references/generation-and-configuration.md b/resources/boost/skills/skir-client-development/references/generation-and-configuration.md index 488c602..0eb3660 100644 --- a/resources/boost/skills/skir-client-development/references/generation-and-configuration.md +++ b/resources/boost/skills/skir-client-development/references/generation-and-configuration.md @@ -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. diff --git a/src/Exceptions/SkirClientException.php b/src/Exceptions/SkirClientException.php index 8ce3717..0584293 100644 --- a/src/Exceptions/SkirClientException.php +++ b/src/Exceptions/SkirClientException.php @@ -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]."); + } } diff --git a/src/Http/SkirRpcRequest.php b/src/Http/SkirRpcRequest.php index 3f2406b..006e650 100644 --- a/src/Http/SkirRpcRequest.php +++ b/src/Http/SkirRpcRequest.php @@ -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 { @@ -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 @@ -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}"; } diff --git a/src/SkirClient.php b/src/SkirClient.php index f235501..535943b 100644 --- a/src/SkirClient.php +++ b/src/SkirClient.php @@ -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); } @@ -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()) { diff --git a/src/SkirClientServiceProvider.php b/src/SkirClientServiceProvider.php index b81d5c6..b4e53d1 100644 --- a/src/SkirClientServiceProvider.php +++ b/src/SkirClientServiceProvider.php @@ -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), + ); }); } diff --git a/src/WireFormat.php b/src/WireFormat.php new file mode 100644 index 0000000..7b8eb09 --- /dev/null +++ b/src/WireFormat.php @@ -0,0 +1,11 @@ +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); + } } diff --git a/tests/Feature/SkirClientTest.php b/tests/Feature/SkirClientTest.php index 3dc1a51..e69209f 100644 --- a/tests/Feature/SkirClientTest.php +++ b/tests/Feature/SkirClientTest.php @@ -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; @@ -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}"; + }); + } }