Module client

Source
Available on crate feature client only.
Expand description

Different clients you can use with this crate to call endpoints.

This enables you to use your own http client/implementation. For example, say you have a http client that has a “client” named foo::Client.

That client has a function call which looks something like this

fn call(&self, req: http::Request<Bytes>) -> futures::future::BoxFuture<'static, Result<http::Response<Bytes>, ClientError>> {
    ...
}

To use that for requests we do the following.

use twitch_api::client::{BoxedFuture, Request, Response};
mod foo {
    use twitch_api::client::{BoxedFuture, Bytes, Response};
    pub struct Client;
    impl Client {
        pub fn call(
            &self,
            req: http::Request<Bytes>,
        ) -> futures::future::BoxFuture<
            'static,
            Result<http::Response<Bytes>, ClientError>,
        > {
            unimplemented!()
        }
    }
    pub type ClientError = std::io::Error;
}
impl twitch_api::HttpClient for foo::Client {
    type Error = foo::ClientError;

    fn req(
        &self,
        request: Request,
    ) -> BoxedFuture<'_, Result<Response, Self::Error>> {
        Box::pin(async move { self.call(request).await })
    }
}
// And for full usage
use twitch_api::TwitchClient;
pub struct MyStruct {
    twitch: TwitchClient<'static, foo::Client>,
    token: twitch_oauth2::AppAccessToken,
}

If your client is from a remote crate, you can use the newtype pattern

Of course, sometimes the clients use different types for their responses and requests. but simply translate them into http types and it will work.

See the source of this module for the implementation of Client for surf and reqwest if you need inspiration.

Structs§

Bytes
A cheaply cloneable and sliceable chunk of contiguous memory.
DummyHttpClient
A client that will never work, used to trick documentation tests
TowerServicetower
A wrapped tower service

Enums§

CompatError
A compability shim for ensuring an error can represent hyper::Error
ReqwestClientDefaultErrorreqwest
Possible errors from ClientDefault::default_client_with_name for reqwest
SurfErrorsurf
Possible errors from Client::req() when using the surf client
TowerErrortower
Errors that can occur when using a TowerService
UreqErrorureq
Possible errors from Client::req() when using the ureq client

Statics§

TWITCH_API_USER_AGENT
The User-Agent product of this crate.

Traits§

Client
A client that can do requests
ClientDefault
A specific client default for setting some sane defaults for API calls and oauth2 usage
ResponseExt
Extension trait for Response

Functions§

user_agent
Gives the User-Agent header value for a client annotated with an added twitch_api product

Type Aliases§

BoxedFuture
A boxed future, mimics futures::future::BoxFuture
Request
The request type we’re expecting with body.
Response
The response type we’re expecting with body