-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Nekidev/Animechan
- Loading branch information
Showing
9 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
__pycache__/ | ||
__pycache__/ | ||
.pytest_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Base module for the Animechan API. The documentation is available at | ||
https://animechan.vercel.app/guide | ||
""" | ||
import typing | ||
import requests | ||
|
||
from anime_api import exceptions | ||
from anime_api.apis.animechan.objects import Quote | ||
|
||
|
||
class AnimechanAPI: | ||
""" | ||
Docs: https://animechan.vercel.app/guide | ||
""" | ||
|
||
endpoint = "https://animechan.vercel.app/api" | ||
|
||
def __init__(self, endpoint: typing.Optional[str] = None): | ||
self.endpoint = endpoint or self.endpoint | ||
|
||
def get_random_quote(self) -> Quote: | ||
""" | ||
Get a random quote from the API. | ||
""" | ||
response = requests.get(f"{self.endpoint}/random") | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError( | ||
status_code=response.status_code, | ||
) | ||
|
||
return Quote(**response.json()) | ||
|
||
def get_10_random_quotes(self) -> typing.List[Quote]: | ||
""" | ||
Get 10 random quotes from the API. | ||
""" | ||
response = requests.get(f"{self.endpoint}/quotes") | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError( | ||
status_code=response.status_code, | ||
) | ||
|
||
return [Quote(**quote) for quote in response.json()] | ||
|
||
def search_by_anime_title(self, anime_title: str, page: int = 1) -> Quote: | ||
""" | ||
Return a list of quotes from the given anime. | ||
""" | ||
response = requests.get( | ||
f"{self.endpoint}/quotes/anime", params={"title": anime_title, "page": page} | ||
) | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError( | ||
status_code=response.status_code, | ||
) | ||
|
||
return [Quote(**quote) for quote in response.json()] | ||
|
||
def search_by_character_name(self, character_name: str, page: int = 1) -> Quote: | ||
""" | ||
Return a list of quotes from the given character. | ||
""" | ||
response = requests.get( | ||
f"{self.endpoint}/quotes/character", | ||
params={"name": character_name, "page": page}, | ||
) | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError( | ||
status_code=response.status_code, | ||
) | ||
|
||
return [Quote(**quote) for quote in response.json()] | ||
|
||
def get_animes(self, page: int = 1): | ||
""" | ||
Return a list of animes. | ||
""" | ||
response = requests.get(f"{self.endpoint}/available/anime", params={"page": page}) | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError( | ||
status_code=response.status_code, | ||
) | ||
|
||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Quote: | ||
""" | ||
Object representing a quote. Anime and character are not objects because the API returns a | ||
title (for the anime) or a name (for the character). | ||
""" | ||
|
||
anime: str | ||
character: str | ||
quote: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Run tests for the AnimechanAPI class. | ||
Usage: | ||
cd tests | ||
poetry run python -m pytest animechan.py | ||
""" | ||
from anime_api.apis.animechan import AnimechanAPI | ||
from anime_api.apis.animechan.objects import Quote | ||
|
||
|
||
def test_get_quote(): | ||
""" | ||
Test the get_quote method. Should return a single quote. | ||
""" | ||
quote = AnimechanAPI().get_random_quote() | ||
|
||
assert isinstance(quote, Quote), "The return type of get_quote() is not a Quote." | ||
|
||
|
||
def test_get_10_random_quotes(): | ||
""" | ||
Test the get_10_random_quotes method. Should return a list of quotes. | ||
""" | ||
quotes = AnimechanAPI().get_10_random_quotes() | ||
|
||
assert isinstance( | ||
quotes, list | ||
), "The return type of get_10_random_quotes() is not a list." | ||
assert len(quotes) > 0, "The list of quotes is empty." | ||
for quote in quotes: | ||
assert isinstance( | ||
quote, Quote | ||
), "The list of quotes does not contain Quote objects." | ||
|
||
|
||
def test_search_quote_by_anime_title(): | ||
""" | ||
Test the search_quote_by_anime_title method. Should return a list of quotes. | ||
""" | ||
quotes = AnimechanAPI().search_by_anime_title("Naruto") | ||
|
||
assert isinstance( | ||
quotes, list | ||
), "The return type of search_quote_by_anime_title() is not a list." | ||
assert len(quotes) > 0, "The list of quotes is empty." | ||
for quote in quotes: | ||
assert isinstance( | ||
quote, Quote | ||
), "The list of quotes does not contain Quote objects." | ||
|
||
|
||
def test_search_quote_by_character_name(): | ||
""" | ||
Test the search_quote_by_character_name method. Should return a list of quotes. | ||
""" | ||
quotes = AnimechanAPI().search_by_character_name("Naruto") | ||
|
||
assert isinstance( | ||
quotes, list | ||
), "The return type of search_quote_by_character_name() is not a list." | ||
assert len(quotes) > 0, "The list of quotes is empty." | ||
|
||
|
||
def test_get_animes(): | ||
""" | ||
Test the get_animes method. Should return a list of anime titles. | ||
""" | ||
animes = AnimechanAPI().get_animes() | ||
|
||
assert isinstance(animes, list), "The return type of get_animes() is not a list." | ||
assert len(animes) > 0, "The list of animes is empty." |