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), pool: redis.Redis = Depends(get_redis_pool),
) -> None: ) -> None:
self.pool = pool self.pool = pool
self.ttl = 1800 self.ttl = 2
async def get(self, key: str) -> Any | None: async def get(self, key: str) -> Any | None:
data = await self.pool.get(key) data = await self.pool.get(key)

View File

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

View File

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

View File

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