Skip to content

Commit

Permalink
coderabbit comments
Browse files Browse the repository at this point in the history
  • Loading branch information
felipao-mx committed Sep 24, 2024
1 parent 1590a9e commit c96049f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 22 deletions.
4 changes: 1 addition & 3 deletions cuenca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
'get_balance',
]

from typing import cast

from . import http
from .resources import (
Account,
Expand Down Expand Up @@ -96,5 +94,5 @@


def get_balance(session: http.Session = session) -> int:
balance_entry = cast('BalanceEntry', BalanceEntry.first(session=session))
balance_entry = BalanceEntry.first(session=session)
return balance_entry.rolling_balance if balance_entry else 0
46 changes: 36 additions & 10 deletions cuenca/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime as dt
import json
from io import BytesIO
from typing import ClassVar, Generator, Optional, Type, TypeVar, cast
from typing import Any, ClassVar, Generator, Optional, Type, TypeVar, cast
from urllib.parse import urlencode

from cuenca_validations.types import (
Expand Down Expand Up @@ -35,7 +35,10 @@ def to_dict(self):
class Retrievable(Resource):
@classmethod
def retrieve(
cls: Type[R_co], id: str, *, session: Session = global_session
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,7 +52,10 @@ def refresh(self, *, session: Session = global_session) -> None:
class Creatable(Resource):
@classmethod
def _create(
cls: Type[R_co], *, session: Session = global_session, **data
cls: Type[R_co],
*,
session: Session = global_session,
**data: Any,
) -> R_co:
resp = session.post(cls._resource, data)
return cls(**resp)
Expand All @@ -61,7 +67,11 @@ class Updateable(Resource):

@classmethod
def _update(
cls: Type[R_co], id: str, *, session: Session = global_session, **data
cls: Type[R_co],
id: str,
*,
session: Session = global_session,
**data: Any,
) -> R_co:
resp = session.patch(f'/{cls._resource}/{id}', data)
return cls(**resp)
Expand All @@ -72,7 +82,11 @@ class Deactivable(Resource):

@classmethod
def deactivate(
cls: Type[R_co], id: str, *, session: Session = global_session, **data
cls: Type[R_co],
id: str,
*,
session: Session = global_session,
**data: Any,
) -> R_co:
resp = session.delete(f'/{cls._resource}/{id}', data)
return cls(**resp)
Expand Down Expand Up @@ -115,7 +129,7 @@ def _upload(
user_id: str,
*,
session: Session = global_session,
**data,
**data: Any,
) -> R_co:
encoded_file = base64.b64encode(file)
resp = session.request(
Expand All @@ -137,7 +151,10 @@ class Queryable(Resource):

@classmethod
def one(
cls: Type[R_co], *, session: Session = global_session, **query_params
cls: Type[R_co],
*,
session: Session = global_session,
**query_params: Any,
) -> R_co:
q = cast(Queryable, cls)._query_params(limit=2, **query_params)
resp = session.get(cls._resource, q.dict())
Expand All @@ -151,7 +168,10 @@ def one(

@classmethod
def first(
cls: Type[R_co], *, session: Session = global_session, **query_params
cls: Type[R_co],
*,
session: Session = global_session,
**query_params: Any,
) -> Optional[R_co]:
q = cast(Queryable, cls)._query_params(limit=1, **query_params)
resp = session.get(cls._resource, q.dict())
Expand All @@ -165,15 +185,21 @@ def first(

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

@classmethod
def all(
cls: Type[R_co], *, session: Session = global_session, **query_params
cls: Type[R_co],
*,
session: Session = global_session,
**query_params: Any,
) -> Generator[R_co, None, None]:
session = session or global_session
q = cast(Queryable, cls)._query_params(**query_params)
Expand Down
4 changes: 1 addition & 3 deletions cuenca/resources/card_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def create(
pin_block=pin_block,
pin_attempts_exceeded=pin_attempts_exceeded,
)
return cast(
'CardValidation', cls._create(session=session, **req.dict())
)
return cls._create(session=session, **req.dict())

@property
def card(self) -> Card:
Expand Down
1 change: 1 addition & 0 deletions cuenca/resources/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def update(
like url and is_active.
:param endpoint_id: existing endpoint_id
:param url
:param events: Optional list of enabled events to update
:param is_enable
:param session
:return: Updated endpoint object
Expand Down
4 changes: 2 additions & 2 deletions cuenca/resources/otps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar, cast
from typing import ClassVar

from ..http import Session, session as global_session
from .base import Creatable
Expand All @@ -21,4 +21,4 @@ def create(cls, session: Session = global_session) -> 'Otp':
"""
Use this method to create a OTP seed
"""
return cast('Otp', cls._create(session=session))
return cls._create(session=session)
2 changes: 1 addition & 1 deletion cuenca/resources/savings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create(
category: SavingCategory,
goal_amount: Optional[int] = None,
goal_date: Optional[dt.datetime] = None,
):
) -> 'Saving':
request = SavingRequest(
name=name,
category=category,
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def create_many(cls, requests: List[TransferRequest]) -> DictStrAny:
except (CuencaException, HTTPError) as e:
transfers['errors'].append(dict(request=req, error=e))
else:
transfers['submitted'].append(cast('Transfer', transfer))
transfers['submitted'].append(transfer)
return transfers

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def update(
curp_document: Optional[HttpUrl] = None,
*,
session: Session = global_session,
):
) -> 'User':
request = UserUpdateRequest(
phone_number=phone_number,
email_address=email_address,
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/wallet_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create(
wallet_uri: str,
transaction_type: WalletTransactionType,
amount: int,
):
) -> 'WalletTransaction':
request = WalletTransactionRequest(
wallet_uri=wallet_uri,
transaction_type=transaction_type,
Expand Down

0 comments on commit c96049f

Please sign in to comment.