Skip to content

Commit

Permalink
Add ratelimit state model
Browse files Browse the repository at this point in the history
* Add expires to verify key
* Add ratelimit state to verify key
  • Loading branch information
Jonxslays committed Aug 18, 2023
1 parent 04b3d47 commit 9725724
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Changelog

## v0.4.1 (Aug 2023)
## v0.4.2 (Aug 2023)

### Additions

- Add `RatelimitState` model.
- Add `ratelimit` and `expires` fields to `ApiKeyVerification`.

---

## v0.4.1 (Aug 2023)

### Changes

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "unkey.py"
version = "0.4.1"
version = "0.4.2"
description = "An asynchronous Python SDK for unkey.dev."
authors = ["Jonxslays"]
license = "GPL-3.0-only"
Expand Down
3 changes: 2 additions & 1 deletion unkey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Final

__packagename__: Final[str] = "unkey.py"
__version__: Final[str] = "0.4.1"
__version__: Final[str] = "0.4.2"
__author__: Final[str] = "Jonxslays"
__copyright__: Final[str] = "2023-present Jonxslays"
__description__: Final[str] = "An asynchronous Python SDK for unkey.dev."
Expand Down Expand Up @@ -61,6 +61,7 @@
"MissingRequiredArgument",
"Ok",
"Ratelimit",
"RatelimitState",
"RatelimitType",
"Result",
"Route",
Expand Down
1 change: 1 addition & 0 deletions unkey/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"ErrorCode",
"HttpResponse",
"Ratelimit",
"RatelimitState",
"RatelimitType",
)
30 changes: 29 additions & 1 deletion unkey/models/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
from .base import BaseModel
from .http import ErrorCode

__all__ = ("ApiKey", "ApiKeyMeta", "ApiKeyVerification", "Ratelimit", "RatelimitType")
__all__ = (
"ApiKey",
"ApiKeyMeta",
"ApiKeyVerification",
"Ratelimit",
"RatelimitState",
"RatelimitType",
)


class RatelimitType(BaseEnum):
Expand Down Expand Up @@ -107,8 +114,29 @@ class ApiKeyVerification(BaseModel):
be ignored.
"""

expires: t.Optional[int]
"""The unix epoch in milliseconds indicating when this key expires,
if it does."""

ratelimit: t.Optional[RatelimitState]
"""The state of the ratelimit set on this key, if any."""

code: t.Optional[ErrorCode]
"""The optional error code returned by the unkey api."""

error: t.Optional[str]
"""The error message if the key was invalid."""


@attrs.define(init=False, weakref_slot=False)
class RatelimitState(BaseModel):
"""The state of the ratelimit for a given key."""

limit: int
"""The number of burstable requests allowed."""

remaining: int
"""The remaining requests in this burst window."""

reset: int
"""The unix timestamp in milliseconds until the next window."""
10 changes: 9 additions & 1 deletion unkey/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ def to_api_key(self, data: DictT) -> models.ApiKey:

def to_api_key_verification(self, data: DictT) -> models.ApiKeyVerification:
model = models.ApiKeyVerification()
ratelimit = data.get("ratelimit")
model.ratelimit = self.to_ratelimit_state(ratelimit) if ratelimit else ratelimit
model.code = models.ErrorCode.from_str_maybe(data.get("code", ""))
self._set_attrs_cased(
model, data, "valid", "owner_id", "meta", "remaining", "error", maybe=True
model, data, "valid", "owner_id", "meta", "remaining", "error", "expires", maybe=True
)

return model

def to_ratelimit_state(self, data: DictT) -> models.RatelimitState:
model = models.RatelimitState()
self._set_attrs(model, data, "reset", "limit", "remaining")
return model


def to_api(self, data: DictT) -> models.Api:
model = models.Api()
self._set_attrs_cased(model, data, "id", "name", "workspace_id")
Expand Down

0 comments on commit 9725724

Please sign in to comment.