Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/controllers/pokemon.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
from dataclasses import dataclass
from typing import Dict, List

import requests
from fastapi import APIRouter

router = APIRouter(
prefix="/pokemon",
)


@dataclass
class PokemonDTO:
name: str
url: str


@router.get("")
async def root():
return {"message": "Hello World"}
async def list_pokemon(page: int = 1):
limit = 20
offset = page - 1
url = f'https://pokeapi.co/api/v2/pokemon?offset={offset}&limit={limit}'

r = requests.get(url, auth=('origin-user',
'eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI'))

r2 = r.json()
return map_dict(r2)


def map_dict(response: Dict) -> List[PokemonDTO]:
return [PokemonDTO(name=item['name'], url=item['url']) for item in response['results']]
Empty file added src/tests/__init__.py
Empty file.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions src/tests/integration/controllers/test_pokemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, ANY

import requests

from src.controllers.pokemon import list_pokemon


class TestPokemonController(IsolatedAsyncioTestCase):
async def test_1(self):
with patch.object(requests, 'get') as mocked_request:
await list_pokemon(page=1)
mocked_request.assert_called_once_with('https://pokeapi.co/api/v2/pokemon?offset=0&limit=20', auth=ANY)

async def test_2(self):
with patch.object(requests, 'get') as mocked_request:
await list_pokemon(page=2)
mocked_request.assert_called_once_with('https://pokeapi.co/api/v2/pokemon?offset=20&limit=20', auth=ANY)