Skip to content

Commit

Permalink
adding UUID as supported id type
Browse files Browse the repository at this point in the history
  • Loading branch information
panos-stavrianos committed Feb 28, 2024
1 parent e3e0f84 commit 00eb56b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions py_directus/directus_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Union, Optional, Type, List, Tuple,
overload
)
from uuid import UUID

import json_fix
import websockets
Expand Down Expand Up @@ -152,8 +153,8 @@ def include_count(self):
return self

async def read(
self, id: Optional[Union[int, str]] = None, method: str = "search",
cache: bool = False, as_task: bool = False
self, id: Optional[Union[UUID, int, str]] = None, method: str = "search",
cache: bool = False, as_task: bool = False
) -> DirectusResponse:
"""
Request data.
Expand All @@ -179,8 +180,8 @@ async def read(
return d_response

async def _read(
self, id: Optional[Union[int, str]] = None, method: str = "search",
renew_cache: bool = False, as_task: bool = False
self, id: Optional[Union[UUID, int, str]] = None, method: str = "search",
renew_cache: bool = False, as_task: bool = False
) -> DirectusResponse:
"""
Send query to server.
Expand Down Expand Up @@ -222,7 +223,7 @@ async def _read(
return d_response

async def _read_cache(
self, id: Optional[Union[int, str]] = None, method: str = "search"
self, id: Optional[Union[UUID, int, str]] = None, method: str = "search"
) -> DirectusResponse:
"""
Get response from cache.
Expand All @@ -243,14 +244,14 @@ async def _read_cache(

return d_response

async def clear_cache(self, id: Optional[Union[int, str]] = None, method: str = "search"):
async def clear_cache(self, id: Optional[Union[UUID, int, str]] = None, method: str = "search"):
query_key_str = self._get_query_string_key(id=id, method=method)

# Try to find query in cache
d_res = await self.directus.cache.delete(query_key_str)
return d_res

def _get_query_string_key(self, id: Optional[Union[int, str]] = None, method: str = "search"):
def _get_query_string_key(self, id: Optional[Union[UUID, int, str]] = None, method: str = "search"):
"""
Generate request key for cache.
"""
Expand All @@ -274,15 +275,16 @@ async def create(self, items: Union[dict, List[dict]], as_task: bool = False) ->
return d_response

@overload
async def update(self, ids: Optional[Union[int, str]], items: dict, as_task: bool = False) -> DirectusResponse:
async def update(self, ids: Optional[Union[UUID, int, str]], items: dict,
as_task: bool = False) -> DirectusResponse:
...

@overload
async def update(self, ids: List[Union[int, str]], items: list, as_task: bool = False) -> DirectusResponse:
async def update(self, ids: List[Union[UUID, int, str]], items: list, as_task: bool = False) -> DirectusResponse:
...

async def update(self, ids, items, as_task: bool = False) -> DirectusResponse:
if isinstance(ids, (int, str, None)) and isinstance(items, dict):
if isinstance(ids, (UUID, int, str, None)) and isinstance(items, dict):
if ids is None:
response = self.directus.connection.patch(self.uri, json=items, auth=self.directus.auth)
else:
Expand Down Expand Up @@ -311,8 +313,9 @@ async def update(self, ids, items, as_task: bool = False) -> DirectusResponse:

return d_response

async def delete(self, ids: Union[int, str, List[Union[int, str]]], as_task: bool = False) -> DirectusResponse:
if isinstance(ids, (int, str)):
async def delete(self, ids: Union[UUID, int, str, List[Union[UUID, int, str]]],
as_task: bool = False) -> DirectusResponse:
if isinstance(ids, (UUID, int, str)):
response = self.directus.connection.delete(f'{self.uri}/{ids}', auth=self.directus.auth)
d_response = DirectusResponse(response, collection=self.collection_class)
elif isinstance(ids, list):
Expand All @@ -333,7 +336,7 @@ async def delete(self, ids: Union[int, str, List[Union[int, str]]], as_task: boo
return d_response

async def subscribe(
self, uri: str, event_type: Optional[str] = None, uid: Optional[str] = None
self, uri: str, event_type: Optional[str] = None, uid: Optional[str] = None
) -> Tuple['Data', 'WebSocketClientProtocol']:
"""
Returns authentication confirmation message and the client websocket.
Expand Down

0 comments on commit 00eb56b

Please sign in to comment.