-
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.
Create requests to the service to receive data (#6)
* Update settings * Update validate for password * Move packets with the schemas * Create base of exceptions for requests * Create requests to the service to receive data * Update protocols * Update schemas
- Loading branch information
Showing
11 changed files
with
202 additions
and
4 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
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
Empty file.
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 datetime import datetime | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
|
||
from .users import GetUser | ||
|
||
|
||
class MeetingCreate(BaseModel): | ||
date: datetime | ||
description: str | ||
|
||
|
||
class GetMeeting(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
date: datetime | ||
is_open: bool | ||
description: str | ||
|
||
|
||
class MeetingUpdate(BaseModel): | ||
date: datetime | None = None | ||
is_open: bool | None = None | ||
description: str | None = None | ||
|
||
|
||
class MeetingParticipants(BaseModel): | ||
id: int | ||
date: datetime | ||
is_open: bool | ||
description: str | ||
users: list[GetUser] |
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,25 @@ | ||
from pydantic import BaseModel, ConfigDict, EmailStr, Field | ||
|
||
|
||
class GetUser(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
id: int | ||
name: str | ||
phone: str | ||
email: EmailStr | ||
meeting_id: int | ||
|
||
|
||
class UserCreate(BaseModel): | ||
name: str = Field(..., min_length=1, max_length=255) | ||
phone: str | ||
email: EmailStr | ||
meeting_id: int | ||
|
||
|
||
class UserUpdate(BaseModel): | ||
name: str | None = None | ||
phone: str | None = None | ||
email: EmailStr | None = None | ||
meeting_id: int | None = None |
Empty file.
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,2 @@ | ||
class HTTPRequestError(Exception): | ||
"""Ошибка запроса к серверу.""" |
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,69 @@ | ||
from collections import Generator | ||
from http import HTTPStatus | ||
|
||
import aiohttp | ||
from aiohttp import ClientResponse | ||
|
||
from src.core import settings | ||
from src.schemas import meetings | ||
from src.services.exceptions import HTTPRequestError | ||
|
||
|
||
class MeetingService: | ||
def __init__(self): | ||
self._path = '/meetings' | ||
|
||
async def get_meetings(self) -> Generator[meetings.GetMeeting]: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.get(self._path) as response: | ||
return await self._get_meetings(response) | ||
|
||
async def create_meeting(self, | ||
meeting: meetings.MeetingCreate) -> meetings.GetMeeting: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.post( | ||
self._path, data=meeting.model_dump() | ||
) as response: | ||
if response.status == HTTPStatus.OK: | ||
return meetings.GetMeeting(**(await response.json())) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def update_meeting( | ||
self, | ||
id: int, | ||
meeting: meetings.MeetingUpdate | ||
) -> meetings.GetMeeting: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.patch( | ||
f'{self._path}/{id}', data=meeting.model_dump() | ||
) as response: | ||
return await self._get_meeting(response) | ||
|
||
async def delete_meeting(self, id: int): | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.delete(f'{self._path}/{id}') as response: | ||
if not (response.status == HTTPStatus.OK): | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def get_participants_list(self, id: int): | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.get( | ||
f'{self._path}/{id}/participants') as response: | ||
if response.status == HTTPStatus.OK: | ||
return meetings.MeetingParticipants( | ||
**(await response.json())) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def _get_meeting(self, | ||
response: ClientResponse) -> meetings.GetMeeting: | ||
if response.status == HTTPStatus.OK: | ||
return meetings.GetMeeting(**(await response.json())) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def _get_meetings( | ||
self, response: ClientResponse | ||
) -> Generator[meetings.GetMeeting]: | ||
if response.status == HTTPStatus.OK: | ||
return (meetings.GetMeeting(**json) for json in | ||
await response.json()) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') |
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,52 @@ | ||
from collections import Generator | ||
from http import HTTPStatus | ||
|
||
import aiohttp | ||
from aiohttp import ClientResponse | ||
|
||
from src.core import settings | ||
from src.schemas import users | ||
from src.services.exceptions import HTTPRequestError | ||
|
||
|
||
class UserService: | ||
def __init__(self): | ||
self._path = '/users' | ||
|
||
async def get_users(self) -> Generator[users.GetUser]: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.get(self._path) as response: | ||
return await self._get_users(response) | ||
|
||
async def create_user(self, user: users.UserCreate) -> users.GetUser: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.post(self._path, | ||
data=user.model_dump()) as response: | ||
return await self._get_user(response) | ||
|
||
async def update_user(self, id: int, | ||
user: users.UserUpdate) -> users.GetUser: | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.post(f'self._path/{id}', | ||
data=user.model_dump()) as response: | ||
return await self._get_user(response) | ||
|
||
async def delete_user(self, id: int): | ||
async with aiohttp.ClientSession(settings.url) as session: | ||
async with session.delete(f'{self._path}/{id}') as response: | ||
if not (response.status == HTTPStatus.OK): | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def _get_user(self, | ||
response: ClientResponse) -> users.GetUser: | ||
if response.status == HTTPStatus.OK: | ||
return users.GetUser(**(await response.json())) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') | ||
|
||
async def _get_users( | ||
self, response: ClientResponse | ||
) -> Generator[users.GetUser]: | ||
if response.status == HTTPStatus.OK: | ||
return (users.GetUser(**json) for json in | ||
await response.json()) | ||
raise HTTPRequestError('Ошибка запроса! Повторите снова!') |