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<Vec<u8>>) -> futures::future::BoxFuture<'static, Result<http::Response<Vec<u8>>, ClientError>> {
    ...
}

To use that for requests we do the following.

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

    fn req(&'a self, request: Request) -> BoxedFuture<'a, Result<Response, Self::Error>> {
        Box::pin(async move {
            Ok(self
                .call(
                    request
                        // The `RequestExt` trait provides a convenience function to convert
                        // a `Request<impl Into<hyper::body::Body>>` into a `Request<Vec<u8>>`
                        .into_request_vec()
                        .await
                        .expect("a request given to the client should always be valid"),
                )
                .await?
                .map(|body| body.into()))
        })
    }
}
// And for full usage
use twitch_api2::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

A stream of Bytes, used when receiving bodies.

A cheaply cloneable and sliceable chunk of contiguous memory.

A client that will never work, used to trick documentation tests

Enums

A compability shim for ensuring an error can represent hyper::Error

Possible errors from Client::req() when using the surf client

Possible errors from Client::req() when using the ureq client

Statics

The User-Agent product of this crate.

Traits

A client that can do requests

A specific client default for setting some sane defaults for API calls and oauth2 usage

Extension trait for Request

Extension trait for Response

Functions

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

Type Definitions

A boxed future, mimics futures::future::BoxFuture

The request type we’re expecting with body.

The response type we’re expecting with body