diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 31f02d2..63834f5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -52,10 +52,21 @@ jobs: with: path: client + - name: Resolve runtime branch + id: runtime-ref + run: | + branch="${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" + if git ls-remote --exit-code --heads https://github.com/php-skir/runtime.git "$branch" > /dev/null; then + echo "ref=$branch" >> "$GITHUB_OUTPUT" + else + echo "ref=main" >> "$GITHUB_OUTPUT" + fi + - name: Checkout runtime uses: actions/checkout@v7 with: repository: php-skir/runtime + ref: ${{ steps.runtime-ref.outputs.ref }} path: runtime - name: Setup PHP @@ -100,10 +111,21 @@ jobs: with: path: client + - name: Resolve runtime branch + id: runtime-ref + run: | + branch="${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" + if git ls-remote --exit-code --heads https://github.com/php-skir/runtime.git "$branch" > /dev/null; then + echo "ref=$branch" >> "$GITHUB_OUTPUT" + else + echo "ref=main" >> "$GITHUB_OUTPUT" + fi + - name: Checkout runtime uses: actions/checkout@v7 with: repository: php-skir/runtime + ref: ${{ steps.runtime-ref.outputs.ref }} path: runtime - name: Setup PHP @@ -135,8 +157,8 @@ jobs: run: | composer config minimum-stability dev composer config prefer-stable true - composer config repositories.runtime '{"type":"path","url":"../runtime","options":{"symlink":false}}' --json - composer config repositories.client '{"type":"path","url":"../client","options":{"symlink":false}}' --json + composer config repositories.runtime '{"type":"path","url":"../runtime","options":{"symlink":false,"versions":{"php-skir/runtime":"0.2.x-dev"}}}' --json + composer config repositories.client '{"type":"path","url":"../client","options":{"symlink":false,"versions":{"php-skir/client":"0.2.x-dev"}}}' --json composer require "php-skir/client:*@dev" --no-interaction --no-progress composer require laravel/boost:^2.4 --dev --no-interaction --no-progress test ! -L vendor/php-skir/client @@ -169,10 +191,21 @@ jobs: with: path: client + - name: Resolve runtime branch + id: runtime-ref + run: | + branch="${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" + if git ls-remote --exit-code --heads https://github.com/php-skir/runtime.git "$branch" > /dev/null; then + echo "ref=$branch" >> "$GITHUB_OUTPUT" + else + echo "ref=main" >> "$GITHUB_OUTPUT" + fi + - name: Checkout runtime uses: actions/checkout@v7 with: repository: php-skir/runtime + ref: ${{ steps.runtime-ref.outputs.ref }} path: runtime - name: Setup PHP diff --git a/README.md b/README.md index c7b513f..8a4ee63 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Laravel package for calling SkirRPC services through generated typed clients and - Generate typed RPC clients from Skir schemas with the [client generation guide](docs/generating-clients.md). - Use standard PHP objects, [Laravel Data](docs/laravel-data.md), or [Simple Data Objects](docs/simple-data-objects.md). -- Configure Laravel container resolution and matching wire codecs with the [configuration and codecs guide](docs/configuration-and-codecs.md). +- Configure the server URL and endpoint with the [configuration guide](docs/configuration.md). - [Handle failures and test without network requests](docs/error-handling-and-testing.md). ## Quick start @@ -62,12 +62,11 @@ composer dump-autoload php artisan vendor:publish --tag=skir-client-config ``` -Configure the server endpoint and matching wire codec: +Configure the server endpoint: ```dotenv SKIR_CLIENT_BASE_URL=https://api.example.test SKIR_CLIENT_ENDPOINT=/api/skir -SKIR_CLIENT_CODEC=dense_json ``` Call the service through the generated typed client: @@ -92,5 +91,5 @@ Standard PHP is the dependency-light baseline. Use [`skir-laravel-data-generator - [Generating clients](docs/generating-clients.md) - [Laravel Data](docs/laravel-data.md) - [Simple Data Objects](docs/simple-data-objects.md) -- [Configuration and codecs](docs/configuration-and-codecs.md) +- [Configuration](docs/configuration.md) - [Error handling and testing](docs/error-handling-and-testing.md) diff --git a/composer.json b/composer.json index 13602e5..00f5e66 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,10 @@ "type": "path", "url": "../runtime", "options": { - "symlink": false + "symlink": false, + "versions": { + "php-skir/runtime": "0.2.x-dev" + } } } ], diff --git a/config/skir-client.php b/config/skir-client.php index 41a7929..f0c3fdd 100644 --- a/config/skir-client.php +++ b/config/skir-client.php @@ -5,7 +5,6 @@ return [ 'base_url' => env('SKIR_CLIENT_BASE_URL'), 'endpoint' => env('SKIR_CLIENT_ENDPOINT', '/'), - 'codec' => env('SKIR_CLIENT_CODEC', 'dense_json'), 'generator' => [ 'node' => env('SKIR_CLIENT_NODE', 'node'), diff --git a/docs/configuration-and-codecs.md b/docs/configuration-and-codecs.md deleted file mode 100644 index 599b3e3..0000000 --- a/docs/configuration-and-codecs.md +++ /dev/null @@ -1,69 +0,0 @@ -# Configuration and codecs - -Publish the Laravel configuration file: - -```bash -php artisan vendor:publish --tag=skir-client-config -``` - -Set the server base URL, RPC endpoint, and wire codec: - -```dotenv -SKIR_CLIENT_BASE_URL=https://api.example.test -SKIR_CLIENT_ENDPOINT=/api/skir -SKIR_CLIENT_CODEC=dense_json -``` - -`SKIR_CLIENT_BASE_URL` has no default and is required; it identifies the server origin or shared API prefix. `SKIR_CLIENT_ENDPOINT` defaults to `/`, and `SKIR_CLIENT_CODEC` defaults to `dense_json`. - -`SkirConnector` trims a trailing slash from the base URL, while each request resolves the configured relative endpoint. Do not repeat the endpoint path in both values: use `https://api.example.test` plus `/api/skir`, not `https://api.example.test/api/skir` plus `/api/skir`. - -## Resolve or construct the client - -In Laravel, the service provider builds the client from the published configuration: - -```php -use Skir\Client\SkirClient; - -$client = app(SkirClient::class); -``` - -You can also construct clients directly. Dense JSON is the constructor default; the other codecs use `SkirClientCodecs` factories: - -```php -use Skir\Client\Codecs\SkirClientCodecs; -use Skir\Client\SkirClient; - -$denseJson = new SkirClient('https://api.example.test', '/api/skir'); -$explicitDenseJson = new SkirClient( - baseUrl: 'https://api.example.test', - endpoint: '/api/skir', - codec: SkirClientCodecs::denseJson(), -); -$standardJson = new SkirClient( - baseUrl: 'https://api.example.test', - endpoint: '/api/skir', - codec: SkirClientCodecs::standardJson(), -); -$base64DenseJson = new SkirClient( - baseUrl: 'https://api.example.test', - endpoint: '/api/skir', - codec: SkirClientCodecs::base64DenseJson(), -); -``` - -The first two clients use the same dense JSON codec; the second selects its factory explicitly. - -## Choose a matching codec - -The configured codec must match the codec exposed by the server endpoint. - -| `SKIR_CLIENT_CODEC` | Wire shape | -| --- | --- | -| `dense_json` | Compact, descriptor-driven JSON values; structs use positional arrays. | -| `standard_json` | Readable JSON where structs use named object fields and primitives remain primitive values. | -| `base64_dense_json` | A dense JSON payload carried inside a base64-encoded JSON string. | - -An unknown configured codec value throws `SkirClientException` when Laravel resolves the client. - -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/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..a25336e --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,42 @@ +# Configuration + +Publish the Laravel configuration file: + +```bash +php artisan vendor:publish --tag=skir-client-config +``` + +Set the server base URL and RPC endpoint: + +```dotenv +SKIR_CLIENT_BASE_URL=https://api.example.test +SKIR_CLIENT_ENDPOINT=/api/skir +``` + +`SKIR_CLIENT_BASE_URL` has no default and is required; it identifies the server origin or shared API prefix. `SKIR_CLIENT_ENDPOINT` defaults to `/`. + +`SkirConnector` trims a trailing slash from the base URL, while each request resolves the configured relative endpoint. Do not repeat the endpoint path in both values: use `https://api.example.test` plus `/api/skir`, not `https://api.example.test/api/skir` plus `/api/skir`. + +## Resolve or construct the client + +In Laravel, the service provider builds the client from the published configuration: + +```php +use Skir\Client\SkirClient; + +$client = app(SkirClient::class); +``` + +You can also construct one directly: + +```php +use Skir\Client\SkirClient; + +$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. + +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/docs/error-handling-and-testing.md b/docs/error-handling-and-testing.md index ccf3c30..b420213 100644 --- a/docs/error-handling-and-testing.md +++ b/docs/error-handling-and-testing.md @@ -12,10 +12,9 @@ A 2xx response that cannot be decoded also throws `SkirClientException`, but wit The exception boundary is deliberately narrow: -- Only a codec-thrown `SkirRuntimeException` during response decoding is wrapped as an invalid-response `SkirClientException`. +- Only a `SkirRuntimeException` thrown while decoding the response is wrapped as an invalid-response `SkirClientException`. - A `SkirRuntimeException` thrown while encoding the request propagates directly. - DNS, connection, and TLS failures propagate as `Saloon\Exceptions\Request\FatalRequestException` and do not have a response. -- An unsupported configured codec throws `SkirClientException` with both `method` and `response` set to `null`. The following handler is specifically for the `SkirClientException` paths. It does not catch direct request-encoding or Saloon fatal-request exceptions: @@ -68,7 +67,6 @@ final class SkirRpcClientTest extends TestCase { config()->set('skir-client.base_url', 'https://api.example.test'); config()->set('skir-client.endpoint', '/api/skir'); - config()->set('skir-client.codec', 'dense_json'); $transport = app(TransportSkirClient::class); $mockClient = new MockClient([ @@ -87,10 +85,8 @@ final class SkirRpcClientTest extends TestCase $mockClient->assertSent(function (Request $request): bool { return $request instanceof SkirRpcRequest && $request->resolveEndpoint() === '/api/skir' - && $request->body()->all() === [ - 'method' => 'GetUser', - 'request' => [42], - ]; + && $request->descriptor->name === 'GetUser' + && $request->body()->all() === 'GetUser:'.$request->descriptor->number.'::[42]'; }); $mockClient->assertSentCount(1); } @@ -100,7 +96,6 @@ final class SkirRpcClientTest extends TestCase { config()->set('skir-client.base_url', 'https://api.example.test'); config()->set('skir-client.endpoint', '/api/skir'); - config()->set('skir-client.codec', 'dense_json'); $transport = app(TransportSkirClient::class); $mockClient = new MockClient([ @@ -125,7 +120,6 @@ final class SkirRpcClientTest extends TestCase { config()->set('skir-client.base_url', 'https://api.example.test'); config()->set('skir-client.endpoint', '/api/skir'); - config()->set('skir-client.codec', 'dense_json'); $transport = app(TransportSkirClient::class); $mockClient = new MockClient([ @@ -152,4 +146,4 @@ final class SkirRpcClientTest extends TestCase `MockClient` matches the configured response to `SkirRpcRequest`, records the outgoing request, and lets `assertSent()` verify the request class, endpoint, method name, and encoded body. `assertSentCount(1)` also guards against duplicate calls. No network call is made. -See [Configuration and codecs](configuration-and-codecs.md) for transport setup and codec selection, and [Generating clients](generating-clients.md) for the schema and generated classes used above. +See [Configuration](configuration.md) for transport setup, and [Generating clients](generating-clients.md) for the schema and generated classes used above. diff --git a/docs/laravel-data.md b/docs/laravel-data.md index ee75868..935d855 100644 --- a/docs/laravel-data.md +++ b/docs/laravel-data.md @@ -52,7 +52,7 @@ composer dump-autoload ## Call the typed client -Configure the transport endpoint and matching codec as described in [Configuration and codecs](configuration-and-codecs.md), then use the generated client: +Configure the transport endpoint as described in [Configuration](configuration.md), then use the generated client: ```php use Skir\Admin\GetUserRequestData; diff --git a/docs/simple-data-objects.md b/docs/simple-data-objects.md index 0a00b26..ad5e898 100644 --- a/docs/simple-data-objects.md +++ b/docs/simple-data-objects.md @@ -54,7 +54,7 @@ composer dump-autoload ## Call the typed client -Configure the transport endpoint and matching codec as described in [Configuration and codecs](configuration-and-codecs.md), then use the generated client: +Configure the transport endpoint as described in [Configuration](configuration.md), then use the generated client: ```php use Skir\Admin\GetUserRequestData; diff --git a/resources/boost/guidelines/core.blade.php b/resources/boost/guidelines/core.blade.php index 8934a6e..17871da 100644 --- a/resources/boost/guidelines/core.blade.php +++ b/resources/boost/guidelines/core.blade.php @@ -5,9 +5,9 @@ generator-owned and may be replaced; do not edit files inside it. Prefer the generated typed client for application code. Use `Skir\Client\SkirClient::invoke()` only -for debugging or custom transport integrations. Keep the base URL, endpoint, and codec aligned with +for debugging or custom transport integrations. Keep the base URL and endpoint aligned with the server. Use the `skir-client-development` skill whenever generating or configuring a client, diagnosing -transport failures, choosing a codec, or writing client tests. Client tests must use Saloon mocks +transport failures, or writing client tests. Client tests must use Saloon mocks and must not contact a real server. diff --git a/resources/boost/skills/skir-client-development/SKILL.md b/resources/boost/skills/skir-client-development/SKILL.md index 42aa20c..c9c1cc9 100644 --- a/resources/boost/skills/skir-client-development/SKILL.md +++ b/resources/boost/skills/skir-client-development/SKILL.md @@ -12,7 +12,7 @@ typed clients over direct transport calls. - Generation, generator configuration, Composer autoloading, Laravel config, or environment setup: read [generation and configuration](references/generation-and-configuration.md). -- Invocation, codecs, exceptions, debugging, recovery, or network-free tests: read +- Invocation, exceptions, debugging, recovery, or network-free tests: read [invocation, errors, and testing](references/invocation-errors-and-testing.md). - Tasks spanning both areas: read both references before changing code. 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 06c3cb8..2ceadda 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 @@ -53,11 +53,10 @@ Set: ```dotenv SKIR_CLIENT_BASE_URL=https://api.example.test SKIR_CLIENT_ENDPOINT=/api/skir -SKIR_CLIENT_CODEC=dense_json ``` `SKIR_CLIENT_BASE_URL` is required. `SKIR_CLIENT_ENDPOINT` defaults to `/`; do not duplicate the -endpoint path in the base URL. `SKIR_CLIENT_CODEC` defaults to `dense_json` and must match the server. +endpoint path in the base URL. Do not substitute repository-local wrapper commands or machine-specific absolute paths in reusable package guidance. Use the consuming application's documented runtime surface when executing these diff --git a/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md b/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md index 94807f3..4d10c9c 100644 --- a/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md +++ b/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md @@ -7,23 +7,17 @@ Application code should call the generated methods so requests and responses ret types. Use low-level `SkirClient::invoke()` only for debugging or custom integrations; it returns the decoded transport value rather than a generated response object. -The client and server must use the same codec: - -- `dense_json`: descriptor-driven JSON; structs are positional arrays. -- `standard_json`: readable JSON objects with named struct fields. -- `base64_dense_json`: dense JSON inside a base64-encoded JSON string. - -An unsupported configured codec throws `SkirClientException` while Laravel resolves the transport. +The client speaks the standard SkirRPC protocol: it POSTs `MethodName:methodNumber::denseJson` and +decodes a dense JSON response, so it works against any SkirRPC server. ## Distinguish failures | Boundary | Result | Inspect or correct | |---|---|---| | HTTP response considered failed, normally 4xx/5xx | `SkirClientException` with method and response | Inspect status/body and the server error policy | -| Successful HTTP response cannot be decoded | `SkirClientException` with method, null response, previous `SkirRuntimeException` | Verify response shape and matching codec | -| Request encoding fails | `SkirRuntimeException` propagates directly | Verify generated request values and codec input | +| Successful HTTP response cannot be decoded | `SkirClientException` with method, null response, previous `SkirRuntimeException` | Verify the endpoint is a SkirRPC server and the schemas match | +| Request encoding fails | `SkirRuntimeException` propagates directly | Verify generated request values | | DNS, connection, or TLS fails | Saloon `FatalRequestException` propagates with no response | Inspect URL, DNS, TLS, and connectivity | -| Codec is unsupported | `SkirClientException` with null method and response | Correct the configured codec | Responses Saloon does not classify as failed, including redirects by default, continue to decoding. Catching only `SkirClientException` does not catch encoding or network failures. @@ -45,7 +39,7 @@ $user = $client->getUser(new GetUserRequest(userId: 42)); $mockClient->assertSent(fn (Request $request): bool => $request instanceof SkirRpcRequest && $request->resolveEndpoint() === '/api/skir' - && $request->body()->all()['method'] === 'GetUser' + && $request->descriptor->name === 'GetUser' ); $mockClient->assertSentCount(1); ``` diff --git a/src/Codecs/Base64DenseJsonCodec.php b/src/Codecs/Base64DenseJsonCodec.php deleted file mode 100644 index 530f4cd..0000000 --- a/src/Codecs/Base64DenseJsonCodec.php +++ /dev/null @@ -1,39 +0,0 @@ -requestType, $request)); - } - - public function decodeResponse(MethodDescriptor $descriptor, string $response): mixed - { - try { - $encodedDenseJson = json_decode($response, true, flags: JSON_THROW_ON_ERROR); - } catch (JsonException $exception) { - throw SkirRuntimeException::invalidValue("Skir base64 dense JSON response is invalid: {$exception->getMessage()}"); - } - - if (! is_string($encodedDenseJson)) { - throw SkirRuntimeException::invalidValue('Skir base64 dense JSON responses must be JSON strings.'); - } - - $denseJson = base64_decode($encodedDenseJson, true); - - if ($denseJson === false) { - throw SkirRuntimeException::invalidValue('Skir base64 dense JSON response contains invalid base64.'); - } - - return DenseJson::fromJson($descriptor->responseType, $denseJson); - } -} diff --git a/src/Codecs/DenseJsonCodec.php b/src/Codecs/DenseJsonCodec.php deleted file mode 100644 index ff63baf..0000000 --- a/src/Codecs/DenseJsonCodec.php +++ /dev/null @@ -1,21 +0,0 @@ -requestType, $request); - } - - public function decodeResponse(MethodDescriptor $descriptor, string $response): mixed - { - return DenseJson::fromJson($descriptor->responseType, $response); - } -} diff --git a/src/Codecs/SkirClientCodec.php b/src/Codecs/SkirClientCodec.php deleted file mode 100644 index 4b6928e..0000000 --- a/src/Codecs/SkirClientCodec.php +++ /dev/null @@ -1,14 +0,0 @@ -getMessage()}"); - } - } -} diff --git a/src/Exceptions/SkirClientException.php b/src/Exceptions/SkirClientException.php index 2972b6a..c35c4b2 100644 --- a/src/Exceptions/SkirClientException.php +++ b/src/Exceptions/SkirClientException.php @@ -37,9 +37,4 @@ public static function invalidResponse(MethodDescriptor $method, Throwable $prev previous: $previous, ); } - - public static function unsupportedCodec(string $codec): self - { - return new self("Skir client codec [{$codec}] is not supported."); - } } diff --git a/src/Http/SkirRpcRequest.php b/src/Http/SkirRpcRequest.php index 76644ad..3f2406b 100644 --- a/src/Http/SkirRpcRequest.php +++ b/src/Http/SkirRpcRequest.php @@ -7,20 +7,19 @@ use Saloon\Contracts\Body\HasBody; use Saloon\Enums\Method; use Saloon\Http\Request; -use Saloon\Traits\Body\HasJsonBody; -use Skir\Client\Codecs\SkirClientCodec; +use Saloon\Traits\Body\HasStringBody; +use Skir\Runtime\DenseJson; use Skir\Runtime\MethodDescriptor; final class SkirRpcRequest extends Request implements HasBody { - use HasJsonBody; + use HasStringBody; protected Method $method = Method::POST; public function __construct( - private readonly MethodDescriptor $descriptor, - private readonly mixed $request, - private readonly SkirClientCodec $codec, + public readonly MethodDescriptor $descriptor, + public readonly mixed $request, private readonly string $endpoint = '/', ) {} @@ -31,15 +30,20 @@ public function resolveEndpoint(): string /** * @return array{ - * method: string, - * request: mixed + * Content-Type: string * } */ - protected function defaultBody(): array + protected function defaultHeaders(): array { return [ - 'method' => $this->descriptor->name, - 'request' => $this->codec->encodeRequest($this->descriptor, $this->request), + 'Content-Type' => 'text/plain; charset=utf-8', ]; } + + protected function defaultBody(): string + { + $requestJson = DenseJson::toJson($this->descriptor->requestType, $this->request); + + return "{$this->descriptor->name}:{$this->descriptor->number}::{$requestJson}"; + } } diff --git a/src/SkirClient.php b/src/SkirClient.php index c71be3c..f235501 100644 --- a/src/SkirClient.php +++ b/src/SkirClient.php @@ -5,11 +5,10 @@ namespace Skir\Client; use Saloon\Http\Faking\MockClient; -use Skir\Client\Codecs\DenseJsonCodec; -use Skir\Client\Codecs\SkirClientCodec; use Skir\Client\Exceptions\SkirClientException; use Skir\Client\Http\SkirConnector; use Skir\Client\Http\SkirRpcRequest; +use Skir\Runtime\DenseJson; use Skir\Runtime\Exceptions\SkirRuntimeException; use Skir\Runtime\MethodDescriptor; @@ -20,7 +19,6 @@ final class SkirClient public function __construct( string $baseUrl, private readonly string $endpoint = '/', - private readonly SkirClientCodec $codec = new DenseJsonCodec, ) { $this->connector = new SkirConnector($baseUrl); } @@ -35,7 +33,7 @@ public function withMockClient(MockClient $mockClient): self public function invoke(MethodDescriptor $descriptor, mixed $request): mixed { $response = $this->connector->send( - new SkirRpcRequest($descriptor, $request, $this->codec, $this->endpoint), + new SkirRpcRequest($descriptor, $request, $this->endpoint), ); if ($response->failed()) { @@ -43,7 +41,7 @@ public function invoke(MethodDescriptor $descriptor, mixed $request): mixed } try { - return $this->codec->decodeResponse($descriptor, $response->body()); + return DenseJson::fromJson($descriptor->responseType, $response->body()); } catch (SkirRuntimeException $exception) { throw SkirClientException::invalidResponse($descriptor, $exception); } diff --git a/src/SkirClientServiceProvider.php b/src/SkirClientServiceProvider.php index 4a4eb80..9d8ecf7 100644 --- a/src/SkirClientServiceProvider.php +++ b/src/SkirClientServiceProvider.php @@ -5,10 +5,7 @@ namespace Skir\Client; use Illuminate\Support\ServiceProvider; -use Skir\Client\Codecs\SkirClientCodec; -use Skir\Client\Codecs\SkirClientCodecs; use Skir\Client\Commands\GenerateClientCommand; -use Skir\Client\Exceptions\SkirClientException; final class SkirClientServiceProvider extends ServiceProvider { @@ -20,7 +17,6 @@ public function register(): void return new SkirClient( baseUrl: (string) config('skir-client.base_url'), endpoint: (string) config('skir-client.endpoint', '/'), - codec: $this->codecFromConfig((string) config('skir-client.codec', 'dense_json')), ); }); } @@ -37,14 +33,4 @@ public function boot(): void ]); } } - - private function codecFromConfig(string $codec): SkirClientCodec - { - return match ($codec) { - 'dense_json' => SkirClientCodecs::denseJson(), - 'standard_json' => SkirClientCodecs::standardJson(), - 'base64_dense_json' => SkirClientCodecs::base64DenseJson(), - default => throw SkirClientException::unsupportedCodec($codec), - }; - } } diff --git a/tests/Feature/BoostAssetsTest.php b/tests/Feature/BoostAssetsTest.php index 0e72360..5bb81fe 100644 --- a/tests/Feature/BoostAssetsTest.php +++ b/tests/Feature/BoostAssetsTest.php @@ -82,7 +82,6 @@ public function it_keeps_client_guidance_anchored_to_the_released_package(): voi [ 'SKIR_CLIENT_BASE_URL', 'SKIR_CLIENT_ENDPOINT', - 'SKIR_CLIENT_CODEC', 'SKIR_CLIENT_ROOT', 'SKIR_CLIENT_SKIR_BIN', 'SKIR_CLIENT_NODE', @@ -93,9 +92,6 @@ public function it_keeps_client_guidance_anchored_to_the_released_package(): voi foreach ([ 'configure-composer', 'composer dump-autoload', - 'dense_json', - 'standard_json', - 'base64_dense_json', 'SkirClientException', 'SkirRuntimeException', 'FatalRequestException', diff --git a/tests/Feature/SkirClientServiceProviderTest.php b/tests/Feature/SkirClientServiceProviderTest.php index c752b80..147c168 100644 --- a/tests/Feature/SkirClientServiceProviderTest.php +++ b/tests/Feature/SkirClientServiceProviderTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\Attributes\Test; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; -use Skir\Client\Exceptions\SkirClientException; use Skir\Client\Http\SkirRpcRequest; use Skir\Client\SkirClient; use Skir\Client\Tests\TestCase; @@ -40,52 +39,4 @@ public function it_resolves_a_configured_client_from_the_container(): void return $request->resolveEndpoint() === '/rpc'; }); } - - #[Test] - public function it_resolves_a_configured_standard_json_client_from_the_container(): void - { - config()->set('skir-client.base_url', 'https://example.com/api'); - config()->set('skir-client.codec', 'standard_json'); - - $mockClient = new MockClient([ - SkirRpcRequest::class => MockResponse::make([ - 'id' => 42, - ], 200), - ]); - - $client = app(SkirClient::class); - $client->withMockClient($mockClient); - - $result = $client->invoke( - new MethodDescriptor('FindUser', 1002, Type::struct([]), Type::struct([])), - [ - 'id' => 42, - ], - ); - - $this->assertSame([ - 'id' => 42, - ], $result); - - $mockClient->assertSent(function (SkirRpcRequest $request): bool { - return $request->body()->all() === [ - 'method' => 'FindUser', - 'request' => [ - 'id' => 42, - ], - ]; - }); - } - - #[Test] - public function it_fails_with_a_package_exception_for_unknown_configured_codecs(): void - { - config()->set('skir-client.base_url', 'https://example.com/api'); - config()->set('skir-client.codec', 'xml'); - - $this->expectException(SkirClientException::class); - $this->expectExceptionMessage('Skir client codec [xml] is not supported.'); - - app(SkirClient::class); - } } diff --git a/tests/Feature/SkirClientTest.php b/tests/Feature/SkirClientTest.php index 155e4b0..3dc1a51 100644 --- a/tests/Feature/SkirClientTest.php +++ b/tests/Feature/SkirClientTest.php @@ -8,14 +8,10 @@ use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; use Saloon\Http\Request; -use Skir\Client\Codecs\SkirClientCodec; -use Skir\Client\Codecs\SkirClientCodecs; use Skir\Client\Exceptions\SkirClientException; use Skir\Client\Http\SkirRpcRequest; use Skir\Client\SkirClient; use Skir\Client\Tests\TestCase; -use Skir\Runtime\DenseJson; -use Skir\Runtime\Exceptions\SkirRuntimeException; use Skir\Runtime\Field; use Skir\Runtime\MethodDescriptor; use Skir\Runtime\Type; @@ -44,10 +40,7 @@ public function it_sends_a_skir_rpc_request_and_decodes_the_dense_json_response( $mockClient->assertSent(function (Request $request): bool { return $request instanceof SkirRpcRequest && $request->resolveEndpoint() === '/' - && $request->body()->all() === [ - 'method' => 'Square', - 'request' => 5.0, - ]; + && $request->body()->all() === 'Square:1001::5'; }); } @@ -83,102 +76,7 @@ public function it_encodes_struct_requests_and_decodes_struct_responses(): void $mockClient->assertSent(function (Request $request): bool { return $request instanceof SkirRpcRequest - && $request->body()->all() === [ - 'method' => 'RenameUser', - 'request' => [42, 'Maxim'], - ]; - }); - } - - #[Test] - public function it_can_use_standard_json_for_readable_http_payloads(): void - { - $mockClient = new MockClient([ - SkirRpcRequest::class => MockResponse::make([ - 'id' => 42, - 'name' => 'Ruben', - ], 200), - ]); - - $client = new SkirClient('https://example.com/api', codec: SkirClientCodecs::standardJson()); - $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() === [ - 'method' => 'RenameUser', - 'request' => [ - 'id' => 42, - 'name' => 'Maxim', - ], - ]; - }); - } - - #[Test] - public function it_can_use_base64_encoded_dense_json_payloads(): void - { - $userType = Type::struct([ - Field::value('id', 0, Type::int32()), - Field::value('name', 1, Type::string()), - ]); - - $mockClient = new MockClient([ - SkirRpcRequest::class => MockResponse::make( - json_encode( - base64_encode(DenseJson::toJson($userType, [ - 'id' => 42, - 'name' => 'Ruben', - ])), - JSON_THROW_ON_ERROR, - ), - 200, - ), - ]); - - $client = new SkirClient('https://example.com/api', codec: SkirClientCodecs::base64DenseJson()); - $client->withMockClient($mockClient); - - $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) use ($userType): bool { - return $request instanceof SkirRpcRequest - && $request->body()->all() === [ - 'method' => 'RenameUser', - 'request' => base64_encode(DenseJson::toJson($userType, [ - 'id' => 42, - 'name' => 'Maxim', - ])), - ]; + && $request->body()->all() === 'RenameUser:1002::[42,"Maxim"]'; }); } @@ -226,47 +124,4 @@ public function it_localises_invalid_dense_json_responses(): void 5.0, ); } - - #[Test] - public function it_localises_invalid_standard_json_responses(): void - { - $this->assertInvalidResponseIsLocalised( - SkirClientCodecs::standardJson(), - 'not-json', - 'Skir standard JSON response is invalid:', - ); - } - - #[Test] - public function it_localises_invalid_base64_dense_json_responses(): void - { - $this->assertInvalidResponseIsLocalised( - SkirClientCodecs::base64DenseJson(), - '"%%%"', - 'Skir base64 dense JSON response contains invalid base64.', - ); - } - - private function assertInvalidResponseIsLocalised( - SkirClientCodec $codec, - string $responseBody, - string $runtimeExceptionMessage, - ): void { - $mockClient = new MockClient([ - MockResponse::make($responseBody, 200), - ]); - $descriptor = new MethodDescriptor('Square', 1001, Type::float32(), Type::float32()); - $client = new SkirClient('https://example.com/api', codec: $codec); - $client->withMockClient($mockClient); - - try { - $client->invoke($descriptor, 5.0); - $this->fail('Expected an invalid RPC response to throw.'); - } catch (SkirClientException $exception) { - $this->assertSame('Skir RPC response for [Square] could not be decoded.', $exception->getMessage()); - $this->assertSame($descriptor, $exception->method); - $this->assertInstanceOf(SkirRuntimeException::class, $exception->getPrevious()); - $this->assertStringContainsString($runtimeExceptionMessage, $exception->getPrevious()->getMessage()); - } - } }