conjure_runtime/raw/
mod.rs

1// Copyright 2020 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! "Raw" HTTP client APIs.
15//!
16//! The [`conjure_runtime::Client`] wraps a raw HTTP client, which is used to handle the actual HTTP communication. A
17//! default raw client is provided, but this can be overridden if desired.
18//!
19//! # Behavior
20//!
21//! The raw client interacts directly with the [`http::Request`] and [`http::Response`] types, with a body type
22//! implementing the [`http_body::Body`] trait rather than using the [`AsyncBody`] type. The request's URI is provided in
23//! absolute-form, and all headers have already been set in the header map. The HTTP response should be
24//! returned directly, without any interpretation of the status code, handling of redirects, etc.
25//!
26//! A raw client is expected to implement `Service<Request<RawBody>, Response = Response<B>>`, where `B` implements the
27//! [`http_body::Body`] trait. The error type returned by the client must implement `Into<Box<dyn Error + Sync + Send>>`
28//! and be safe-loggable.
29//!
30//! Some configuration set in the [`Builder`] affects the raw client, including the connect timeout, security
31//! configuration, and proxy configuration. The default raw client respects these settings, but other implementations
32//! will need to handle them on their own.
33//!
34//! [`conjure_runtime::Client`]: crate::Client
35//! [`AsyncBody`]: conjure_http::client::AsyncBody
36use crate::builder;
37pub use crate::raw::body::*;
38pub use crate::raw::default::*;
39use crate::Builder;
40use conjure_error::Error;
41use std::future::Future;
42use std::sync::Arc;
43
44mod body;
45mod default;
46
47/// An asynchronous function from request to response.
48///
49/// This trait is based on the `tower::Service` trait, but differs in two ways. It does not have a `poll_ready` method
50/// as our client-side backpressure depends on the request, and the `call` method takes `&self` rather than `&mut self`
51/// as our client is designed to be used through a shared reference.
52pub trait Service<R> {
53    /// The response type returned by the service.
54    type Response;
55    /// The error type returned by the service.
56    type Error;
57
58    /// Asynchronously perform the request.
59    fn call(&self, req: R) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send;
60}
61
62impl<R, T> Service<R> for Arc<T>
63where
64    T: ?Sized + Service<R>,
65{
66    type Response = T::Response;
67    type Error = T::Error;
68
69    fn call(&self, req: R) -> impl Future<Output = Result<T::Response, T::Error>> {
70        (**self).call(req)
71    }
72}
73
74/// A factory of raw HTTP clients.
75pub trait BuildRawClient {
76    /// The raw client type.
77    type RawClient;
78
79    /// Creates a new raw client.
80    fn build_raw_client(
81        &self,
82        builder: &Builder<builder::Complete<Self>>,
83    ) -> Result<Self::RawClient, Error>
84    where
85        Self: Sized;
86}