HTTP Transporter
Relevant source files
The following files were used as context for generating this wiki page:
The HttpTransporter class executes all network communication between the PHP SDK and the Resend API. It implements the Transporter contract to provide a standardized way of sending requests using PSR-18 compliant HTTP clients.
This component handles the lifecycle of an API call, including request construction from value objects, payload serialization, response decoding, and comprehensive error mapping.
Sources: src/Transporters/HttpTransporter.php:15-22, src/Transporters/HttpTransporter.php:41-45
Core Components and Architecture
The transporter architecture relies on four primary components to facilitate API communication:
- HttpTransporter: The central engine that manages the request/response lifecycle.
- ClientInterface: A PSR-18 compliant HTTP client (e.g., Guzzle) that performs the actual network I/O.
- BaseUri: A value object containing the API's base address (e.g.,
api.resend.com). - Payload: An object containing the resource path, HTTP method, and parameters for a specific request.
Sources: src/Transporters/HttpTransporter.php:26-30, composer.json:14-18
Request Flow Diagram
The following sequence diagram illustrates the process of sending a request through the transporter:
The sequence shows how the transporter mediates between the internal SDK domain and the external network layer.
Sources: src/Transporters/HttpTransporter.php:41-55, src/ValueObjects/Transporter/Payload.php:161-165
Data Serialization and Transformation
The Payload object transforms internal data into PSR-7 request instances. It supports multiple content types and HTTP methods based on the API operation requested.
Supported Operations
| Operation Type | HTTP Method | Implementation |
|---|---|---|
| Create | POST | Payload::create() |
| List | GET | Payload::list() |
| Get | GET | Payload::get() |
| Update | PATCH | Payload::update() |
| Replace | PUT | Payload::replace() |
| Delete | DELETE | Payload::delete() |
| Upload | POST | Payload::upload() |
Sources: src/ValueObjects/Transporter/Payload.php:37-147, tests/ValueObjects/Transporter/Payload.php:13-90
Content Handling
The transporter handles two primary data formats:
- JSON: Default for most operations. It converts empty parameters into JSON objects
{}rather than arrays[]to maintain API compatibility. - Multipart: Used for file uploads. It generates a
MultipartStreamwith a unique boundary and attaches it to the request.
Sources: src/ValueObjects/Transporter/Payload.php:181-209, tests/ValueObjects/Transporter/Payload.php:117-151
Error Handling and Exception Mapping
The transporter validates every response before returning data to the caller. It throws specific exceptions based on the failure type.
Processing Logic Flow
The flowchart describes the decision tree used in the request and throwIfJsonError methods.
Sources: src/Transporters/HttpTransporter.php:41-76, src/Transporters/HttpTransporter.php:101-131
Recognized Resend Errors
The transporter specifically monitors for a predefined set of error names returned by the API.
| Error Category | Specific Error Names |
|---|---|
| Authentication | invalid_api_key, missing_api_key, restricted_api_key |
| Quotas | daily_quota_exceeded, monthly_quota_exceeded, rate_limit_exceeded |
| Validation | invalid_parameter, missing_required_field, validation_error |
| Infrastructure | internal_server_error, method_not_allowed, not_found |
Sources: src/Transporters/HttpTransporter.php:141-161
Conclusion
The HttpTransporter provides a robust, PSR-compliant foundation for API communication. By decoupling request construction into Payload objects and response validation into specialized error-handling methods, it ensures the SDK remains resilient to network failures and API-specific errors.
Sources: src/Transporters/HttpTransporter.php:15-163, src/ValueObjects/Transporter/Payload.php:21-213