Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR] introduces ApiRequest and dynamic dispatch across implemen…
…tations (#19)
- Loading branch information
1 parent
4c9dd6f
commit cc8d34d
Showing
18 changed files
with
131 additions
and
62 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,4 +0,0 @@ | ||
from typing import Any, Dict | ||
|
||
PokeApiEndpoints = Dict[str, str] | ||
Pokemon = Dict[str, Any] | ||
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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
from enum import Enum | ||
from typing import Type | ||
|
||
from pokedex.api.request.implementations.cached import CachedPokeApiRequest | ||
from pokedex.api.request.implementations.default import PokeApiRequest | ||
from pokedex.api.request.protocol import DeferredRequest | ||
from pokedex.constants import API_REQUEST_IMPL | ||
|
||
|
||
class ApiRequest(Enum): | ||
DEFAULT: DeferredRequest = PokeApiRequest | ||
CACHED: DeferredRequest = CachedPokeApiRequest | ||
|
||
@staticmethod | ||
def type() -> Type[DeferredRequest]: | ||
return ApiRequest[API_REQUEST_IMPL].value |
Empty file.
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,17 @@ | ||
from dataclasses import dataclass | ||
|
||
from requests import Response | ||
|
||
from pokedex.db.client import cached_get | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CachedPokeApiRequest: | ||
url: str | ||
|
||
def __call__(self) -> Response: | ||
return cached_get(self.url) | ||
|
||
@property | ||
def __name__(self): | ||
return f"{self.__class__.__name__}:{self.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from dataclasses import dataclass | ||
|
||
import requests | ||
|
||
|
||
@dataclass(frozen=True) | ||
class PokeApiRequest: | ||
url: str | ||
|
||
def __call__(self) -> requests.Response: | ||
response = requests.get(self.url) | ||
response.raise_for_status() | ||
return response | ||
|
||
@property | ||
def __name__(self): | ||
return f"{self.__class__.__name__}:{self.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from typing import Protocol, runtime_checkable | ||
|
||
from requests import Response | ||
|
||
|
||
@runtime_checkable | ||
class DeferredRequest(Protocol): | ||
url: str | ||
|
||
def __call__(self) -> Response: | ||
classname = self.__class__.__name__ | ||
raise NotImplementedError(f"Followers of the {classname} Protocol must return a {Response.__name__}") |
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,5 +1,10 @@ | ||
from os import environ | ||
from pathlib import Path | ||
|
||
PROJECTROOT = Path(__file__).parent.parent.absolute() | ||
DBROOT = PROJECTROOT / "pokedex" / "db" | ||
CACHEPATH = DBROOT / "cache" | ||
|
||
BASE_URL = "https://pokeapi.co/api/v2/" | ||
|
||
API_REQUEST_IMPL = environ.get("API_REQUEST_IMPL") or "DEFAULT" |
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
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,21 @@ | ||
import json | ||
from unittest import TestCase | ||
|
||
from pokedex.db.models import Report | ||
|
||
|
||
class TestReport(TestCase): | ||
|
||
def test_report_representation(self): | ||
results = { | ||
"https://pokeapi.co/api/v2/pokemon/1/": True, | ||
"https://pokeapi.co/api/v2/pokemon/2/": True, | ||
"https://pokeapi.co/api/v2/pokemon/3/": False, | ||
} | ||
report = Report(persisted=results) | ||
expected_report = json.dumps( | ||
{"persisted": results, "count": len(results)}, | ||
indent=4 | ||
) | ||
|
||
assert str(report) == expected_report |