Skip to content

Commit

Permalink
Q_co
Browse files Browse the repository at this point in the history
  • Loading branch information
felipao-mx committed Sep 21, 2024
1 parent 1945e18 commit 8254dd1
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions cuenca/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from ..exc import MultipleResultsFound, NoResultFound
from ..http import Session, session as global_session

R = TypeVar('R', bound='Resource', covariant=True)
R_co = TypeVar('R_co', bound='Resource', covariant=True)
Q_co = TypeVar('Q_co', bound='Queryable', covariant=True)


class Resource(BaseModel):
Expand All @@ -35,8 +36,8 @@ def to_dict(self):
class Retrievable(Resource):
@classmethod
def retrieve(
cls: Type[R], id: str, *, session: Session = global_session
) -> R:
cls: Type[R_co], id: str, *, session: Session = global_session
) -> R_co:
resp = session.get(f'/{cls._resource}/{id}')
return cls(**resp)

Expand All @@ -49,8 +50,8 @@ def refresh(self, *, session: Session = global_session) -> None:
class Creatable(Resource):
@classmethod
def _create(
cls: Type[R], *, session: Session = global_session, **data
) -> R:
cls: Type[R_co], *, session: Session = global_session, **data
) -> R_co:
resp = session.post(cls._resource, data)
return cls(**resp)

Expand All @@ -61,8 +62,8 @@ class Updateable(Resource):

@classmethod
def _update(
cls: Type[R], id: str, *, session: Session = global_session, **data
) -> R:
cls: Type[R_co], id: str, *, session: Session = global_session, **data
) -> R_co:
resp = session.patch(f'/{cls._resource}/{id}', data)
return cls(**resp)

Expand All @@ -72,8 +73,8 @@ class Deactivable(Resource):

@classmethod
def deactivate(
cls: Type[R], id: str, *, session: Session = global_session, **data
) -> R:
cls: Type[R_co], id: str, *, session: Session = global_session, **data
) -> R_co:
resp = session.delete(f'/{cls._resource}/{id}', data)
return cls(**resp)

Expand All @@ -85,7 +86,7 @@ def is_active(self) -> bool:
class Downloadable(Resource):
@classmethod
def download(
cls: Type[R],
cls: Type[R_co],
id: str,
file_format: FileFormat = FileFormat.any,
*,
Expand All @@ -110,13 +111,13 @@ def xml(self) -> bytes:
class Uploadable(Resource):
@classmethod
def _upload(
cls: Type[R],
cls: Type[R_co],
file: bytes,
user_id: str,
*,
session: Session = global_session,
**data,
) -> R:
) -> R_co:
encoded_file = base64.b64encode(file)
resp = session.request(
'post',
Expand All @@ -137,8 +138,8 @@ class Queryable(Resource):

@classmethod
def one(
cls: Type[R], *, session: Session = global_session, **query_params
) -> R:
cls: Type[Q_co], *, session: Session = global_session, **query_params
) -> Q_co:
q = cls._query_params(limit=2, **query_params)
resp = session.get(cls._resource, q.dict())
items = resp['items']
Expand All @@ -151,8 +152,8 @@ def one(

@classmethod
def first(
cls: Type[R], *, session: Session = global_session, **query_params
) -> Optional[R]:
cls: Type[Q_co], *, session: Session = global_session, **query_params
) -> Optional[Q_co]:
q = cls._query_params(limit=1, **query_params)
resp = session.get(cls._resource, q.dict())
try:
Expand All @@ -165,16 +166,16 @@ def first(

@classmethod
def count(
cls: Type[R], *, session: Session = global_session, **query_params
cls: Type[Q_co], *, session: Session = global_session, **query_params
) -> int:
q = cls._query_params(count=True, **query_params)
resp = session.get(cls._resource, q.dict())
return resp['count']

@classmethod
def all(
cls: Type[R], *, session: Session = global_session, **query_params
) -> Generator[R, None, None]:
cls: Type[Q_co], *, session: Session = global_session, **query_params
) -> Generator[Q_co, None, None]:
session = session or global_session
q = cls._query_params(**query_params)
next_page_uri = f'{cls._resource}?{urlencode(q.dict())}'
Expand Down

0 comments on commit 8254dd1

Please sign in to comment.