Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api key limits #25

Merged
merged 2 commits into from
Dec 15, 2023
Merged
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
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release History
===============

1.1.0 (2023-12-08)
------------------

- Add api-key limits

1.0.8 (2023-12-06)
------------------

Expand Down
2 changes: 1 addition & 1 deletion cybsi/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.8"
__version__ = "1.1.0"
__title__ = "cybsi-cloud-sdk"
__description__ = "Cybsi Cloud development kit"
__license__ = "Apache License 2.0"
Expand Down
6 changes: 6 additions & 0 deletions cybsi/cloud/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
APIKeyForm,
APIKeyView,
)
from .limits import (
RequestLimitForm,
RequestLimitTargetView,
RequestLimitView,
LimitPeriod,
)
from .permission import (
ResourceAction,
ResourcePermissionView,
Expand Down
10 changes: 10 additions & 0 deletions cybsi/cloud/auth/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
rfc3339_timestamp,
)
from ..pagination import Cursor, Page
from .limits import RequestLimitForm, RequestLimitView
from .permission import ResourcePermissionForm, ResourcePermissionView
from .token import TokenView

Expand Down Expand Up @@ -196,12 +197,14 @@ class APIKeyForm(JsonObjectForm):
expires_at: Expiration date.
The API-Key is automatically disabled after the expiration date.
description: API-Key description.
request_limits: List of API-Key request limits.
"""

def __init__(
self,
permissions: Iterable[ResourcePermissionForm],
*,
request_limits: Optional[Iterable[RequestLimitForm]] = None,
expires_at: Optional[datetime] = None,
description: Optional[str] = None,
):
Expand All @@ -210,6 +213,8 @@ def __init__(
self._data["expiresAt"] = rfc3339_timestamp(expires_at)
if description is not None:
self._data["description"] = description
if request_limits is not None:
self._data["requestLimits"] = [limit.json() for limit in request_limits]
self._data["permissions"] = [perm.json() for perm in permissions]


Expand Down Expand Up @@ -268,3 +273,8 @@ def revoked(self) -> bool:
def permissions(self) -> List[ResourcePermissionView]:
"""List of permissions."""
return [ResourcePermissionView(perm) for perm in self._get("permissions")]

@property
def request_limits(self) -> List[RequestLimitView]:
"""List of request limits."""
return [RequestLimitView(limit) for limit in self._get("requestLimits")]
71 changes: 71 additions & 0 deletions cybsi/cloud/auth/limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from enum_tools import document_enum

from ..enum import CybsiAPIEnum
from ..internal import JsonObjectForm, JsonObjectView
from .permission import ResourceAction
from .resource import ResourceRefView


@document_enum
class LimitPeriod(CybsiAPIEnum):
"""Limit time window."""

Day = "Day"
"""Time window with period of one day."""


class RequestLimitTargetView(JsonObjectView):
"""Request limit target."""

@property
def resource(self) -> ResourceRefView:
"""Resource."""
return ResourceRefView(self._get("resource"))

@property
def action(self) -> ResourceAction:
"""Limited action."""
return ResourceAction(self._get("action"))


class RequestLimitView(JsonObjectView):
"""Request limit."""

@property
def target(self) -> RequestLimitTargetView:
"""Limit target."""
return RequestLimitTargetView(self._get("target"))

@property
def limit(self) -> int:
"""Maximum requests count within time window."""
return self._get("limit")

@property
def period(self) -> LimitPeriod:
"""Time window for limit."""
return LimitPeriod(self._get("period"))


class RequestLimitForm(JsonObjectForm):
"""Request limit form.

Args:
resource_id: resource identifier.
action: limited action.
limit_period: time window for limit.
limit: maximum requests count within time window.
"""

def __init__(
self,
*,
resource_id: int,
action: ResourceAction,
limit_period: LimitPeriod,
limit: int,
):
super().__init__()
self._data["target"] = {"resource": {"id": resource_id}, "action": action.value}
self._data["limit"] = limit
self._data["period"] = limit_period.value
4 changes: 4 additions & 0 deletions cybsi/cloud/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class SemanticErrorCodes(CybsiAPIEnum):
"""schemaID parameter can't be changed."""
SchemaNotFound = "SchemaNotFound"
"""The specified schema is not found"""
InvalidRequestLimit = "InvalidRequestLimit"
"""The specified request limit cannot be set."""
LimitSetConflict = "LimitSetConflict"
"""The limit set has conflicts."""

# Objects
InvalidKeyFormat = "InvalidKeyFormat"
Expand Down
3 changes: 1 addition & 2 deletions examples/get_collection_objects_chained.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

# Retrieve collection schema, it describes all attributes
# of objects you can encounter in the collection.
schema_view = client.iocean.collections.view_schema(
collection_id=collection_id)
schema_view = client.iocean.collections.view_schema(collection_id=collection_id)
print(schema_view.schema)

# Retrieve first page of collection objects.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cybsi-cloud-sdk"
version = "1.0.8"
version = "1.1.0"
description = "Cybsi Cloud development kit"
authors = ["Cybsi Cloud developers"]
license = "Apache License 2.0"
Expand Down Expand Up @@ -36,7 +36,7 @@ extend_skip = ["__init__.py"]
[tool.tbump]

[tool.tbump.version]
current = "1.0.8"
current = "1.1.0"

regex = '''
^
Expand Down