Skip to content

Commit

Permalink
chore: remove mal_id var
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrielFR committed Jan 2, 2025
1 parent 25629f8 commit b0c98d7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 41 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-2025 Andriel Ferreira <https://github.com/AndrielFR>

query ($id: Int, $mal_id: Int) {
Media (id: $id, idMal: $mal_id, type: ANIME) {
query ($id: Int) {
Media (id: $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-2025 Andriel Ferreira <https://github.com/AndrielFR>

query ($id: Int, $mal_id: Int) {
Media (id: $id, idMal: $mal_id, type: MANGA) {
query ($id: Int) {
Media (id: $id, type: MANGA) {
id
idMal
title {
Expand Down
40 changes: 14 additions & 26 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,18 @@ impl Client {
///
/// ```
/// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> {
/// let anime = client.get_anime(Some(1), None).await?;
/// let anime = client.get_anime(1).await?;
///
/// # Ok(())
/// # }
/// ```
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.unwrap_or(0) }),
};
pub async fn get_anime(&self, id: i64) -> Result<crate::models::Anime> {
let data = self
.request(MediaType::Anime, Action::Get, variables)
.request(
MediaType::Anime,
Action::Get,
serde_json::json!({ "id": id }),
)
.await
.unwrap();

Expand Down Expand Up @@ -87,22 +83,18 @@ impl Client {
///
/// ```
/// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> {
/// let manga = client.get_manga(Some(1), None).await?;
/// let manga = client.get_manga(1).await?;
///
/// # Ok(())
/// # }
/// ```
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.unwrap_or(0) }),
};
pub async fn get_manga(&self, id: i64) -> Result<crate::models::Manga> {
let data = self
.request(MediaType::Manga, Action::Get, variables)
.request(
MediaType::Manga,
Action::Get,
serde_json::json!({ "id": id }),
)
.await
.unwrap();

Expand Down Expand Up @@ -207,10 +199,6 @@ impl Client {
.await
.unwrap();

std::fs::File::create("user.json")
.unwrap()
.write_all(data["data"]["User"].to_string().as_bytes())
.unwrap();
match serde_json::from_str::<User>(&data["data"]["User"].to_string()) {
Ok(user) => Ok(user),
Err(e) => Err(crate::Error::ApiError(e.to_string())),
Expand Down
7 changes: 2 additions & 5 deletions src/models/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ pub struct Anime {
impl Anime {
pub async fn load_full(self) -> Result<Self> {
if !self.is_full_loaded {
let mut anime = Client::default()
.get_anime(Some(self.id), self.id_mal)
.await
.unwrap();
let mut anime = Client::default().get_anime(self.id).await.unwrap();
anime.is_full_loaded = true;
Ok(anime)
} else {
panic!("This anime is already full loaded")
panic!("This anime is already full loaded!")
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/models/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ pub struct Manga {
impl Manga {
pub async fn load_full(self) -> Result<Self> {
if !self.is_full_loaded {
let mut manga = Client::default()
.get_manga(Some(self.id), self.id_mal)
.await
.unwrap();
let mut manga = Client::default().get_manga(self.id).await.unwrap();
manga.is_full_loaded = true;

Ok(manga)
Expand Down
2 changes: 1 addition & 1 deletion tests/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use rust_anilist::Client;

#[tokio::test]
async fn get_anime() {
let anime = Client::default().get_anime(Some(20), None).await;
let anime = Client::default().get_anime(20).await;
assert!(anime.is_ok())
}
2 changes: 1 addition & 1 deletion tests/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use rust_anilist::Client;

#[tokio::test]
async fn get_manga() {
let manga = Client::default().get_manga(Some(30026), None).await;
let manga = Client::default().get_manga(30026).await;
assert!(manga.is_ok())
}

0 comments on commit b0c98d7

Please sign in to comment.