-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
75 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
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,32 @@ | ||
from abc import ABC, abstractmethod | ||
from http import HTTPStatus | ||
from typing import Any, Dict | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from src.models.user import User | ||
from src.urls import Urls | ||
|
||
|
||
class AbstractAuthorization(ABC): | ||
@abstractmethod | ||
def sign_in(self, username: str) -> User: | ||
pass | ||
|
||
|
||
class ConcreteAuthorization(AbstractAuthorization): | ||
def _deserialize(self, json_: Dict[str, Any]): | ||
return User.parse_obj(json_) | ||
|
||
def _get_user(self, username: str) -> User: | ||
response: Response = requests.get( | ||
Urls.USERS.value, params={'user_name': username} | ||
) | ||
return self._deserialize(response.json()) | ||
|
||
def sign_in(self, username: str) -> User: | ||
response: Response = requests.post(Urls.USERS.value, json={'login': username}) | ||
if response.status_code == HTTPStatus.BAD_REQUEST: | ||
return self._get_user(username) | ||
return self._deserialize(response.json()) |
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,62 +1,31 @@ | ||
from abc import ABC, abstractmethod | ||
from http import HTTPStatus | ||
from typing import Any, Dict, List | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from src.models.currency import Currency | ||
from typing import List | ||
|
||
from src.DAL.auth import AbstractAuthorization, ConcreteAuthorization | ||
from src.DAL.currencies import AbstractCurrenciesGetter, ConcreteCurrencyGetter | ||
from src.DAL.user_currencies import ( | ||
AbstractUserCurrencyGetter, | ||
ConcreteUserCurrencyGetter, | ||
) | ||
from src.models.currency import Currency, UserCurrency | ||
from src.models.user import User | ||
from src.urls import Urls | ||
from pydantic import parse_obj_as | ||
|
||
|
||
class AbstractAuthorization(ABC): | ||
@abstractmethod | ||
def sign_in(self, username: str) -> User: | ||
pass | ||
|
||
|
||
class AbstractCurrenciesGetter(ABC): | ||
@abstractmethod | ||
def get(self) -> List[Currency]: | ||
pass | ||
|
||
|
||
class ConcreteAuthorization(AbstractAuthorization): | ||
def _deserialize(self, json_: Dict[str, Any]): | ||
return User.parse_obj(json_) | ||
|
||
def _get_user(self, username: str) -> User: | ||
response: Response = requests.get( | ||
Urls.USERS.value, params={'user_name': username} | ||
) | ||
return self._deserialize(response.json()) | ||
|
||
def sign_in(self, username: str) -> User: | ||
response: Response = requests.post(Urls.USERS.value, json={'login': username}) | ||
if response.status_code == HTTPStatus.BAD_REQUEST: | ||
return self._get_user(username) | ||
return self._deserialize(response.json()) | ||
|
||
|
||
class ConcreteCurrencyGetter(AbstractCurrenciesGetter): | ||
def get(self) -> List[Currency]: | ||
response: Response = requests.get(Urls.CURRENCIES.value) | ||
return parse_obj_as(List[Currency], response.json()) | ||
|
||
|
||
class Client: | ||
def __init__( | ||
self, | ||
authorization: AbstractAuthorization = ConcreteAuthorization(), | ||
currencies_getter: AbstractCurrenciesGetter = ConcreteCurrencyGetter(), | ||
user_currencies_getter: AbstractUserCurrencyGetter = ConcreteUserCurrencyGetter(), | ||
): | ||
self._authorization: AbstractAuthorization = authorization | ||
self._currencies_getter: AbstractCurrenciesGetter = currencies_getter | ||
self._user_currencies_getter = user_currencies_getter | ||
|
||
def get_currencies(self) -> List[Currency]: | ||
def get_all_currencies(self) -> List[Currency]: | ||
return self._currencies_getter.get() | ||
|
||
def get_user_currencies(self, user_id: int) -> List[UserCurrency]: | ||
return self._user_currencies_getter.get(user_id) | ||
|
||
def sign_in(self, username: str) -> User: | ||
return self._authorization.sign_in(username) |
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 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List | ||
|
||
import requests | ||
from pydantic import parse_obj_as | ||
from requests import Response | ||
|
||
from src.models.currency import Currency | ||
from src.urls import Urls | ||
|
||
|
||
class AbstractCurrenciesGetter(ABC): | ||
@abstractmethod | ||
def get(self) -> List[Currency]: | ||
pass | ||
|
||
|
||
class ConcreteCurrencyGetter(AbstractCurrenciesGetter): | ||
def get(self) -> List[Currency]: | ||
response: Response = requests.get(Urls.CURRENCIES.value) | ||
return parse_obj_as(List[Currency], response.json()) |
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,30 @@ | ||
from abc import ABC, abstractmethod | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
from typing import List, Iterator | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from config import service_settings | ||
from src.models.currency import UserCurrency, Currency, CurrencyItem | ||
from src.urls import Urls | ||
from pydantic import parse_obj_as | ||
|
||
|
||
class AbstractUserCurrencyGetter(ABC): | ||
@abstractmethod | ||
def get(self, user_id: int) -> List[UserCurrency]: | ||
pass | ||
|
||
|
||
class ConcreteUserCurrencyGetter(AbstractUserCurrencyGetter): | ||
def _get_currency(self, currency_item: CurrencyItem) -> UserCurrency: | ||
response: Response = requests.get(f'{Urls.CURRENCIES.value}/{currency_item.currency_id}') | ||
currency = Currency.parse_obj(response.json()) | ||
return UserCurrency(**currency.dict(), amount=currency_item.amount) | ||
|
||
def get(self, user_id: int) -> List[UserCurrency]: | ||
response: Response = requests.get(f'{Urls.USERS.value}/{user_id}/currencies') | ||
user_currencies = parse_obj_as(List[CurrencyItem], response.json()) | ||
with ThreadPoolExecutor(max_workers=service_settings.max_workers) as executor: | ||
return list(executor.map(lambda item: self._get_currency(item), user_currencies)) |
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