#![cfg_attr(docsrs, feature(doc_cfg))]
#[macro_use]
mod macros;
pub mod request;
pub mod types;
mod error;
pub use error::Error;
use std::sync::LazyLock;
use asknothingx2_util::{
api::{preset, HeaderMut},
oauth::{AccessToken, ClientId},
};
use reqwest::{header::HeaderMap, Client};
use url::Url;
const TWITCH_API_BASE: &str = "https://api.twitch.tv/helix";
static BASE_URL: LazyLock<Url> = LazyLock::new(|| url::Url::parse(TWITCH_API_BASE).unwrap());
#[derive(Debug, Clone)]
pub struct TwitchAPI {
access_token: AccessToken,
client_id: ClientId,
url: Url,
client: Client,
}
impl TwitchAPI {
pub fn new(access_token: AccessToken, client_id: ClientId) -> Self {
Self {
access_token,
client_id,
url: BASE_URL.clone(),
client: preset::rest_api("twitch-highway/1.0")
.build_client()
.unwrap(),
}
}
pub fn with_client(access_token: AccessToken, client_id: ClientId, client: Client) -> Self {
Self {
access_token,
client_id,
url: BASE_URL.clone(),
client,
}
}
pub fn with_url(access_token: AccessToken, client_id: ClientId, url: Url) -> Self {
Self {
access_token,
client_id,
url,
client: preset::rest_api("twitch-highway/1.0")
.build_client()
.unwrap(),
}
}
pub fn set_access_token(mut self, access_token: AccessToken) -> Self {
self.access_token = access_token;
self
}
pub fn set_client_id(mut self, client_id: ClientId) -> Self {
self.client_id = client_id;
self
}
pub fn set_url(mut self, url: Url) -> Self {
self.url = url;
self
}
pub fn set_client(mut self, client: Client) -> Self {
self.client = client;
self
}
pub fn access_token(&self) -> &AccessToken {
&self.access_token
}
pub fn client_id(&self) -> &ClientId {
&self.client_id
}
pub(crate) fn default_headers(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
HeaderMut::new(&mut headers)
.bearer_token(self.access_token.secret())
.client_id(&self.client_id)
.unwrap();
headers
}
#[cfg(any(
feature = "ads",
feature = "channel-points",
feature = "channels",
feature = "chat",
feature = "conduits",
feature = "extensions",
feature = "entitlements",
feature = "eventsub",
feature = "moderation",
feature = "polls",
feature = "predictions",
feature = "schedule",
feature = "streams",
feature = "users",
feature = "whisper",
))]
pub(crate) fn header_json(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
HeaderMut::new(&mut headers)
.bearer_token(self.access_token.secret())
.client_id(&self.client_id)
.unwrap()
.content_type_json();
headers
}
#[cfg(feature = "extensions")]
pub(crate) fn build_jwt_headers(&self, jwt: &crate::types::JWTToken) -> HeaderMap {
let mut headers = HeaderMap::new();
HeaderMut::new(&mut headers).bearer_token(jwt.as_str());
headers
}
pub(crate) fn build_url(&self) -> Url {
self.url.clone()
}
}
#[cfg(feature = "ads")]
pub mod ads;
#[cfg(feature = "analytics")]
pub mod analytics;
#[cfg(feature = "bits")]
pub mod bits;
#[cfg(feature = "ccls")]
pub mod ccls;
#[cfg(feature = "channel-points")]
pub mod channel_points;
#[cfg(feature = "channels")]
pub mod channels;
#[cfg(feature = "charity")]
pub mod charity;
#[cfg(feature = "chat")]
pub mod chat;
#[cfg(feature = "clips")]
pub mod clips;
#[cfg(feature = "conduits")]
pub mod conduits;
#[cfg(feature = "entitlements")]
pub mod entitlements;
#[cfg(feature = "eventsub")]
pub mod eventsub;
#[cfg(feature = "extensions")]
pub mod extensions;
#[cfg(feature = "games")]
pub mod games;
#[cfg(feature = "goals")]
pub mod goals;
#[cfg(feature = "guest-star")]
pub mod guest_star;
#[cfg(feature = "hype-train")]
pub mod hype_train;
#[cfg(feature = "moderation")]
pub mod moderation;
#[cfg(feature = "polls")]
pub mod polls;
#[cfg(feature = "predictions")]
pub mod predictions;
#[cfg(feature = "raid")]
pub mod raid;
#[cfg(feature = "schedule")]
pub mod schedule;
#[cfg(feature = "search")]
pub mod search;
#[cfg(feature = "streams")]
pub mod streams;
#[cfg(feature = "subscriptions")]
pub mod subscriptions;
#[cfg(feature = "teams")]
pub mod teams;
#[cfg(feature = "users")]
pub mod users;
#[cfg(feature = "videos")]
pub mod videos;
#[cfg(feature = "whisper")]
pub mod whisper;
#[cfg(any(
feature = "analytics",
feature = "bits",
feature = "eventsub",
feature = "moderation"
))]
mod serde_helpers;