-
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.
- Loading branch information
Showing
8 changed files
with
332 additions
and
163 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
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,85 @@ | ||
""" | ||
Base module for the Catboys API. Endpoints can be found at https://catboys.com/api | ||
""" | ||
import typing | ||
import requests | ||
|
||
from anime_api import exceptions | ||
|
||
from anime_api.apis.catboys.objects import Image, EightBall, DiceRoll | ||
|
||
|
||
class CatboysAPI: | ||
""" | ||
Endpoints: https://api.catboys.com/endpoints | ||
""" | ||
|
||
endpoint: str = "https://api.catboys.com" | ||
|
||
def __init__(self, endpoint: typing.Optional[str] = None): | ||
self.endpoint = endpoint or self.endpoint | ||
|
||
def get_random_image(self) -> Image: | ||
""" | ||
Returns a random image. | ||
""" | ||
|
||
response = requests.get(self.endpoint + "/img") | ||
CatboysAPI._check_response_code(response) | ||
|
||
return Image.from_json(response.json()) | ||
|
||
def get_random_baka_gif(self) -> str: | ||
""" | ||
Returns a random baka gif. | ||
""" | ||
|
||
response = requests.get(self.endpoint + "/baka") | ||
CatboysAPI._check_response_code(response) | ||
|
||
return response.json()["url"] | ||
|
||
def get_eight_ball(self) -> EightBall: | ||
""" | ||
Returns an 8ball prediction. | ||
""" | ||
|
||
response = requests.get(self.endpoint + "/8ball") | ||
CatboysAPI._check_response_code(response) | ||
|
||
return EightBall.from_json(response.json()) | ||
|
||
def get_dice_roll(self) -> DiceRoll: | ||
""" | ||
Returns a random number and image from a virtual 6-sided dice. | ||
""" | ||
|
||
response = requests.get(self.endpoint + "/dice") | ||
CatboysAPI._check_response_code(response) | ||
|
||
return DiceRoll.from_json(response.json()) | ||
|
||
def get_catboy_saying(self) -> str: | ||
""" | ||
Returns a random saying from a virtual catboy. | ||
""" | ||
|
||
response = requests.get(self.endpoint + "/catboy") | ||
CatboysAPI._check_response_code(response) | ||
|
||
return response.json()["response"] | ||
|
||
@staticmethod | ||
def _check_response_code(res) -> None: | ||
""" | ||
Check if the request was successful. | ||
""" | ||
data = res.json() | ||
if ( | ||
res.status_code not in range(200, 300) | ||
or data.get("error", "none") != "none" | ||
): | ||
raise exceptions.ServerError( | ||
res.status_code, | ||
f"An error occurred while fetching the data from the server{'. ' + data['error'] if data.get('error', 'none') != 'none' else ''}", | ||
) |
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,39 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class _Artist: | ||
name: str | ||
url: str | ||
|
||
|
||
@dataclass | ||
class Image: | ||
url: str | ||
artist: _Artist | ||
source_url: str | ||
|
||
def from_json(data: dict): | ||
return Image( | ||
url=data["url"], | ||
artist=_Artist(data["artist"], data["artist_url"]), | ||
source_url=data["source_url"], | ||
) | ||
|
||
|
||
@dataclass | ||
class EightBall: | ||
answer: str | ||
image: str | ||
|
||
def from_json(data: dict): | ||
return EightBall(answer=data["response"], image=data["url"]) | ||
|
||
|
||
@dataclass | ||
class DiceRoll: | ||
number: int | ||
image: str | ||
|
||
def from_json(data: dict): | ||
return DiceRoll(number=data["response"], image=data["url"]) |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "anime-api" | ||
version = "0.15.4" | ||
version = "1.0.0" | ||
description = "A collection of wrappers for anime-related APIs" | ||
authors = ["Neki <[email protected]>"] | ||
readme = "README.md" | ||
|
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,58 @@ | ||
""" | ||
Run tests for the AnimuAPI class | ||
Usage: | ||
Replace the api_token variable with your API token. | ||
cd tests | ||
poetry run python -m pytest test_catboys_api.py | ||
""" | ||
from anime_api.apis import CatboysAPI | ||
from anime_api.apis.catboys import objects | ||
|
||
|
||
def test_get_random_image(): | ||
""" | ||
Test the get_random_image method | ||
""" | ||
api = CatboysAPI() | ||
image = api.get_random_image() | ||
assert isinstance(image, objects.Image) | ||
|
||
|
||
def test_get_random_baka_gif(): | ||
""" | ||
Test the get_random_baka_gif method | ||
""" | ||
api = CatboysAPI() | ||
gif = api.get_random_baka_gif() | ||
assert isinstance(gif, str) | ||
|
||
|
||
def test_get_eight_ball(): | ||
""" | ||
Test the get_eight_ball method | ||
""" | ||
api = CatboysAPI() | ||
eight_ball = api.get_eight_ball() | ||
assert isinstance(eight_ball, objects.EightBall) | ||
|
||
|
||
def test_get_dice_roll(): | ||
""" | ||
Test the get_dice_roll method | ||
""" | ||
|
||
api = CatboysAPI() | ||
dice_roll = api.get_dice_roll() | ||
assert isinstance(dice_roll, objects.DiceRoll) | ||
|
||
|
||
def test_get_catboy_saying(): | ||
""" | ||
Test the get_catboy_saying method | ||
""" | ||
|
||
api = CatboysAPI() | ||
catboy_saying = api.get_catboy_saying() | ||
assert isinstance(catboy_saying, str) |