ProtocolTranslator

Trait ProtocolTranslator 

Source
pub trait ProtocolTranslator: Send + Sync {
    // Required methods
    fn translate_request(
        &self,
        req: &Request<TranslationBody>,
    ) -> Result<TranslatedRequest, TranslationError>;
    fn translate_response(
        &self,
        resp: &Response<TranslationBody>,
    ) -> Result<TranslatedResponse, TranslationError>;
    fn content_type_in(&self) -> &str;
    fn content_type_out(&self) -> &str;
}
Expand description

Translate a request/response pair between two wire protocols.

A translator is constructed at config publish from a route’s translation block and held by the request path for the duration of one request. translate_request converts the inbound request body to the upstream’s wire format; translate_response converts the upstream’s response body back to the client’s wire format. content_type_in is the media type the client sends (and expects back); content_type_out is the media type the upstream expects (and sends back).

The trait is synchronous and operates on fully-buffered bodies (TranslationBody): protocol translation is an explicitly buffering step (the same posture as the DW-028 body transform and the DW-061 aggregation plugin), never a tax on the zero-buffering proxy path. A route that configures translation opts its requests into the buffer-and-convert path; a route without the block streams untouched.

Required Methods§

Source

fn translate_request( &self, req: &Request<TranslationBody>, ) -> Result<TranslatedRequest, TranslationError>

Convert the inbound request to the upstream’s wire format. The translator reads req’s body (via TranslationBody::as_bytes) and returns the converted method, path, headers, and body to send upstream.

Source

fn translate_response( &self, resp: &Response<TranslationBody>, ) -> Result<TranslatedResponse, TranslationError>

Convert the upstream response to the client’s wire format. The translator reads resp’s body and returns the converted status, headers, and body to send the client.

Source

fn content_type_in(&self) -> &str

The media type the CLIENT sends and expects back (e.g. application/json for a REST client, text/xml for a SOAP client).

Source

fn content_type_out(&self) -> &str

The media type the UPSTREAM expects and sends back (e.g. application/graphql for a GraphQL upstream, application/xml for a SOAP upstream, application/grpc for a gRPC upstream).

Implementors§