refactor avatar_fetch out
This commit is contained in:
parent
f94f1baba5
commit
7c1bd99be3
2 changed files with 65 additions and 61 deletions
63
src/avatar_fetch.rs
Normal file
63
src/avatar_fetch.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
use twitch_api::TwitchClient;
|
||||||
|
use twitch_oauth2::AppAccessToken;
|
||||||
|
use std::error::Error;
|
||||||
|
use twitch_api::helix::users;
|
||||||
|
use crate::config;
|
||||||
|
use crate::user_data::UserData;
|
||||||
|
|
||||||
|
pub struct AvatarFetch {
|
||||||
|
client: TwitchClient<'static, reqwest::Client>,
|
||||||
|
token: AppAccessToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AvatarFetch {
|
||||||
|
pub fn new(client: TwitchClient<'static, reqwest::Client>, token: AppAccessToken) -> Self {
|
||||||
|
AvatarFetch {
|
||||||
|
client,
|
||||||
|
token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect(config: &config::TwitchConfig) -> Result<Self, Box<dyn Error>> {
|
||||||
|
let client: TwitchClient<reqwest::Client> = TwitchClient::new();
|
||||||
|
let token = AppAccessToken::get_app_access_token(
|
||||||
|
&client,
|
||||||
|
config.client_id().into(),
|
||||||
|
config.client_secret().into(),
|
||||||
|
vec![],
|
||||||
|
).await?;
|
||||||
|
Ok(Self::new(client, token))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_by_id(&self, id: &str) -> Result<Option<UserData>, Box<dyn Error>> {
|
||||||
|
let mut request = users::GetUsersRequest::new();
|
||||||
|
let ids: &[&twitch_types::UserIdRef] = &[id.into()];
|
||||||
|
request.id = ids.into();
|
||||||
|
let response: Vec<users::User> = self.client.helix.req_get(request, &self.token).await?.data;
|
||||||
|
|
||||||
|
Ok(self.user_from_response(response))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn user_from_response(&self, response: Vec<users::User>) -> Option<UserData> {
|
||||||
|
if let Some(user) = response.first() {
|
||||||
|
dbg!(user);
|
||||||
|
Some(UserData::new(
|
||||||
|
user.login.to_string(),
|
||||||
|
user.display_name.to_string(),
|
||||||
|
user.id.to_string(),
|
||||||
|
user.profile_image_url.clone().unwrap_or("".to_string()),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_by_name(&self, name: &str) -> Result<Option<UserData>, Box<dyn Error>> {
|
||||||
|
let mut request = users::GetUsersRequest::new();
|
||||||
|
let logins: &[&twitch_types::UserNameRef] = &[name.into()];
|
||||||
|
request.login = logins.into();
|
||||||
|
let response: Vec<users::User> = self.client.helix.req_get(request, &self.token).await?.data;
|
||||||
|
|
||||||
|
Ok(self.user_from_response(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/main.rs
63
src/main.rs
|
|
@ -1,16 +1,14 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use twitch_api::helix::users::get_users;
|
use avatar_fetch::AvatarFetch;
|
||||||
use twitch_api::twitch_oauth2::AppAccessToken;
|
|
||||||
use twitch_api::TwitchClient;
|
|
||||||
|
|
||||||
use crate::avatar_cache::AvatarCache;
|
use crate::avatar_cache::AvatarCache;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::user_data::UserData;
|
|
||||||
|
|
||||||
mod user_data;
|
mod user_data;
|
||||||
mod avatar_cache;
|
mod avatar_cache;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod avatar_fetch;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
@ -36,60 +34,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AvatarFetch {
|
|
||||||
client: TwitchClient<'static, reqwest::Client>,
|
|
||||||
token: AppAccessToken,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AvatarFetch {
|
|
||||||
fn new(client: TwitchClient<'static, reqwest::Client>, token: AppAccessToken) -> Self {
|
|
||||||
AvatarFetch {
|
|
||||||
client,
|
|
||||||
token,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn connect(config: &config::TwitchConfig) -> Result<Self, Box<dyn Error>> {
|
|
||||||
let client: TwitchClient<reqwest::Client> = TwitchClient::new();
|
|
||||||
let token = AppAccessToken::get_app_access_token(
|
|
||||||
&client,
|
|
||||||
config.client_id().into(),
|
|
||||||
config.client_secret().into(),
|
|
||||||
vec![],
|
|
||||||
).await?;
|
|
||||||
Ok(Self::new(client, token))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_user_by_id(&self, id: &str) -> Result<Option<UserData>, Box<dyn Error>> {
|
|
||||||
let mut request = get_users::GetUsersRequest::new();
|
|
||||||
let ids: &[&twitch_types::UserIdRef] = &[id.into()];
|
|
||||||
request.id = ids.into();
|
|
||||||
let response: Vec<get_users::User> = self.client.helix.req_get(request, &self.token).await?.data;
|
|
||||||
|
|
||||||
Ok(self.user_from_response(response))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn user_from_response(&self, response: Vec<get_users::User>) -> Option<UserData> {
|
|
||||||
if let Some(user) = response.first() {
|
|
||||||
dbg!(user);
|
|
||||||
Some(UserData::new(
|
|
||||||
user.login.to_string(),
|
|
||||||
user.display_name.to_string(),
|
|
||||||
user.id.to_string(),
|
|
||||||
user.profile_image_url.clone().unwrap_or("".to_string()),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_user_by_name(&self, name: &str) -> Result<Option<UserData>, Box<dyn Error>> {
|
|
||||||
let mut request = get_users::GetUsersRequest::new();
|
|
||||||
let logins: &[&twitch_types::UserNameRef] = &[name.into()];
|
|
||||||
request.login = logins.into();
|
|
||||||
let response: Vec<get_users::User> = self.client.helix.req_get(request, &self.token).await?.data;
|
|
||||||
|
|
||||||
Ok(self.user_from_response(response))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue