From 960007b06e2b47d41f88365c26f043f61c817f08 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 23 Apr 2025 13:58:03 +0200 Subject: import: send user-agent header to all apis --- import/src/musicbrainz.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 import/src/musicbrainz.rs (limited to 'import/src/musicbrainz.rs') diff --git a/import/src/musicbrainz.rs b/import/src/musicbrainz.rs new file mode 100644 index 0000000..059b8f5 --- /dev/null +++ b/import/src/musicbrainz.rs @@ -0,0 +1,44 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2025 metamuffin +*/ + +use reqwest::{ + header::{HeaderMap, HeaderName, HeaderValue}, + Client, ClientBuilder, +}; +use std::sync::Arc; +use tokio::sync::Semaphore; +use crate::USER_AGENT; + +pub struct MusicBrainz { + client: Client, + key: String, + rate_limit: Arc, +} + +impl MusicBrainz { + pub fn new(api_key: &str) -> Self { + let client = ClientBuilder::new() + .default_headers(HeaderMap::from_iter([ + ( + HeaderName::from_static("accept"), + HeaderValue::from_static("application/json"), + ), + ( + HeaderName::from_static("user-agent"), + HeaderValue::from_static(USER_AGENT), + ), + ])) + .build() + .unwrap(); + Self { + client, + // send at most 1 req/s according to musicbrainz docs, each lock is held for 10s + // this implementation also never sends more than 10 requests in-flight. + rate_limit: Arc::new(Semaphore::new(10)), + key: api_key.to_owned(), + } + } +} -- cgit v1.2.3-70-g09d2