Add endpoint to get information for an image

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 3 years ago
parent be8997976b
commit 96adeb3afe
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,5 +1,5 @@
use crate::error::ApiResult;
use crate::models::{Anime, Artist, SearchResponse, ThemeEntry};
use crate::models::{Anime, Artist, Image, SearchResponse, ThemeEntry};
use reqwest::Response;
use serde::Serialize;
use std::collections::HashMap;
@ -110,6 +110,20 @@ impl AnimeThemesClient {
Ok(response.remove("entry").unwrap())
}
/// Returns an image by id
pub async fn image(&self, id: u32, include: &[&str]) -> ApiResult<Image> {
let mut response: HashMap<String, Image> = self
.api_get(
format!("/image/{}", id).as_str(),
&[("include", include.join(","))],
)
.await?
.json()
.await?;
Ok(response.remove("image").unwrap())
}
/// Starts a get request to the API endpoint
async fn api_get<T: Serialize + ?Sized>(&self, path: &str, query: &T) -> ApiResult<Response> {
let response = self

@ -152,6 +152,8 @@ pub struct Image {
pub meta: EntryMetadata,
pub path: String,
pub facet: ImageFacet,
pub anime: Option<Vec<Anime>>,
pub artists: Option<Vec<Artist>>,
}
#[derive(Debug, Clone, Deserialize)]

@ -40,3 +40,11 @@ async fn it_returns_entries_by_id() {
assert!(result.videos.is_some())
}
#[tokio::test]
async fn it_returns_images_by_id() {
let client = AnimeThemesClient::default();
let result = client.image(7247, &["anime"]).await.unwrap();
assert!(result.anime.is_some())
}

Loading…
Cancel
Save