Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4c0e358
initial commit
oliiiiiiiiiiiii Dec 30, 2021
881149c
update mypy config
oliiiiiiiiiiiii Dec 30, 2021
fb25e0f
revert back to original
oliiiiiiiiiiiii Dec 30, 2021
d033605
create base file and ABCs for cleaner design
oliiiiiiiiiiiii Dec 31, 2021
af551fe
merge 8 object groups into 1 file
oliiiiiiiiiiiii Jan 2, 2022
c20acee
merge remaining endpoint groups
oliiiiiiiiiiiii Jan 2, 2022
8814ad1
create error property for all objects
oliiiiiiiiiiiii Jan 2, 2022
5e851e3
make group ABCs to reduce code duplication
oliiiiiiiiiiiii Jan 3, 2022
6ce1063
delte caching gonna do it later
oliiiiiiiiiiiii Jan 3, 2022
20313a0
nice project
torvalds Jan 7, 2022
2d939f7
create caching
oliiiiiiiiiiiii Jan 13, 2022
86eaadc
Delete pyproject.toml
oliiiiiiiiiiiii Jan 13, 2022
18d1f2d
remove unused import
oliiiiiiiiiiiii Jan 13, 2022
b5c525a
reformated with black
oliiiiiiiiiiiii Jan 13, 2022
73ecc7f
update typing
oliiiiiiiiiiiii Jan 13, 2022
91cf7a8
Delete __init__.pyi
oliiiiiiiiiiiii Jan 13, 2022
09bc425
create parsers.py and remove a bunch os stuff
oliiiiiiiiiiiii Jan 13, 2022
0a2fdf1
Merge branch 'rewrite' of https://github.com/oliiiiiiiiiiiii/pokeclie…
oliiiiiiiiiiiii Jan 13, 2022
1f08035
reformated with black
oliiiiiiiiiiiii Jan 13, 2022
0757d78
completed all the parsers
oliiiiiiiiiiiii Jan 14, 2022
51ed8b5
formatted with black
oliiiiiiiiiiiii Jan 14, 2022
f72305d
reduce code duplication
oliiiiiiiiiiiii Jan 14, 2022
d2f454e
fix returning of instance instead of a class
oliiiiiiiiiiiii Jan 14, 2022
71a1323
initial test for caching
oliiiiiiiiiiiii Jan 14, 2022
1b144bc
fixing shit
oliiiiiiiiiiiii Jan 15, 2022
4e7187e
fix a bunch of working : contest type not working
oliiiiiiiiiiiii Jan 16, 2022
b9a46bf
fix contest types ( all objects fixed )and final commit to caching
oliiiiiiiiiiiii Jan 16, 2022
284c176
remove from_cache kwarg
oliiiiiiiiiiiii Jan 16, 2022
c6193c4
changes to imports and add parsing for moves
oliiiiiiiiiiiii Jan 17, 2022
3f12157
add support for parsing of moves grp
oliiiiiiiiiiiii Jan 17, 2022
2b98229
no relative import
oliiiiiiiiiiiii Jan 17, 2022
13fcc2c
refactor class names.
Genoinno Jan 17, 2022
648978c
create a new branch
Genoinno Jan 17, 2022
8c4d508
Update parsers.py
oliiiiiiiiiiiii Jan 17, 2022
90f61f4
fix duplicate properties
oliiiiiiiiiiiii Jan 17, 2022
566bcc9
Merge branch 'rewrite' into TheGenocides-patch-1-rewrite
oliiiiiiiiiiiii Jan 17, 2022
eb55e7c
Merge pull request #8 from TheGenocides/TheGenocides-patch-1-rewrite
oliiiiiiiiiiiii Jan 17, 2022
a104c19
fix a property name
oliiiiiiiiiiiii Jan 17, 2022
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
135 changes: 135 additions & 0 deletions Pokemon/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from Pokemon.caching import CacheDict
import abc
import dataclasses
import typing
import httpx
import pydantic
import json

Payload = typing.Dict[str, typing.Any]
base_url = "https://pokeapi.co/api/v2/"


class BaseType1(pydantic.BaseModel, abc.ABC):

id: typing.Optional[typing.Union[int, str]]
name: typing.Optional[str]

@pydantic.root_validator(pre=True)
@classmethod
def id_or_name(
cls, vals: typing.Dict[str, typing.Any]
) -> typing.Optional[typing.Dict[str, typing.Any]]:
"""Checks if either of name or id is
passed returns error if none of
them is passed or both are passed"""

if "id" not in vals and "name" not in vals:
raise TypeError("Missing Arguments : name or id")
elif "id" in vals and "name" in vals:
raise TypeError("Too many Arguments provided : name and id")
return vals

@property
@abc.abstractmethod
def cache(self) -> object:
...

@property
@abc.abstractmethod
def address(self) -> str:
...

@property
@abc.abstractmethod
def error(self) -> Exception:
...

@property
def url(self) -> str:
return f"{base_url}{self.address}/{self.id or self.name}"

@property
def raw_data(self) -> CacheDict[int, CacheDict[str, typing.Any]]:
id_or_name = self.id or self.name
id_and_name_list = [i for h in self.cache.name_id_map.items() for i in h]
if not id_or_name in id_and_name_list:
try:
response = CacheDict(httpx.get(self.url).json())
self.cache.add_new(self.address, response)
except json.JSONDecodeError:
raise self.error(arg=id_or_name)
else:
return response
if not self.id:
return self.cache.cached_data[self.cache.name_id_map.get(self.name)][
self.address
]
return self.cache.cached_data[self.id][self.address]

class Config:
allow_mutation = False


class BaseType2(pydantic.BaseModel, abc.ABC):

id: typing.Union[int, str]

@property
@abc.abstractmethod
def cache(self) -> object:
...

@property
@abc.abstractmethod
def address(self) -> str:
...

@property
@abc.abstractmethod
def error(self) -> Exception:
...

@property
def url(self) -> str:
return f"{base_url}{self.address}/{self.id}"

@property
def raw_data(self) -> CacheDict[int, CacheDict[str, typing.Any]]:
if not id in self.cache.cached_data:
try:
response = CacheDict(httpx.get(self.url).json())
self.cache.add_new(self.address, response)
except json.JSONDecodeError:
raise self.error(arg=self.id)
else:
return response
return self.cache.cached_data[self.id][self.address]

...

class Config:
allow_mutation = False


@dataclasses.dataclass(frozen=True)
class UtilsParser(abc.ABC):
data: Payload


@dataclasses.dataclass(frozen=True)
class BaseParser1(UtilsParser):
@property
def id(self):
return self.data.get("id")

@property
def name(self):
return self.data.get("name")


@dataclasses.dataclass(frozen=True)
class BaseParser2(UtilsParser):
@property
def id(self):
return self.data.get("id")
Loading