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§
- Body
- A stream of
Bytes
, used when receiving bodies. - Bytes
- A cheaply cloneable and sliceable chunk of contiguous memory.
- Dummy
Http Client - A client that will never work, used to trick documentation tests
Enums§
- Compat
Error - A compability shim for ensuring an error can represent
hyper::Error
- Reqwest
Client Default Error - Possible errors from
ClientDefault::default_client_with_name
for reqwest - Surf
Error - Possible errors from
Client::req()
when using the surf client - Ureq
Error - Possible errors from
Client::req()
when using the ureq client
Statics§
- TWITCH_
API2_ USER_ AGENT - The User-Agent
product
of this crate.
Traits§
- Client
- A client that can do requests
- Client
Default - A specific client default for setting some sane defaults for API calls and oauth2 usage
- Request
Ext - Extension trait for
Request
- Response
Ext - Extension trait for
Response
Functions§
- user_
agent - Gives the User-Agent header value for a client annotated with an added
twitch_api2
product
Type Aliases§
- Boxed
Future - 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