Skip to content

Commit

Permalink
feat(client): add support for optional ID in variables
Browse files Browse the repository at this point in the history
Added logic to handle optional ID in the variables map. If an ID is
provided, it will be included in the map as "id". If no ID is provided,
the map will include "mal_id" instead.
  • Loading branch information
AndrielFR committed Jan 2, 2025
1 parent da38697 commit a389643
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
4 changes: 2 additions & 2 deletions queries/get_anime.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 Andriel Ferreira <https://github.com/AndrielFR>

query ($id: Int, $id_mal: Int) {
Media (id: $id, idMal: $id_mal, type: ANIME) {
query ($id: Int, $mal_id: Int) {
Media (id: $id, idMal: $mal_id, type: ANIME) {
id
idMal
title {
Expand Down
4 changes: 2 additions & 2 deletions queries/get_manga.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 Andriel Ferreira <https://github.com/AndrielFR>

query ($id: Int, $id_mal: Int) {
Media (id: $id, idMal: $id_mal, type: MANGA) {
query ($id: Int, $mal_id: Int) {
Media (id: $id, idMal: $mal_id, type: MANGA) {
id
idMal
title {
Expand Down
60 changes: 58 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,46 @@ impl Default for Client {
}

impl Client {
/// Set the API token.
pub fn api_token(mut self, token: &str) -> Self {
self.api_token = Some(token.to_string());
self
}

/// Set the timeout for the requests (in seconds).
pub fn timeout(mut self, seconds: u64) -> Self {
self.timeout = seconds;
self
}

pub async fn get_anime(&self, variables: serde_json::Value) -> Result<crate::models::Anime> {
/// Get an anime by its ID or MAL ID.
///
/// # Arguments
///
/// * `id` - The ID of the anime.
/// * `mal_id` - The MAL ID of the anime.
///
/// # Errors
///
/// Returns an error if the request fails.
///
/// # Example
///
/// ```no_run
/// use anilist::Client;
///
/// let client = Client::default();
/// let anime = client.get_anime(Some(1), None).await.unwrap();
/// ```
pub async fn get_anime(
&self,
id: Option<i64>,
mal_id: Option<i64>,
) -> Result<crate::models::Anime> {
let variables = match id {
Some(id) => serde_json::json!({ "id": id }),
None => serde_json::json!({ "mal_id": mal_id }),
};
let data = self.request("anime", "get", variables).await.unwrap();

match serde_json::from_str::<Anime>(&data["data"]["Media"].to_string()) {
Expand All @@ -45,7 +74,34 @@ impl Client {
}
}

pub async fn get_manga(&self, variables: serde_json::Value) -> Result<crate::models::Manga> {
/// Get a manga by its ID or MAL ID.
///
/// # Arguments
///
/// * `id` - The ID of the manga.
/// * `mal_id` - The MAL ID of the manga.
///
/// # Errors
///
/// Returns an error if the request fails.
///
/// # Example
///
/// ```no_run
/// use anilist::Client;
///
/// let client = Client::default();
/// let manga = client.get_manga(Some(1), None).await.unwrap();
/// ```
pub async fn get_manga(
&self,
id: Option<i64>,
mal_id: Option<i64>,
) -> Result<crate::models::Manga> {
let variables = match id {
Some(id) => serde_json::json!({ "id": id }),
None => serde_json::json!({ "mal_id": mal_id }),
};
let data = self.request("manga", "get", variables).await.unwrap();

match serde_json::from_str::<Manga>(&data["data"]["Media"].to_string()) {
Expand Down
2 changes: 1 addition & 1 deletion src/models/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Anime {
pub async fn load_full(self) -> Result<Self> {
if !self.is_full_loaded {
let mut anime = Client::default()
.get_anime(serde_json::json!({"id": self.id}))
.get_anime(Some(self.id), self.id_mal)
.await
.unwrap();
anime.is_full_loaded = true;
Expand Down
2 changes: 1 addition & 1 deletion src/models/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Manga {
pub async fn load_full(self) -> Result<Self> {
if !self.is_full_loaded {
let mut manga = Client::default()
.get_manga(serde_json::json!({"id": self.id}))
.get_manga(Some(self.id), self.id_mal)
.await
.unwrap();
manga.is_full_loaded = true;
Expand Down

0 comments on commit a389643

Please sign in to comment.