Crate twitch_api
source ·Expand description
Rust library for talking with the new Twitch API aka. "Helix", EventSub and more! Use Twitch endpoints fearlessly!
Examples
Get a channel
use twitch_api::helix::HelixClient;
use twitch_api::twitch_oauth2::{AccessToken, UserToken};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
// Create the HelixClient, which is used to make requests to the Twitch API
let client: HelixClient<reqwest::Client> = HelixClient::default();
// Create a UserToken, which is used to authenticate requests.
let token =
UserToken::from_token(&client, AccessToken::from("mytoken"))
.await?;
println!(
"Channel: {:?}",
client.get_channel_from_login("twitchdev", &token).await?
);
Ok(())
}
Get information about a channel with the Get Channel Information
helix endpoint.
use twitch_api::twitch_oauth2::{
tokens::errors::AppAccessTokenError, AppAccessToken, TwitchToken,
};
use twitch_api::{helix::channels::GetChannelInformationRequest, TwitchClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let client: TwitchClient<reqwest::Client> = TwitchClient::default();
let token = AppAccessToken::get_app_access_token(
&client,
"validclientid".into(),
"validclientsecret".into(),
vec![/* scopes */],
)
.await?;
let ids: &[&twitch_types::UserIdRef] = &["27620241".into()];
let req = GetChannelInformationRequest::broadcaster_ids(ids);
println!(
"{:?}",
&client.helix.req_get(req, &token).await?.data[0].title
);
Ok(())
}
There is also convenience functions, like accessing channel information with a specified login name
let client = TwitchClient::default();
println!("{:?}", &client.helix.get_channel_from_login("twitch", &token).await?.unwrap().title);
Features
This crate provides almost no functionality by default, only exposing types
. To enable more features, refer to below table.
Feature | |
---|---|
twitch_oauth2 | Gives scopes for endpoints and topics that are needed to call them. |
client | Gives a client abstraction for endpoints. See for example TmiClient and HelixClient |
helix | Enables Helix endpoints |
tmi | Enables TMI endpoints |
eventsub | Enables deserializable structs for EventSub |
pubsub | Enables deserializable structs for PubSub |
hmac | Enable message authentication using HMAC on EventSub |
time | Enable time utilities on Timestamp |
all | Enables all above features. Do not use this in production, it’s better if you specify exactly what you need |
ureq | Enables ureq for HttpClient . |
surf | Enables surf for HttpClient . Note that this does not enable any default client backend, if you get a compile error, specify surf in your Cargo.toml . By default, surf uses feature curl-client |
reqwest | Enables reqwest for HttpClient . Note that this does not enable any default TLS backend, if you get invalid URL, scheme is not http , specify reqwest in your Cargo.toml. By default, reqwest uses feature default-tls |
tower | Enables using tower services for HttpClient . |
beta | Enables beta endpoints, topics or features. Breakage may occur, semver compatibility not guaranteed. |
unsupported | Enables undocumented or experimental endpoints, including beta endpoints, topics or features. Breakage may occur, semver compatibility not guaranteed. |
trace_unknown_fields | Logs ignored fields as WARN log messages where applicable. Please consider using this and filing an issue or PR when a new field has been added to the endpoint but not added to this library. |
deny_unknown_fields | Adds #[serde(deny_unknown_fields)] on all applicable structs/enums. Please consider using this and filing an issue or PR when a new field has been added to the endpoint but not added to this library. |
deser_borrow | Makes fields on Deserialize -able structs borrow if they can be borrowed, this feature is enabled by default, but exists to enable using serde::de::DeserializeOwned or for<'de> serde::Deserialize<'de> by disabling this feature. |
Re-exports
pub use twitch_oauth2;
pub use client::Client as HttpClient;
pub use twitch_types as types;
Modules
- client
client
Different clients you can use with this crate to call endpoints. - eventsub
eventsub
Holds serializable EventSub stuff - Extra types not defined in
twitch_types
- helix
helix
Helix endpoints or the New Twitch API - pubsub
pubsub
Holds serializable pubsub stuff - tmi
tmi
TMI Endpoint, twitch’s unsupported api for better chatters retrieval
Structs
- HelixClient
client
andhelix
Client for Helix or the New Twitch API - Client for the twitch TMI endpoint, almost entirely undocumented and certainly not supported.
- TwitchClient
client
and (helix
ortmi
)Client for Twitch APIs.
Enums
- DeserError
serde_json
A deserialization error
Statics
- TWITCH_EVENTSUB_WEBSOCKET_URL
eventsub
Location to twitch Eventsub WebSocket - TWITCH_HELIX_URL
helix
Location of Twitch Helix - TWITCH_PUBSUB_URL
pubsub
Location to twitch PubSub - Location of Twitch TMI
Functions
- parse_json
serde_json
Parse a string asT
, logging ignored fields and giving a more detailed error message on parse errors - parse_json_value
serde_json
Parse a json Value asT
, logging ignored fields and giving a more detailed error message on parse errors