flix_tmdb/api/
episodes.rs

1//! Episodes API
2
3use core::time::Duration;
4use std::rc::Rc;
5
6use flix_model::numbers::{EpisodeNumber, SeasonNumber};
7
8use governor::Jitter;
9
10use crate::Config;
11use crate::model::Episode;
12use crate::model::id::ShowId;
13
14use super::{Error, make_request};
15
16/// TMDB Episodes API client
17pub struct Client {
18	config: Rc<Config>,
19}
20
21impl Client {
22	/// Create a new client with the given configuration
23	pub fn new(config: Rc<Config>) -> Self {
24		Self { config }
25	}
26}
27
28impl Client {
29	/// Fetch the details of the episode refered to by ID, season number and episode number
30	pub async fn get_details(
31		&self,
32		id: impl Into<ShowId>,
33		season: impl Into<SeasonNumber>,
34		episode: impl Into<EpisodeNumber>,
35		language: Option<&str>,
36	) -> Result<Episode, Error> {
37		self.config
38			.limiter
39			.until_ready_with_jitter(Jitter::new(
40				Duration::from_millis(0),
41				Duration::from_millis(50),
42			))
43			.await;
44
45		Ok(self
46			.config
47			.client
48			.execute(make_request(
49				&self.config,
50				&format!(
51					"/3/tv/{}/season/{}/episode/{}",
52					id.into().into_raw(),
53					season.into(),
54					episode.into()
55				),
56				language,
57			)?)
58			.await?
59			.error_for_status()?
60			.json()
61			.await?)
62	}
63}