diff --git a/.gitignore b/.gitignore index c608a32..5ecec2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__/ dist/ poetry.lock .env -.pytest_cache/ \ No newline at end of file +.pytest_cache/ +.venv/ \ No newline at end of file diff --git a/blagues_api/__init__.py b/blagues_api/__init__.py index 628e935..da3134a 100644 --- a/blagues_api/__init__.py +++ b/blagues_api/__init__.py @@ -1,3 +1,3 @@ -from .main import BlaguesAPI, BlagueType, CountJoke, Blague +from .main import Blague, BlaguesAPI, BlagueType, CountJoke __all__ = ["BlaguesAPI", "BlagueType", "CountJoke", "Blague"] diff --git a/blagues_api/main.py b/blagues_api/main.py index 962e6e6..db83228 100644 --- a/blagues_api/main.py +++ b/blagues_api/main.py @@ -1,7 +1,8 @@ +from enum import Enum +from typing import List + import aiohttp import pydantic -from typing import List -from enum import Enum class BlagueType(str, Enum): @@ -31,14 +32,12 @@ class BlaguesAPI: def __init__(self, token: str): self.token = token self.base_url = "https://www.blagues-api.fr/api" - self.headers = {'Authorization': f'Bearer {self.token}'} + self.headers = {"Authorization": f"Bearer {self.token}"} async def _get(self, url: str, params: dict = None) -> dict: async with aiohttp.ClientSession(raise_for_status=True) as session: async with session.get( - self.base_url + url, - headers=self.headers, - params=params + self.base_url + url, headers=self.headers, params=params ) as resp: return await resp.json() @@ -46,26 +45,23 @@ async def random(self, *, disallow: List[str] = None) -> Blague: endpoint = "/random" params = {"disallow": disallow} if disallow else {} data = await self._get(endpoint, params) - - return Blague.model_validate(data) + return Blague.model_validate(data) async def random_categorized(self, category: str) -> Blague: endpoint = f"/type/{category}/random" data = await self._get(endpoint) - - return Blague.model_validate(data) + return Blague.model_validate(data) async def from_id(self, id: int) -> Blague: endpoint = f"/id/{id}" data = await self._get(endpoint) - + return Blague.model_validate(data) async def count(self) -> CountJoke: endpoint = "/count" data = await self._get(endpoint) - - return CountJoke.model_validate(data) + return CountJoke.model_validate(data) diff --git a/tests/test_blagues_api.py b/tests/test_blagues_api.py index 4a53cea..4f35915 100644 --- a/tests/test_blagues_api.py +++ b/tests/test_blagues_api.py @@ -1,7 +1,7 @@ import pytest - from dotenv import dotenv_values -from blagues_api import BlaguesAPI, BlagueType, CountJoke, Blague + +from blagues_api import Blague, BlaguesAPI, BlagueType, CountJoke pytestmark = pytest.mark.asyncio @@ -11,6 +11,7 @@ def token(): env = dotenv_values(".env") return env["TOKEN"] + @pytest.fixture def client(token): return BlaguesAPI(token)