From a2924614e0c28006f5be3980c1c2deea3cb1e236 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 3 Jul 2022 16:32:31 +0200 Subject: [PATCH] Fix includes Signed-off-by: trivernis --- Cargo.lock | 2 +- Cargo.toml | 4 +- src/client.rs | 61 ++++++----- src/includes.rs | 218 +++++++++++++++++++++++++++++++++++++-- src/lib.rs | 3 +- src/tests/test_client.rs | 58 ++++++++--- 6 files changed, 294 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e935a6d..a62b80c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "animethemes-rs" -version = "0.3.0" +version = "0.4.0" dependencies = [ "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 3d723f3..14fe3e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "animethemes-rs" -version = "0.3.0" +version = "0.4.0" authors = ["trivernis "] -edition = "2018" +edition = "2021" readme = "README.md" repository = "https://github.com/trivernis/animethemes-rs" license = "Apache-2.0" diff --git a/src/client.rs b/src/client.rs index 4252e14..204fd85 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ use crate::error::ApiResult; +use crate::includes::*; use crate::models::{ Anime, AnimeSynonym, Artist, Image, Resource, SearchResponse, Series, Song, Theme, ThemeEntry, Video, @@ -15,7 +16,7 @@ pub static DEFAULT_VIDEO_ENDPOINT: &str = "https://animethemes.moe/video/"; #[derive(Clone, Debug)] pub struct AnimeThemesClient { api_endpoint: String, - video_endpoint: String, + pub video_endpoint: String, client: reqwest::Client, } @@ -47,10 +48,11 @@ impl AnimeThemesClient { /// ``` /// # use animethemes_rs::error::ApiResult; /// use animethemes_rs::client::AnimeThemesClient; + /// use animethemes_rs::includes::SearchIncludes; /// /// # async fn a() -> ApiResult<()> { /// let client = AnimeThemesClient::default(); - /// let response = client.search("Attack on Titan", &[], &[]).await?; + /// let response = client.search("Attack on Titan", &[], SearchIncludes::default()).await?; /// /// assert!(response.anime.is_some()); /// assert!(response.songs.is_some()); @@ -60,12 +62,13 @@ impl AnimeThemesClient { &self, query: &str, fields: &[&str], - include: &[&str], + include: SearchIncludes, ) -> ApiResult { - let mut query = vec![("q", query.to_string()), ("include", include.join(","))]; + let mut query = vec![("q".to_string(), query.to_string())]; + query.append(&mut include.indo_includes()); if !fields.is_empty() { - query.push(("fields[search]", fields.join(","))); + query.push(("fields[search]".to_string(), fields.join(","))); } let mut response: HashMap = self.api_get("/search", &query[..]).await?.json().await?; @@ -74,56 +77,62 @@ impl AnimeThemesClient { } /// Returns an anime by a given slug string - pub async fn anime(&self, slug: &str, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("anime", slug, include).await + pub async fn anime(&self, slug: &str, include: AnimeInclude) -> ApiResult { + self.entry_by_id_with_include("anime", slug, include.includes()) + .await } /// Returns an artist by a given slug string - pub async fn artist(&self, slug: &str, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("artist", slug, include).await + pub async fn artist(&self, slug: &str, include: ArtistInclude) -> ApiResult { + self.entry_by_id_with_include("artist", slug, include.includes()) + .await } /// Returns an entry by a given id - pub async fn entry(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("animethemeentry", id, include) + pub async fn entry(&self, id: u32, include: ThemeEntryInclude) -> ApiResult { + self.entry_by_id_with_include("animethemeentry", id, include.includes()) .await } /// Returns an image by id - pub async fn image(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("image", id, include).await + pub async fn image(&self, id: u32, include: ImageInclude) -> ApiResult { + self.entry_by_id_with_include("image", id, include.includes()) + .await } /// Returns a resource by id - pub async fn resource(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("resource", id, include).await + pub async fn resource(&self, id: u32, include: ResourceInclude) -> ApiResult { + self.entry_by_id_with_include("resource", id, include.includes()) + .await } /// Returns a series by slug - pub async fn series(&self, slug: &str, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("series", slug, include).await + pub async fn series(&self, slug: &str, include: SeriesInclude) -> ApiResult { + self.entry_by_id_with_include("series", slug, include.includes()) + .await } /// Returns a song by id - pub async fn song(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("song", id, include).await + pub async fn song(&self, id: u32, include: SongInclude) -> ApiResult { + self.entry_by_id_with_include("song", id, include.includes()) + .await } /// Returns a synonym by id - pub async fn synonym(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("animesynonym", id, include) + pub async fn synonym(&self, id: u32, include: SynonymInclude) -> ApiResult { + self.entry_by_id_with_include("animesynonym", id, include.includes()) .await } /// Returns a theme by id - pub async fn theme(&self, id: u32, include: &[&str]) -> ApiResult { - self.entry_by_id_with_include("animetheme", id, include) + pub async fn theme(&self, id: u32, include: ThemeInclude) -> ApiResult { + self.entry_by_id_with_include("animetheme", id, include.includes()) .await } /// Returns a video by basename - pub async fn video(&self, basename: &str, include: &[&str]) -> ApiResult