1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 <metamuffin.org>
*/
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<Semaphore>,
}
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(),
}
}
}
|