fix: reverse() получает урл из имени ендпоинта

develop
Сергей Ванюшкин 2024-02-06 15:12:32 +03:00
parent a2ed5a6732
commit f72c6fe4d7
4 changed files with 31 additions and 47 deletions

View File

@ -31,7 +31,7 @@ class RedisRepository:
pool: redis.Redis = Depends(get_redis_pool),
) -> None:
self.pool = pool
self.ttl = 1800
self.ttl = 2
async def get(self, key: str) -> Any | None:
data = await self.pool.get(key)

View File

@ -52,6 +52,6 @@ async def client(event_loop) -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(
app=app,
base_url='http://localhost:8000/api/v1/menus',
base_url='http://localhost:8000',
) as async_client:
yield async_client

View File

@ -1,6 +1,6 @@
from httpx import AsyncClient, Response
from .urls import reverse_url
from .urls import reverse
class Repository:
@ -9,28 +9,28 @@ class Repository:
async def read_all(ac: AsyncClient) -> tuple[int, dict]:
"""чтение всех меню"""
response: Response = await ac.get(reverse_url('menus'))
response: Response = await ac.get(reverse('get_menus'))
return response.status_code, response.json()
@staticmethod
async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Получение меню по id"""
response: Response = await ac.get(
reverse_url('menu', menu_id=data.get('id'))
reverse('get_menu', menu_id=data.get('id'))
)
return response.status_code, response.json()
@staticmethod
async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""создания меню"""
response: Response = await ac.post(reverse_url('menus'), json=data)
response: Response = await ac.post(reverse('add_menu'), json=data)
return response.status_code, response.json()
@staticmethod
async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Обновление меню по id"""
response: Response = await ac.patch(
reverse_url('menu', menu_id=data.get('id')),
reverse('update_menu', menu_id=data.get('id')),
json=data,
)
return response.status_code, response.json()
@ -39,7 +39,7 @@ class Repository:
async def delete(ac: AsyncClient, data: dict) -> int:
"""Удаление меню по id"""
response: Response = await ac.delete(
reverse_url('menu', menu_id=data.get('id')),
reverse('delete_menu', menu_id=data.get('id')),
)
return response.status_code
@ -48,7 +48,7 @@ class Repository:
async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]:
"""чтение всех меню"""
response: Response = await ac.get(
reverse_url('submenus', menu_id=menu.get('id')),
reverse('get_submenus', menu_id=menu.get('id')),
)
return response.status_code, response.json()
@ -60,8 +60,8 @@ class Repository:
) -> tuple[int, dict]:
"""Получение меню по id"""
response: Response = await ac.get(
reverse_url(
'submenu',
reverse(
'get_submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
@ -76,7 +76,7 @@ class Repository:
) -> tuple[int, dict]:
"""создания меню"""
response: Response = await ac.post(
reverse_url('submenu', menu_id=menu.get('id')),
reverse('create_submenu_item', menu_id=menu.get('id')),
json=submenu,
)
return response.status_code, response.json()
@ -87,8 +87,8 @@ class Repository:
) -> tuple[int, dict]:
"""Обновление меню по id"""
response: Response = await ac.patch(
reverse_url(
'submenu',
reverse(
'update_submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
@ -100,8 +100,8 @@ class Repository:
async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int:
"""Удаление меню по id"""
response: Response = await ac.delete(
reverse_url(
'submenu',
reverse(
'delete_submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
@ -115,8 +115,8 @@ class Repository:
) -> tuple[int, dict]:
"""чтение всех блюд"""
response: Response = await ac.get(
reverse_url(
'dishes',
reverse(
'get_dishes',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
@ -129,8 +129,8 @@ class Repository:
) -> tuple[int, dict]:
"""Получение блюда по id"""
response: Response = await ac.get(
reverse_url(
'dish',
reverse(
'get_dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
@ -144,8 +144,8 @@ class Repository:
) -> tuple[int, dict]:
"""создания блюда"""
response: Response = await ac.post(
reverse_url(
'dishes',
reverse(
'create_dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
@ -159,8 +159,8 @@ class Repository:
) -> tuple[int, dict]:
"""Обновление блюда по id"""
response: Response = await ac.patch(
reverse_url(
'dish',
reverse(
'update_dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
@ -178,8 +178,8 @@ class Repository:
) -> int:
"""Удаление блюда по id"""
response: Response = await ac.delete(
reverse_url(
'dish',
reverse(
'delete_dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),

View File

@ -1,25 +1,9 @@
def reverse_url(loc: str, **kwargs) -> str:
menu_pref = '/'
submenu_pref = menu_pref + str(kwargs.get('menu_id', '')) + '/submenus/'
dish_pref = submenu_pref + str(kwargs.get('submenu_id', '')) + '/dishes/'
from fastfood.app import create_app
match loc:
case 'menus':
return menu_pref
app = create_app()
case 'menu':
return menu_pref + str(kwargs.get('menu_id', ''))
case 'submenus':
return submenu_pref
def reverse(loc: str, **kwargs) -> str:
case 'submenu':
return submenu_pref + str(kwargs.get('submenu_id', ''))
case 'dishes':
return dish_pref
case 'dish':
return dish_pref + str(kwargs.get('dish_id', ''))
return menu_pref
url = app.url_path_for(loc, **kwargs)
return url