Skip to main content

HttpClient

Trait HttpClient 

Source
pub trait HttpClient: Send + Sync {
    // Required method
    fn send(
        &self,
        request: HttpRequest,
    ) -> impl Future<Output = Result<HttpResponse, HttpClientError>> + Send;
}
Expand description

Trait for pluggable HTTP client backends.

Implement this trait to use any HTTP library with the Mesa SDK. The SDK handles authorization headers, JSON serialization, and retry logic — your implementation only needs to execute the raw HTTP request and return the response.

§What the SDK handles

Before calling send, the SDK:

  • Sets the Authorization: Bearer <api-key> header
  • Sets Content-Type: application/json for requests with a body
  • Sets Accept: application/json
  • Serializes request bodies to JSON bytes

After send returns, the SDK:

  • Deserializes the response body from JSON
  • Checks the status code for errors
  • Retries on transient failures (429, 5xx, timeouts, connection errors)

§What you implement

Your send method should:

  1. Execute the HTTP request described by HttpRequest
  2. Return the status code, headers, and raw body bytes in HttpResponse
  3. Map transport errors to HttpClientError variants

§Error mapping for retries

The SDK uses HttpClientError variants to decide whether to retry:

Map your HTTP library’s errors accordingly to get correct retry behavior.

§Example

use mesa_dev::{ClientBuilder, HttpClient, HttpRequest, HttpResponse, error::HttpClientError};

struct MyClient;

impl HttpClient for MyClient {
    async fn send(
        &self,
        request: HttpRequest,
    ) -> Result<HttpResponse, HttpClientError> {
        // Execute request.method against request.url with request.headers
        // and optional request.body, then return HttpResponse.
        todo!()
    }
}

let client = ClientBuilder::new("my-api-key").build_with(MyClient);

Required Methods§

Source

fn send( &self, request: HttpRequest, ) -> impl Future<Output = Result<HttpResponse, HttpClientError>> + Send

Send an HTTP request and return the response.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§