mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 00:38:54 +00:00
Construct rosu api
This commit is contained in:
parent
96ac66d84c
commit
2bc9412f86
3 changed files with 33 additions and 10 deletions
|
@ -53,7 +53,7 @@ impl TypeMapKey for OsuClient {
|
|||
/// - Commands on the "osu" prefix
|
||||
/// - Hooks. Hooks are completely opt-in.
|
||||
///
|
||||
pub fn setup(
|
||||
pub async fn setup(
|
||||
_path: &std::path::Path,
|
||||
data: &mut TypeMap,
|
||||
announcers: &mut AnnouncerHandler,
|
||||
|
@ -71,13 +71,23 @@ pub fn setup(
|
|||
|
||||
// API client
|
||||
let http_client = data.get::<HTTPClient>().unwrap().clone();
|
||||
let mk_osu_client = || {
|
||||
Arc::new(OsuHttpClient::new(
|
||||
let mk_osu_client = || async {
|
||||
Arc::new(
|
||||
OsuHttpClient::new(
|
||||
std::env::var("OSU_API_KEY").expect("Please set OSU_API_KEY as osu! api key."),
|
||||
http_client.clone(),
|
||||
))
|
||||
std::env::var("OSU_API_CLIENT_ID")
|
||||
.expect("Please set OSU_API_CLIENT_ID as osu! api v2 client ID.")
|
||||
.parse()
|
||||
.expect("client_id should be u64"),
|
||||
std::env::var("OSU_API_CLIENT_SECRET")
|
||||
.expect("Please set OSU_API_CLIENT_SECRET as osu! api v2 client secret."),
|
||||
)
|
||||
.await
|
||||
.expect("osu! should be initialized"),
|
||||
)
|
||||
};
|
||||
let osu_client = mk_osu_client();
|
||||
let osu_client = mk_osu_client().await;
|
||||
data.insert::<OsuClient>(osu_client.clone());
|
||||
data.insert::<oppai_cache::BeatmapCache>(oppai_cache::BeatmapCache::new(
|
||||
http_client.clone(),
|
||||
|
@ -89,7 +99,7 @@ pub fn setup(
|
|||
));
|
||||
|
||||
// Announcer
|
||||
let osu_client = mk_osu_client();
|
||||
let osu_client = mk_osu_client().await;
|
||||
announcers.add(
|
||||
announcer::ANNOUNCER_KEY,
|
||||
announcer::Announcer::new(osu_client),
|
||||
|
|
|
@ -19,6 +19,8 @@ const REQUESTS_PER_MINUTE: usize = 100;
|
|||
pub struct Client {
|
||||
client: Ratelimit<HTTPClient>,
|
||||
key: String,
|
||||
|
||||
rosu: rosu_v2::Osu,
|
||||
}
|
||||
|
||||
fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::Error> {
|
||||
|
@ -33,13 +35,23 @@ fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::
|
|||
|
||||
impl Client {
|
||||
/// Create a new client from the given API key.
|
||||
pub fn new(key: String, client: HTTPClient) -> Client {
|
||||
pub async fn new(
|
||||
key: String,
|
||||
client: HTTPClient,
|
||||
client_id: u64,
|
||||
client_secret: impl Into<String>,
|
||||
) -> Result<Client> {
|
||||
let client = Ratelimit::new(
|
||||
client,
|
||||
REQUESTS_PER_MINUTE,
|
||||
std::time::Duration::from_secs(60),
|
||||
);
|
||||
Client { client, key }
|
||||
let rosu = rosu_v2::OsuBuilder::new()
|
||||
.client_id(client_id)
|
||||
.client_secret(client_secret)
|
||||
.build()
|
||||
.await?;
|
||||
Ok(Client { client, key, rosu })
|
||||
}
|
||||
|
||||
pub(crate) async fn build_request(&self, url: &str) -> Result<reqwest::RequestBuilder> {
|
||||
|
|
|
@ -135,6 +135,7 @@ async fn main() {
|
|||
// osu!
|
||||
#[cfg(feature = "osu")]
|
||||
youmubot_osu::discord::setup(&db_path, &mut data, &mut announcers)
|
||||
.await
|
||||
.expect("osu! is initialized");
|
||||
// codeforces
|
||||
#[cfg(feature = "codeforces")]
|
||||
|
|
Loading…
Add table
Reference in a new issue