-
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
6 changed files
with
69 additions
and
49 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,33 @@ | ||
from abc import ABC, abstractmethod | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
from typing import List | ||
|
||
import requests | ||
from pydantic import parse_obj_as | ||
from requests import Response | ||
|
||
from config import service_settings | ||
from src.models.currency import Currency | ||
from src.models.operation import Operation, OperationToShow | ||
from src.urls import Urls | ||
|
||
|
||
class AbstractOperationsGetter(ABC): | ||
@abstractmethod | ||
def get(self, user_id: int) -> List[OperationToShow]: | ||
pass | ||
|
||
|
||
class ConcreteOperationsGetter(AbstractOperationsGetter): | ||
def _get_operation(self, operation: Operation) -> OperationToShow: | ||
response: Response = requests.get( | ||
f'{Urls.CURRENCIES.value}/{operation.currency_id}' | ||
) | ||
currency = Currency.parse_obj(response.json()) | ||
return OperationToShow(**operation.dict(), currency_name=currency.name) | ||
|
||
def get(self, user_id: int) -> List[OperationToShow]: | ||
response: Response = requests.get(f'{Urls.USERS.value}/{user_id}/operations') | ||
operations = parse_obj_as(List[Operation], response.json()) | ||
with ThreadPoolExecutor(service_settings.max_workers) as executor: | ||
return list(executor.map(self._get_operation, operations)) |
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,19 @@ | ||
from decimal import Decimal | ||
from enum import Enum | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class OperationType(Enum): | ||
BUY = 'BUY' | ||
SELL = 'SELL' | ||
|
||
|
||
class Operation(BaseModel): | ||
operation_type: OperationType | ||
currency_id: int | ||
amount: Decimal | ||
|
||
|
||
class OperationToShow(Operation): | ||
currency_name: str |
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