hoyo_rs/
lib.rs

1// #![deny(missing_docs)]
2#![allow(warnings)]
3
4//! # hoyo-rs
5//! API wrapper for Genshin Impact & Honkai Star Rail.
6
7/// Components for model interaction.
8pub mod components;
9use std::sync::Arc;
10
11/// Response models.
12pub mod models;
13pub use models::*;
14
15/// Error definition;
16pub mod error;
17pub use error::*;
18
19mod utils;
20
21mod config;
22
23mod client;
24use client::Client;
25use components::auth::AuthClient;
26use config::Config;
27use reqwest_cookie_store::{CookieStore, CookieStoreMutex};
28use utils::common::Region;
29
30/// Client to use Hoyolab API
31#[derive(Clone)]
32pub struct Hoyo {
33    config: config::Config,
34    client: Client,
35    cookie_store: Arc<CookieStoreMutex>,
36}
37
38impl Hoyo {
39    /// Creates new HoyoApi instance.
40    pub fn new() -> Self {
41        Self::from_config(Config::default())
42    }
43
44    /// Creates new HoyoApi instance from config.
45    pub fn from_config(config: Config) -> Self {
46        let cookie_store = Arc::new(CookieStoreMutex::new(CookieStore::new(None)));
47        let client = Client::builder()
48            .cookie_provider(cookie_store.clone())
49            .build()
50            .expect("failed to build Request client");
51        Self {
52            config,
53            client,
54            cookie_store,
55        }
56    }
57
58    /// Creates an authentication client
59    pub fn authclient(&self, region: Region) -> AuthClient {
60        AuthClient::new(self.client.clone(), region, self.cookie_store.clone())
61    }
62}