Skip to content
Open
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
19 changes: 13 additions & 6 deletions nylas/models/grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,30 @@ class ListGrantsQueryParams(TypedDict):
limit: The maximum number of objects to return.
This field defaults to 10. The maximum allowed value is 200.
offset: Offset grant results by this number.
sortBy: Sort entries by field name
orderBy: Specify ascending or descending order.
sort_by: Sort entries by field name.
order_by: Specify ascending or descending order.
since: Scope grants from a specific point in time by Unix timestamp.
before: Scope grants to a specific point in time by Unix timestamp.
email: Filtering your query based on grant email address (if applicable)
grantStatus: Filtering your query based on grant email status (if applicable)
grant_status: Filtering your query based on grant email status (if applicable)
ip: Filtering your query based on grant IP address
provider: Filtering your query based on OAuth provider
sortBy: Deprecated camelCase alias for sort_by.
orderBy: Deprecated camelCase alias for order_by.
grantStatus: Deprecated camelCase alias for grant_status.
"""

limit: NotRequired[int]
offset: NotRequired[int]
sortBy: NotRequired[str]
orderBy: NotRequired[str]
sort_by: NotRequired[str]
order_by: NotRequired[str]
since: NotRequired[int]
before: NotRequired[int]
email: NotRequired[str]
grantStatus: NotRequired[str]
grant_status: NotRequired[str]
ip: NotRequired[str]
provider: NotRequired[Provider]
# Backward-compatible aliases for callers still passing camelCase keys.
sortBy: NotRequired[str]
orderBy: NotRequired[str]
grantStatus: NotRequired[str]
24 changes: 23 additions & 1 deletion nylas/resources/grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
from nylas.models.response import Response, ListResponse, DeleteResponse


def _normalize_grants_query_params(query_params: ListGrantsQueryParams = None) -> dict:
if not query_params:
return query_params

normalized_query_params = dict(query_params)
key_aliases = {
"sortBy": "sort_by",
"orderBy": "order_by",
"grantStatus": "grant_status",
}

for camel_case_key, snake_case_key in key_aliases.items():
if camel_case_key in normalized_query_params:
if snake_case_key not in normalized_query_params:
normalized_query_params[snake_case_key] = normalized_query_params[
camel_case_key
]
del normalized_query_params[camel_case_key]

return normalized_query_params


class Grants(
ListableApiResource,
FindableApiResource,
Expand Down Expand Up @@ -47,7 +69,7 @@ def list(
return super().list(
path="/v3/grants",
response_type=Grant,
query_params=query_params,
query_params=_normalize_grants_query_params(query_params),
overrides=overrides,
)

Expand Down
55 changes: 55 additions & 0 deletions tests/resources/test_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,61 @@ def test_list_grants(self, http_client_list_response):
"GET", "/v3/grants", None, None, None, overrides=None
)

def test_list_grants_normalizes_camel_case_query_params(
self, http_client_list_response
):
grants = Grants(http_client_list_response)

grants.list(
query_params={
"sortBy": "created_at",
"orderBy": "asc",
"grantStatus": "valid",
"limit": 10,
}
)

http_client_list_response._execute.assert_called_once_with(
"GET",
"/v3/grants",
None,
{
"sort_by": "created_at",
"order_by": "asc",
"grant_status": "valid",
"limit": 10,
},
None,
overrides=None,
)

def test_list_grants_prefers_snake_case_query_params(self, http_client_list_response):
grants = Grants(http_client_list_response)

grants.list(
query_params={
"sortBy": "updated_at",
"sort_by": "created_at",
"orderBy": "desc",
"order_by": "asc",
"grantStatus": "invalid",
"grant_status": "valid",
}
)

http_client_list_response._execute.assert_called_once_with(
"GET",
"/v3/grants",
None,
{
"sort_by": "created_at",
"order_by": "asc",
"grant_status": "valid",
},
None,
overrides=None,
)

def test_find_grant(self, http_client_response):
grants = Grants(http_client_response)

Expand Down
Loading