-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters