-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
81 lines (64 loc) · 2.85 KB
/
Copy pathservices.py
File metadata and controls
81 lines (64 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from datetime import date, timedelta
from databases.models import User, Favorite
from databases.connection import async_session
from sqlalchemy import delete
from sqlalchemy.future import select
from aiohttp import ClientSession
from schemas import DaySummaryOutput
# ----------------------------------------------------------------
class UserService():
async def create_user(name: str):
async with async_session() as session:
session.add(User(name=name))
await session.commit()
async def delete_user(user_id: int):
async with async_session() as session:
await session.execute(delete(User).where(User.id==user_id))
await session.commit()
async def list_users():
async with async_session() as session:
result = await session.execute(select(User))
return result.scalars().all()
async def get_user_by_id(user_id: int):
async with async_session() as session:
result = await session.execute(select(User).where(User.id==user_id))
return result.scalar()
class FavoriteService():
async def add_favorite(user_id: int, symbol: str):
async with async_session() as session:
session.add(Favorite(user_id=user_id, symbol=symbol))
await session.commit()
async def delete_favorite(user_id: int, symbol: str):
async with async_session() as session:
await session.execute(delete(Favorite).where(
Favorite.user_id==user_id,
Favorite.symbol==symbol)
)
await session.commit()
# ---------------------------------------------------------------------
class AssetsService():
async def day_symmary(symbol: str):
async with ClientSession() as session:
yesterday = str(date.today() - timedelta(days=1))
base_url = "https://www.mercadobitcoin.net/api/{symbol}/day-summary/{date}"
yesterday = yesterday.replace('-', '/')
url = base_url.format(symbol=symbol, date=yesterday)
try:
response = await session.get(url=url)
data = await response.json()
dso = DaySummaryOutput(
symbol=symbol,
date=yesterday,
highest=data.get('highest', 0),
lowest=data.get('lowest', 0),
details='Success'
)
except Exception as err:
dso = DaySummaryOutput(
symbol=symbol,
date=yesterday,
highest=0, lowest=0,
details=f'ERROR ==>> {str(err)}'
)
finally:
return dso.model_dump()