|
| 1 | +--- |
| 2 | +name: fast-forward-http-client |
| 3 | +description: Provide Fast Forward's Symfony-backed PSR-18 client, supply its required PSR-17 factories, and test success and transport failures without a network. |
| 4 | +license: MIT; see LICENSE |
| 5 | +compatibility: Supports fast-forward/http-client ^1.2 and PHP ^8.3. Requires Composer and declared extensions; inspect the installed version. Earlier releases are outside this contract. |
| 6 | +metadata: |
| 7 | + author: Fast Forward |
| 8 | + api-baseline: fast-forward/http-client ^1.2 |
| 9 | +--- |
| 10 | + |
| 11 | +# Wire a PSR-18 HTTP client |
| 12 | + |
| 13 | +Use this skill for an application consuming `fast-forward/http-client:^1.2`. |
| 14 | +Read its instructions, manifest/lockfile and installed source before editing. |
| 15 | +Run `composer show fast-forward/http-client`. For versions outside this contract, |
| 16 | +inspect matching documentation; do not silently upgrade the application. |
| 17 | + |
| 18 | +## First working integration |
| 19 | + |
| 20 | +Requires PHP ^8.3, Composer's application autoloader and this package's runtime |
| 21 | +dependencies. The direct example also needs `nyholm/psr7:^1.8`. |
| 22 | +With authority to install the dependency: |
| 23 | + |
| 24 | +```sh |
| 25 | +composer require 'fast-forward/http-client:^1.2' 'nyholm/psr7:^1.8' |
| 26 | +``` |
| 27 | + |
| 28 | +Create `app.php` in the application root alongside composer.json/vendor: |
| 29 | + |
| 30 | +```php |
| 31 | +<?php |
| 32 | + |
| 33 | +declare(strict_types=1); |
| 34 | + |
| 35 | +require __DIR__ . '/vendor/autoload.php'; |
| 36 | + |
| 37 | +use FastForward\Config\ArrayConfig; |
| 38 | +use FastForward\Container\ContainerInterface; |
| 39 | +use FastForward\Container\ServiceProvider\ArrayServiceProvider; |
| 40 | +use FastForward\Http\Client\ServiceProvider\HttpClientServiceProvider; |
| 41 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 42 | +use Psr\Http\Client\ClientExceptionInterface; |
| 43 | +use Psr\Http\Client\ClientInterface; |
| 44 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 45 | +use Psr\Http\Message\StreamFactoryInterface; |
| 46 | +use Symfony\Component\HttpClient\HttpClient; |
| 47 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 48 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 49 | +use Symfony\Component\HttpClient\Exception\TransportException; |
| 50 | +use function FastForward\Container\container; |
| 51 | + |
| 52 | +$config = new ArrayConfig([ContainerInterface::class => [HttpClientServiceProvider::class]]); |
| 53 | +$factory = new Psr17Factory(); |
| 54 | +$makeContainer = static function (MockHttpClient $transport) use ($config, $factory): ContainerInterface { |
| 55 | + return container( |
| 56 | + new ArrayServiceProvider([ |
| 57 | + HttpClient::class => static fn(): MockHttpClient => $transport, |
| 58 | + ResponseFactoryInterface::class => static fn(): Psr17Factory => $factory, |
| 59 | + StreamFactoryInterface::class => static fn(): Psr17Factory => $factory, |
| 60 | + ]), |
| 61 | + $config, |
| 62 | + ); |
| 63 | +}; |
| 64 | +$services = $makeContainer(new MockHttpClient(new MockResponse('{"ready":true}', ['http_code' => 200]))); |
| 65 | +$client = $services->get(ClientInterface::class); |
| 66 | +$response = $client->sendRequest($factory->createRequest('GET', 'https://example.invalid/health')); |
| 67 | +if ($response->getStatusCode() !== 200 || (string) $response->getBody() !== '{"ready":true}') { |
| 68 | + throw new RuntimeException('Client response differs.'); |
| 69 | +} |
| 70 | +$failedClient = $makeContainer(new MockHttpClient(static function (): never { |
| 71 | + throw new TransportException('offline fixture'); |
| 72 | +}))->get(ClientInterface::class); |
| 73 | + |
| 74 | +echo "integration ready\n"; |
| 75 | +``` |
| 76 | + |
| 77 | +Run `php app.php`. It throws on a wrong result; success prints |
| 78 | +`integration ready`. It uses no production data and starts no server. |
| 79 | + |
| 80 | +## Wiring and failure boundaries |
| 81 | + |
| 82 | +The primary product is `FastForward\Http\Client\ServiceProvider\HttpClientServiceProvider`. Place that class in ArrayConfig under `FastForward\Container\ContainerInterface::class`, pass the config to `FastForward\Container\container($config)`, and request `Psr\Http\Client\ClientInterface`. It uses Symfony HttpClient::create and Psr18Client. A provider-only application may pass the provider class directly. |
| 83 | + |
| 84 | +This package requires PSR-17 interfaces but does not ship a concrete message implementation or factory provider. In the application install `fast-forward/http-factory:^1.2` and register `FastForward\Http\Message\Factory\ServiceProvider\HttpMessageFactoryServiceProvider::class` before the client provider. Alternatively supply ResponseFactoryInterface and StreamFactoryInterface through your own ArrayServiceProvider. The direct example needs `nyholm/psr7:^1.8` explicitly; MockHttpClient/MockResponse come from the required Symfony component, not a test-only package. |
| 85 | + |
| 86 | +Put custom factories/transport in an earlier ArrayServiceProvider; first successfully resolving source wins. Provider services are cached. A missing PSR-17 factory can make get(ClientInterface::class) fail even if has() is true. The helper constructs the composed container; your application passes the resolved client to services that send requests. |
| 87 | + |
| 88 | +Read and validate application configuration before constructing a custom Symfony transport; this provider has no invented timeout/environment config key. For custom timeout/base URI/auth choose HttpClient::create($options) in an explicit factory. PSR-18 HTTP 4xx/5xx responses are returned normally; catch ClientExceptionInterface for client/transport failures. Keep credentials in the application's secret mechanism and avoid real network calls in tests. |
| 89 | + |
| 90 | +The helper tries aggregate sources in registration order. A PSR container or |
| 91 | +not-found exception from one source permits fallback to another source; other |
| 92 | +throwables propagate. Source priority is therefore the first successful resolution, |
| 93 | +not a guarantee that an earlier broken factory terminates lookup. Nested provider |
| 94 | +list errors during helper composition are caught; if services are absent, inspect |
| 95 | +the raw configuration and resolve the provider directly to diagnose the failure. |
| 96 | + |
| 97 | +## Verify integration |
| 98 | + |
| 99 | +Run the [complete example](examples/integration.php) against the application's |
| 100 | +autoloader in a disposable process. It checks success and negative cases: |
| 101 | + |
| 102 | +```sh |
| 103 | +php /path/to/fast-forward-http-client/examples/integration.php ./vendor/autoload.php |
| 104 | +``` |
| 105 | + |
| 106 | +Expected final marker: `fast-forward-http-client: ok`. Keep application-specific regression |
| 107 | +tests in its existing PHPUnit suite, with each fixture class in its same-named |
| 108 | +file. File copying, host discovery and completed agent tasks are different checks. |
| 109 | +For optional copying/synchronization load |
| 110 | +[installation and lifecycle](references/installation.md). |
0 commit comments