@dbu
@dbu
@sagikazarmark
@sagikazarmark
Request / Response
Interfaces for Request & Response
Immutable, withX methods return a copy
$request = new GuzzleHttp\Psr7\Request();
$request = $request
->withMethod('POST')
->withUri($uri)
->withBody($body)
;
var_dump(json_decode($response->getBody()));
Server
create request
send request
Interface for message factory
=> decouple from message implementation
$request = $requestFactory->createRequest(
$method,
$url,
$headers,
$body
);
$request->withHeader(...);
Server
create request
send request
Message Factory
Interface for HTTP clients
=> decouple from client implementation
$response = $client
->sendRequest($request);
Server
create request
send request
HTTPlug
Simplistic API for the tutorial
$promise = $client->sendAsync($request);
$promise->then(
[$this, 'onSuccess'],
[$this, 'onFailure']
);
...
$promise->wait();
public function __construct(
HttpClient $client = null,
MessageFactory $messageFactory = null
) {
$this->client = $client ?:
HttpClientDiscovery::find();
$this->messageFactory = $messageFactory ?:
MessageFactoryDiscovery::find();
}
Transport: HTTP
Serialization: JSON/XML/etc
Error handling (HTTP Status)
Domain model
Error handling (Domain)
HTTP factories
<?php
$request = $factory->createReques('GET', '/api/user')
->withHeader('Authentication', 'Bearer token')
->WithHeader()
//...
;
HTTP Client
OpenAPI spec to PHP code
https://github.com/jolicode/jane-openapi
{
"version": "1.0",
"name": "php-http/guzzle6-adapter",
"bindings": {
"04b5a002-71a8-473d-a8df-75671551b84a": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Adapter\\Guzzle6\\Client",
"type": "Http\\Client\\HttpClient"
}
}
}
We are glad for any feedback!