Merge pull request #6 from InputUsername/feature/alternative-hosts
Add support for alternative Listenbrainz hosts
This commit is contained in:
commit
8b195d1651
|
@ -23,6 +23,7 @@ const API_ROOT_URL: &str = "https://api.listenbrainz.org/1/";
|
||||||
/// - [`Error::Http`]: there was some other HTTP error while interacting with the API.
|
/// - [`Error::Http`]: there was some other HTTP error while interacting with the API.
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
agent: Agent,
|
agent: Agent,
|
||||||
|
api_root_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
|
@ -30,13 +31,21 @@ impl Client {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
agent: ureq::agent(),
|
agent: ureq::agent(),
|
||||||
|
api_root_url: API_ROOT_URL.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_url(url: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
agent: ureq::agent(),
|
||||||
|
api_root_url: url.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to perform a GET request against an endpoint
|
/// Helper method to perform a GET request against an endpoint
|
||||||
/// without any query parameters.
|
/// without any query parameters.
|
||||||
fn get<R: ResponseType>(&self, endpoint: Endpoint) -> Result<R, Error> {
|
fn get<R: ResponseType>(&self, endpoint: Endpoint) -> Result<R, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
|
let endpoint = format!("{}{}", self.api_root_url, endpoint);
|
||||||
|
|
||||||
let response = self.agent.get(&endpoint).call()?;
|
let response = self.agent.get(&endpoint).call()?;
|
||||||
|
|
||||||
|
@ -52,7 +61,7 @@ impl Client {
|
||||||
offset: Option<u64>,
|
offset: Option<u64>,
|
||||||
range: Option<&str>,
|
range: Option<&str>,
|
||||||
) -> Result<Option<R>, Error> {
|
) -> Result<Option<R>, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
|
let endpoint = format!("{}{}", self.api_root_url, endpoint);
|
||||||
|
|
||||||
let mut request = self.agent.get(&endpoint);
|
let mut request = self.agent.get(&endpoint);
|
||||||
|
|
||||||
|
@ -85,7 +94,7 @@ impl Client {
|
||||||
{
|
{
|
||||||
let data = serde_json::to_value(data).map_err(Error::RequestJson)?;
|
let data = serde_json::to_value(data).map_err(Error::RequestJson)?;
|
||||||
|
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
|
let endpoint = format!("{}{}", self.api_root_url, endpoint);
|
||||||
|
|
||||||
let response = self.agent
|
let response = self.agent
|
||||||
.post(&endpoint)
|
.post(&endpoint)
|
||||||
|
@ -106,7 +115,7 @@ impl Client {
|
||||||
|
|
||||||
/// Endpoint: [`validate-token`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-validate-token)
|
/// Endpoint: [`validate-token`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-validate-token)
|
||||||
pub fn validate_token(&self, token: &str) -> Result<ValidateTokenResponse, Error> {
|
pub fn validate_token(&self, token: &str) -> Result<ValidateTokenResponse, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::ValidateToken);
|
let endpoint = format!("{}{}", self.api_root_url, Endpoint::ValidateToken);
|
||||||
|
|
||||||
let response = self.agent
|
let response = self.agent
|
||||||
.get(&endpoint)
|
.get(&endpoint)
|
||||||
|
@ -152,7 +161,7 @@ impl Client {
|
||||||
count: Option<u64>,
|
count: Option<u64>,
|
||||||
time_range: Option<u64>,
|
time_range: Option<u64>,
|
||||||
) -> Result<UserListensResponse, Error> {
|
) -> Result<UserListensResponse, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::UserListens(user_name));
|
let endpoint = format!("{}{}", self.api_root_url, Endpoint::UserListens(user_name));
|
||||||
|
|
||||||
let mut request = self.agent.get(&endpoint);
|
let mut request = self.agent.get(&endpoint);
|
||||||
|
|
||||||
|
@ -176,7 +185,7 @@ impl Client {
|
||||||
|
|
||||||
/// Endpoint: [`latest-import`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-latest-import) (`GET`)
|
/// Endpoint: [`latest-import`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-latest-import) (`GET`)
|
||||||
pub fn get_latest_import(&self, user_name: &str) -> Result<GetLatestImportResponse, Error> {
|
pub fn get_latest_import(&self, user_name: &str) -> Result<GetLatestImportResponse, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::LatestImport);
|
let endpoint = format!("{}{}", self.api_root_url, Endpoint::LatestImport);
|
||||||
|
|
||||||
self.agent
|
self.agent
|
||||||
.get(&endpoint)
|
.get(&endpoint)
|
||||||
|
@ -331,7 +340,7 @@ impl Client {
|
||||||
&self,
|
&self,
|
||||||
id: Option<i64>,
|
id: Option<i64>,
|
||||||
) -> Result<StatusGetDumpInfoResponse, Error> {
|
) -> Result<StatusGetDumpInfoResponse, Error> {
|
||||||
let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::StatusGetDumpInfo);
|
let endpoint = format!("{}{}", self.api_root_url, Endpoint::StatusGetDumpInfo);
|
||||||
|
|
||||||
let mut request = self.agent.get(&endpoint);
|
let mut request = self.agent.get(&endpoint);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue