You've already forked flix
Update dependencies and lints
This commit is contained in:
+49
-35
@@ -1,6 +1,5 @@
|
||||
//! TMDB API clients
|
||||
//! TMDB API clients.
|
||||
|
||||
use core::ops::Deref;
|
||||
use core::time::Duration;
|
||||
use std::sync::RwLock;
|
||||
|
||||
@@ -17,20 +16,26 @@ pub mod movies;
|
||||
pub mod seasons;
|
||||
pub mod shows;
|
||||
|
||||
/// A generic error wrapping Url and Reqwest errors
|
||||
/// A generic error wrapping Url and Reqwest errors.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[expect(clippy::error_impl_error, reason = "Error is a good name here")]
|
||||
#[expect(clippy::exhaustive_enums, reason = "unlikely to add new variants")]
|
||||
pub enum Error {
|
||||
/// Url error wrapper
|
||||
/// Url error wrapper.
|
||||
#[error("url parse error: {0}")]
|
||||
Url(#[from] url::ParseError),
|
||||
/// Reqwest error wrapper
|
||||
/// Reqwest error wrapper.
|
||||
#[error("reqwest error: {0}")]
|
||||
Reqwest(#[from] reqwest::Error),
|
||||
/// Json error wrapper
|
||||
/// Json error wrapper.
|
||||
#[error("json error: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
/// Turn a [Config] and `path` into a [Request].
|
||||
///
|
||||
/// # Errors
|
||||
/// See [Error] for failure modes.
|
||||
fn make_request(config: &Config, path: &str, language: Option<&str>) -> Result<Request, Error> {
|
||||
let url = config.base_url.join(path)?;
|
||||
|
||||
@@ -38,7 +43,7 @@ fn make_request(config: &Config, path: &str, language: Option<&str>) -> Result<R
|
||||
header::AUTHORIZATION,
|
||||
format!("Bearer {}", config.bearer_token),
|
||||
);
|
||||
if let Some(ref user_agent) = config.user_agent {
|
||||
if let Some(user_agent) = &config.user_agent {
|
||||
builder = builder.header(header::USER_AGENT, user_agent);
|
||||
}
|
||||
if let Some(language) = language {
|
||||
@@ -48,22 +53,22 @@ fn make_request(config: &Config, path: &str, language: Option<&str>) -> Result<R
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
|
||||
/// Execute a [Request] and deserialize the response.
|
||||
///
|
||||
/// # Errors
|
||||
/// See [Error] for failure modes.
|
||||
async fn exec_request<T: DeserializeOwned>(
|
||||
config: &Config,
|
||||
cache: &dyn Cache,
|
||||
policy: &RwLock<CachePolicy>,
|
||||
request: Request,
|
||||
) -> Result<T, Error> {
|
||||
let (read_cache, write_cache) = if let Ok(guard) = policy.read() {
|
||||
match guard.deref() {
|
||||
CachePolicy::None => (None, None),
|
||||
CachePolicy::Full => (Some(cache), Some(cache)),
|
||||
CachePolicy::Read => (Some(cache), None),
|
||||
CachePolicy::Update => (None, Some(cache)),
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
let (read_cache, write_cache) = policy.read().map_or((None, None), |guard| match *guard {
|
||||
CachePolicy::None => (None, None),
|
||||
CachePolicy::Full => (Some(cache), Some(cache)),
|
||||
CachePolicy::Read => (Some(cache), None),
|
||||
CachePolicy::Update => (None, Some(cache)),
|
||||
});
|
||||
|
||||
let path = request.url().path().to_owned();
|
||||
|
||||
@@ -73,24 +78,23 @@ async fn exec_request<T: DeserializeOwned>(
|
||||
response = cache.get(&path);
|
||||
}
|
||||
let needs_cache_write = response.is_none();
|
||||
let response = match response {
|
||||
Some(response) => response,
|
||||
None => {
|
||||
config
|
||||
.limiter
|
||||
.until_ready_with_jitter(Jitter::new(
|
||||
Duration::from_millis(0),
|
||||
Duration::from_millis(50),
|
||||
))
|
||||
.await;
|
||||
config
|
||||
.client
|
||||
.execute(request)
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.bytes()
|
||||
.await?
|
||||
}
|
||||
let response = if let Some(response) = response {
|
||||
response
|
||||
} else {
|
||||
config
|
||||
.limiter
|
||||
.until_ready_with_jitter(Jitter::new(
|
||||
Duration::from_millis(0),
|
||||
Duration::from_millis(50),
|
||||
))
|
||||
.await;
|
||||
config
|
||||
.client
|
||||
.execute(request)
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.bytes()
|
||||
.await?
|
||||
};
|
||||
|
||||
// write to the cache if needed
|
||||
@@ -102,3 +106,13 @@ async fn exec_request<T: DeserializeOwned>(
|
||||
|
||||
Ok(serde_json::from_slice(&response)?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Error;
|
||||
|
||||
#[test]
|
||||
fn use_types() {
|
||||
drop(Error::Url(url::ParseError::Overflow));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user