Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/workos/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@
import sys
from datetime import datetime
from enum import Enum
from typing import Any, Dict, NoReturn, Protocol, TypedDict, TypeVar
from typing import Any, Dict, Literal, NoReturn, Protocol, TypedDict, TypeVar

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class NotGiven:
"""Sentinel used as the default for nullable optional parameters.

Distinguishes an omitted argument ("leave unchanged", not sent) from an
explicit ``None``, which clears the field by sending JSON ``null``.
Falsy so ``if not param`` reads naturally.
"""

def __bool__(self) -> Literal[False]:
return False

def __repr__(self) -> str:
return "NOT_GIVEN"


NOT_GIVEN = NotGiven()


class RequestOptions(TypedDict, total=False):
"""Per-request options that can be passed to any API method."""

Expand Down
26 changes: 9 additions & 17 deletions src/workos/api_keys/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if TYPE_CHECKING:
from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions, enum_value
from .._types import RequestOptions, enum_value, NOT_GIVEN, NotGiven
from .models import (
ApiKey,
ApiKeyValidationResponse,
Expand Down Expand Up @@ -184,7 +184,7 @@ def create_api_key_expire(
self,
id: str,
*,
expires_at: Optional[str] = None,
expires_at: Union[str, None, NotGiven] = NOT_GIVEN,
request_options: Optional[RequestOptions] = None,
Comment thread
greptile-apps[bot] marked this conversation as resolved.
) -> ApiKey:
"""Expire an API key
Expand All @@ -207,13 +207,9 @@ def create_api_key_expire(
RateLimitExceededError: If rate limited (429).
ServerError: If the server returns a 5xx error.
"""
body: Dict[str, Any] = {
k: v
for k, v in {
"expires_at": expires_at,
}.items()
if v is not None
}
body: Dict[str, Any] = {}
if expires_at is not NOT_GIVEN:
body["expires_at"] = expires_at
return self._client.request(
method="post",
path=("api_keys", str(id), "expire"),
Expand Down Expand Up @@ -389,7 +385,7 @@ async def create_api_key_expire(
self,
id: str,
*,
expires_at: Optional[str] = None,
expires_at: Union[str, None, NotGiven] = NOT_GIVEN,
request_options: Optional[RequestOptions] = None,
) -> ApiKey:
"""Expire an API key
Expand All @@ -412,13 +408,9 @@ async def create_api_key_expire(
RateLimitExceededError: If rate limited (429).
ServerError: If the server returns a 5xx error.
"""
body: Dict[str, Any] = {
k: v
for k, v in {
"expires_at": expires_at,
}.items()
if v is not None
}
body: Dict[str, Any] = {}
if expires_at is not NOT_GIVEN:
body["expires_at"] = expires_at
return await self._client.request(
method="post",
path=("api_keys", str(id), "expire"),
Expand Down
Loading
Loading