#![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().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().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
}
#[allow(unused)]
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
}
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
}
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
}
#[allow(unused)]
pub(crate) fn build_url(&self) -> Url {
self.url.clone()
}
}
pub mod ads;
pub mod analytics;
pub mod bits;
pub mod ccls;
pub mod channel_points;
pub mod channels;
pub mod charity;
pub mod chat;
pub mod clips;
pub mod conduits;
pub mod entitlements;
pub mod eventsub;
pub mod extensions;
pub mod games;
pub mod goals;
pub mod guest_star;
pub mod hype_train;
pub mod moderation;
pub mod polls;
pub mod predictions;
pub mod raid;
pub mod schedule;
pub mod search;
pub mod streams;
pub mod subscriptions;
pub mod teams;
pub mod users;
pub mod videos;
pub mod whisper;
mod serde_helpers;