2.0 KiB
2.0 KiB
HTTP Adapters
Overview
An HTTP adapter is a component that manages the communication between your application and an API. It abstracts the details of making HTTP requests and handling responses, allowing you to use different HTTP clients (like Guzzle, cURL, or custom clients) interchangeably without changing the way you interact with the API.
MollieHttpAdapterPicker
The MollieHttpAdapterPicker is responsible for selecting the appropriate HTTP adapter based on the environment or the provided HTTP client. If no client is specified in the MollieApiClient constructor, it picks a default adapter.
How It Works
- No Client Specified: If no client is provided, it checks if Guzzle is available and picks the appropriate version of the Guzzle adapter.
- Custom Client Provided: If a custom client is provided and it implements the
HttpAdapterContract, it is used directly. If it's a Guzzle client, it is wrapped in aGuzzleMollieHttpAdapter. - Unrecognized Client: Throws an
UnrecognizedClientExceptionif the client is not recognized.
Creating a Custom Adapter
To create a custom HTTP adapter:
- Implement HttpAdapterContract.
- Use HasDefaultFactories Trait to simplify adapter implementation.
use Mollie\Api\Contracts\HttpAdapterContract;
use Mollie\Api\Traits\HasDefaultFactories;
class MyCustomHttpAdapter implements HttpAdapterContract {
use HasDefaultFactories;
public function sendRequest(PendingRequest $pendingRequest): Response {
// Implementation for sending HTTP request
}
public function version(): ?string {
return 'my-custom-adapter/1.0';
}
}
Available Adapters
Out of the box, the Mollie API client provides several adapters:
- GuzzleMollieHttpAdapter: Wraps a Guzzle HTTP client for sending requests.
- CurlMollieHttpAdapter: Uses cURL for sending HTTP requests. This is the default if Guzzle is not available.
- PSR18MollieHttpAdapter: PSR-18 compatible HTTP adapter