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

Change event cursor to string #8

Merged
merged 4 commits into from
Jan 12, 2024
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
16 changes: 9 additions & 7 deletions upstash_qstash/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,21 @@ async def events(self, req: Optional[EventsRequest] = None) -> GetEventsResponse

The logs endpoint is paginated and returns only 100 logs at a time.
If you want to receive more logs, you can use the cursor to paginate.
The cursor is a unix timestamp with millisecond precision
The cursor is a stringified unix timestamp with millisecond precision

:param req: An instance of EventsRequest containing the cursor
:return: The events response object.

Example:
--------
Initialize the cursor to the current timestamp in milliseconds:
>>> cursor = int(time.time() * 1000)
>>> logs = []
>>> while cursor > 0:
>>> all_events = []
>>> cursor = None
>>> while True:
>>> res = await client.events({"cursor": cursor})
>>> logs.extend(res['events'])
>>> cursor = res.get('cursor', 0)
>>> print(len(res["events"]))
>>> all_events.extend(res["events"])
>>> cursor = res.get("cursor")
>>> if cursor is None:
>>> break
"""
return await Events.get(self.http, req)
14 changes: 13 additions & 1 deletion upstash_qstash/asyncio/dlq.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ async def list_messages(
self, opts: Optional[ListMessagesOpts] = None
) -> ListMessageResponse:
"""
Asynchronously list messages in the dlq
Asynchronously list messages in the dlq.

Example:
--------
>>> dlq = client.dlq()
>>> all_messages = []
>>> cursor = None
>>> while True:
>>> res = await dlq.list_messages({"cursor": cursor})
>>> all_messages.extend(res["messages"])
>>> cursor = res.get("cursor")
>>> if cursor is None:
>>> break
"""
req: UpstashRequest = {
"path": ["v2", "dlq"],
Expand Down
6 changes: 3 additions & 3 deletions upstash_qstash/asyncio/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Optional
from typing import Optional
from upstash_qstash.upstash_http import HttpClient
from upstash_qstash.events import EventsRequest, GetEventsResponse

Expand All @@ -12,8 +12,8 @@ async def get(
Asynchronously retrieve logs.
"""
query = {}
if req is not None and req.get("cursor") is not None and req["cursor"] > 0:
query["cursor"] = str(req["cursor"])
if req is not None and req.get("cursor"):
query["cursor"] = req["cursor"]

return await http.request_async(
{
Expand Down
15 changes: 9 additions & 6 deletions upstash_qstash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ def events(self, req: Optional[EventsRequest] = None) -> GetEventsResponse:

The logs endpoint is paginated and returns only 100 logs at a time.
If you want to receive more logs, you can use the cursor to paginate.
The cursor is a unix timestamp with millisecond precision
The cursor is a stringified unix timestamp with millisecond precision

:param req: An instance of EventsRequest containing the cursor
:return: The events response object.

Example:
--------
Initialize the cursor to the current timestamp in milliseconds:
>>> cursor = int(time.time() * 1000)
>>> logs = []
>>> while cursor > 0:
>>> all_events = []
>>> cursor = None
>>> while True:
>>> res = client.events({"cursor": cursor})
>>> logs.extend(res['events'])
>>> cursor = res.get('cursor', 0)
>>> print(len(res["events"]))
>>> all_events.extend(res["events"])
>>> cursor = res.get("cursor")
>>> if cursor is None:
>>> break
"""
return Events.get(self.http, req)
14 changes: 13 additions & 1 deletion upstash_qstash/dlq.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ def list_messages(
self, opts: Optional[ListMessagesOpts] = None
) -> ListMessageResponse:
"""
List messages in the dlq
List messages in the dlq.

Example:
--------
>>> dlq = client.dlq()
>>> all_messages = []
>>> cursor = None
>>> while True:
>>> res = dlq.list_messages({"cursor": cursor})
>>> all_messages.extend(res["messages"])
>>> cursor = res.get("cursor")
>>> if cursor is None:
>>> break
"""
req: UpstashRequest = {
"path": ["v2", "dlq"],
Expand Down
8 changes: 4 additions & 4 deletions upstash_qstash/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypedDict, Optional, List, Dict
from typing import TypedDict, Optional, List
from enum import Enum
from upstash_qstash.upstash_http import HttpClient

Expand Down Expand Up @@ -29,7 +29,7 @@ class State(Enum):
EventsRequest = TypedDict(
"EventsRequest",
{
"cursor": int,
"cursor": str,
},
)

Expand All @@ -49,8 +49,8 @@ def get(http: HttpClient, req: Optional[EventsRequest] = None) -> GetEventsRespo
Retrieve logs.
"""
query = {}
if req is not None and req.get("cursor") is not None and req["cursor"] > 0:
query["cursor"] = str(req["cursor"])
if req is not None and req.get("cursor") is not None:
query["cursor"] = req["cursor"]

return http.request(
{
Expand Down
Loading