Download the PHP package bentools/typed-http-client without Composer
On this page you can find all versions of the php package bentools/typed-http-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package typed-http-client
Symfony Typed HTTP Client
A thin, type-safe decorator around Symfony's HTTP Client that hydrates responses into real objects instead of plain arrays.
You keep the whole HttpClientInterface API — including streaming — and get a getData() method on every response, returning whatever type your factory produces. Static analyzers (PHPStan, Psalm) follow the type all the way through, thanks to generics annotations.
Installation
Requires PHP >= 8.2. The library only depends on symfony/contracts; bring your own HttpClientInterface implementation (e.g. symfony/http-client, used by default when installed).
Usage
Quick start with a closure
The fastest way to get typed data out of a response is to pass a closure. It receives the raw response, the $throw flag, and a RequestContext:
Dedicated factory classes
For anything reusable, implement DataFactoryInterface. The @implements annotation is what makes generics click:
Discriminating by request
The RequestContext carries the method, url and options of the originating request, so a single factory can serve several endpoints:
Deriving clients
withFactory() returns a new client with a different factory, on the same transport. Handy for scoping one client per resource:
withOptions() works exactly like Symfony's, and keeps the factory:
Streaming
stream() works like Symfony's, and yields chunks keyed by the typed response:
Default behavior
Without a factory, the client falls back to ArrayDataFactory: getData() simply returns the JSON-decoded body as an array.
Error handling
getData(bool $throw = true) follows Symfony's convention: with $throw = true (the default), HTTP 4xx/5xx responses raise the usual ClientException / ServerException. Pass false to let your factory deal with erroneous payloads itself — the flag is forwarded to it.
Data is resolved lazily (nothing is decoded until you call getData()) and memoized (the factory runs at most once per response).
Design notes
TypedHttpClientimplementsHttpClientInterface: it is a drop-in replacement anywhere a Symfony HTTP client is expected.- Responses returned by
request()areDataAwareResponseinstances, which decorate the inner response and implementResponseInterface— every native method (getStatusCode(),getHeaders(),toArray(), …) is still available. stream()only accepts responses created by aTypedHttpClient, and throws aLogicExceptionotherwise.
Development
License
MIT.