Skip to content

Commit

Permalink
Handle optional before in list metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tribble committed Jul 24, 2024
1 parent 0f3367c commit 9667433
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
22 changes: 7 additions & 15 deletions workos/directory_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient
from workos.utils.pagination_order import PaginationOrder
from workos.utils.request import REQUEST_METHOD_DELETE, REQUEST_METHOD_GET

from workos.utils.validation import DIRECTORY_SYNC_MODULE, validate_settings
from workos.resources.directory_sync import (
DirectoryGroup,
Expand Down Expand Up @@ -51,8 +50,7 @@ def list_users(
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsyncListResource:
...
) -> SyncOrAsyncListResource: ...

def list_groups(
self,
Expand All @@ -62,17 +60,13 @@ def list_groups(
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsyncListResource:
...
) -> SyncOrAsyncListResource: ...

def get_user(self, user: str) -> SyncOrAsync[DirectoryUser]:
...
def get_user(self, user: str) -> SyncOrAsync[DirectoryUser]: ...

def get_group(self, group: str) -> SyncOrAsync[DirectoryGroup]:
...
def get_group(self, group: str) -> SyncOrAsync[DirectoryGroup]: ...

def get_directory(self, directory: str) -> SyncOrAsync[Directory]:
...
def get_directory(self, directory: str) -> SyncOrAsync[Directory]: ...

def list_directories(
self,
Expand All @@ -83,11 +77,9 @@ def list_directories(
after: Optional[str] = None,
organization: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsyncListResource:
...
) -> SyncOrAsyncListResource: ...

def delete_directory(self, directory: str) -> SyncOrAsync[None]:
...
def delete_directory(self, directory: str) -> SyncOrAsync[None]: ...


class DirectorySync(DirectorySyncModule):
Expand Down
16 changes: 4 additions & 12 deletions workos/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from workos.resources.list import (
ListArgs,
ListPage,
WorkOSListResource,
WorkOsListResource,
)

Expand Down Expand Up @@ -38,7 +37,7 @@ def list_events(
) -> SyncOrAsync[EventsListResource]: ...


class Events(EventsModule, WorkOSListResource):
class Events(EventsModule):
"""Offers methods through the WorkOS Events service."""

_http_client: SyncHTTPClient
Expand Down Expand Up @@ -77,9 +76,6 @@ def list_events(
"organization_id": organization,
"range_start": range_start,
"range_end": range_end,
# TODO: This is a hack, and it's wrong. Events does not support before or order
"before": None,
"order": "desc",
}

response = self._http_client.request(
Expand All @@ -88,15 +84,14 @@ def list_events(
params=params,
token=workos.api_key,
)

return WorkOsListResource(
list_method=self.list_events,
list_args=params,
**ListPage[Event](**response).model_dump(),
**ListPage[Event](**response).model_dump(exclude_unset=True),
)


class AsyncEvents(EventsModule, WorkOSListResource):
class AsyncEvents(EventsModule):
"""Offers methods through the WorkOS Events service."""

_http_client: AsyncHTTPClient
Expand Down Expand Up @@ -134,9 +129,6 @@ async def list_events(
"organization_id": organization_id,
"range_start": range_start,
"range_end": range_end,
# TODO: THis is wrong, Events does not support before or order
"before": None,
"order": "desc",
}

response = await self._http_client.request(
Expand All @@ -149,5 +141,5 @@ async def list_events(
return WorkOsListResource(
list_method=self.list_events,
list_args=params,
**ListPage[Event](**response).model_dump(),
**ListPage[Event](**response).model_dump(exclude_unset=True),
)
34 changes: 23 additions & 11 deletions workos/resources/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
from typing import (
AsyncIterator,
Awaitable,
Dict,
List,
Literal,
Required,
TypeVar,
Generic,
Callable,
Iterator,
Optional,
Union,
cast,
)
from typing_extensions import TypedDict
from workos.resources.base import WorkOSBaseResource
Expand Down Expand Up @@ -122,8 +125,11 @@ def auto_paging_iter(self):
)


class ListMetadata(BaseModel):
class ListAfterMetadata(BaseModel):
after: Optional[str] = None


class ListMetadata(ListAfterMetadata):
before: Optional[str] = None


Expand All @@ -133,14 +139,15 @@ class ListPage(WorkOSModel, Generic[ListableResource]):
list_metadata: ListMetadata


class ListArgs(TypedDict):
limit: int
class ListArgs(TypedDict, total=False):
before: Optional[str]
after: Optional[str]
order: Literal["asc", "desc"]
limit: Required[int]
order: Optional[Literal["asc", "desc"]]


ListAndFilterParams = TypeVar("ListAndFilterParams", bound=ListArgs)
ListMetadataType = TypeVar("ListMetadataType", ListAfterMetadata, ListMetadata)


class BaseWorkOsListResource(
Expand All @@ -149,16 +156,22 @@ class BaseWorkOsListResource(
):
object: Literal["list"]
data: List[ListableResource]
list_metadata: ListMetadata
list_metadata: Union[ListAfterMetadata, ListMetadata]

list_method: Callable = Field(exclude=True)
list_args: ListAndFilterParams = Field(exclude=True)

def _parse_params(self):
fixed_pagination_params = {
"order": self.list_args["order"],
"limit": self.list_args["limit"],
}
fixed_pagination_params = cast(
# Type hints consider this a mismatch because it assume the dictionary is dict[str, int]
Dict[str, Union[int, str, None]],
{
"limit": self.list_args["limit"],
},
)
if "order" in self.list_args:
fixed_pagination_params["order"] = self.list_args["order"]

# Omit common list parameters
filter_params = {
k: v
Expand All @@ -171,8 +184,7 @@ def _parse_params(self):
@abc.abstractmethod
def auto_paging_iter(
self,
) -> Union[AsyncIterator[ListableResource], Iterator[ListableResource]]:
...
) -> Union[AsyncIterator[ListableResource], Iterator[ListableResource]]: ...


class WorkOsListResource(
Expand Down

0 comments on commit 9667433

Please sign in to comment.