Middlewares are classes that implement the MiddlewareInterface
. By default, middlewares from guzzle are used (except http_errors
) and the client adds 3 custom middlewares:
- ResponseExceptionMiddleware - throws exceptions for status codes between 400 - 500
- UnexpectedErrorMiddleware - wraps an exception that is not expected into the
UnexpectedErrorException
- AuthenticationMiddleware - adds the
Authorization
header
To register your own middleware, use the CrawlerClient::withMiddlewares()
method, which returns a new instance of the client.
To remove the middleware use the CrawlerClient::withoutMiddlewares()
method.
$client = $client
->withMiddlewares(new MyCustomMiddleware())
->withoutMiddlewares('unexpected_error');
If you would like to register some of "legacy" middlewares from Guzzle, it is possible to wrap them in an instance of the ClosureMiddleware
object.
use GuzzleHttp\Middleware;
use SixtyEightPublishers\CrawlerClient\Middleware\ClosureMiddleware;
$container = [];
$client = $client->withMiddlewares(new ClosureMiddleware('history', 10, Middleware::history($container)));
The client uses an object implementing the SerializerInterface
for serialization and deserialization of request and response bodies.
Serializer is implemented using jms/serializer.
The default serializer is implemented using jms/serializer and can be changed via the method CrawlerClient::withSerializer()
.
$client = $client->withSerializer(new MyCustomSerializer());
It is not a good idea to change the default implementation completely, the option to replace the serializer is more for emergencies, in case some error occurs and you need to fix it quickly in the application.