Skip to content

Commit 82efd3e

Browse files
committed
Move specific models from Crawlee to SDK
1 parent 770394b commit 82efd3e

File tree

3 files changed

+93
-14
lines changed

3 files changed

+93
-14
lines changed

src/apify/apify_storage_client/_key_value_store_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99

1010
from apify_client import ApifyClientAsync
1111
from crawlee.storage_clients._base import KeyValueStoreClient
12-
from crawlee.storage_clients.models import (
13-
KeyValueStoreListKeysPage,
14-
KeyValueStoreMetadata,
15-
KeyValueStoreRecord,
16-
KeyValueStoreRecordMetadata,
17-
)
12+
from crawlee.storage_clients.models import KeyValueStoreMetadata, KeyValueStoreRecord, KeyValueStoreRecordMetadata
13+
from ._models import KeyValueStoreListKeysPage
1814

1915
from apify._crypto import create_hmac_signature
2016

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from __future__ import annotations
2+
3+
from datetime import datetime, timedelta
4+
from typing import Annotated
5+
6+
from pydantic import BaseModel, ConfigDict, Field
7+
8+
from crawlee import Request
9+
from crawlee._utils.docs import docs_group
10+
11+
12+
@docs_group('Data structures')
13+
class ProlongRequestLockResponse(BaseModel):
14+
"""Response to prolong request lock calls."""
15+
16+
model_config = ConfigDict(populate_by_name=True)
17+
18+
lock_expires_at: Annotated[datetime, Field(alias='lockExpiresAt')]
19+
20+
21+
@docs_group('Data structures')
22+
class RequestQueueHead(BaseModel):
23+
"""Model for request queue head.
24+
25+
Represents a collection of requests retrieved from the beginning of a queue,
26+
including metadata about the queue's state and lock information for the requests.
27+
"""
28+
29+
model_config = ConfigDict(populate_by_name=True)
30+
31+
limit: Annotated[int | None, Field(alias='limit', default=None)]
32+
"""The maximum number of requests that were requested from the queue."""
33+
34+
had_multiple_clients: Annotated[bool, Field(alias='hadMultipleClients', default=False)]
35+
"""Indicates whether the queue has been accessed by multiple clients (consumers)."""
36+
37+
queue_modified_at: Annotated[datetime, Field(alias='queueModifiedAt')]
38+
"""The timestamp when the queue was last modified."""
39+
40+
lock_time: Annotated[timedelta | None, Field(alias='lockSecs', default=None)]
41+
"""The duration for which the returned requests are locked and cannot be processed by other clients."""
42+
43+
queue_has_locked_requests: Annotated[bool | None, Field(alias='queueHasLockedRequests', default=False)]
44+
"""Indicates whether the queue contains any locked requests."""
45+
46+
items: Annotated[list[Request], Field(alias='items', default_factory=list[Request])]
47+
"""The list of request objects retrieved from the beginning of the queue."""
48+
49+
50+
class KeyValueStoreKeyInfo(BaseModel):
51+
"""Model for a key-value store key info."""
52+
53+
model_config = ConfigDict(populate_by_name=True)
54+
55+
key: Annotated[str, Field(alias='key')]
56+
size: Annotated[int, Field(alias='size')]
57+
58+
59+
class KeyValueStoreListKeysPage(BaseModel):
60+
"""Model for listing keys in the key-value store."""
61+
62+
model_config = ConfigDict(populate_by_name=True)
63+
64+
count: Annotated[int, Field(alias='count')]
65+
limit: Annotated[int, Field(alias='limit')]
66+
is_truncated: Annotated[bool, Field(alias='isTruncated')]
67+
items: Annotated[list[KeyValueStoreKeyInfo], Field(alias='items', default_factory=list)]
68+
exclusive_start_key: Annotated[str | None, Field(alias='exclusiveStartKey', default=None)]
69+
next_exclusive_start_key: Annotated[str | None, Field(alias='nextExclusiveStartKey', default=None)]
70+
71+
72+
class CachedRequest(BaseModel):
73+
"""Pydantic model for cached request information."""
74+
75+
id: str
76+
"""The ID of the request."""
77+
78+
was_already_handled: bool
79+
"""Whether the request was already handled."""
80+
81+
hydrated: Request | None = None
82+
"""The hydrated request object (the original one)."""
83+
84+
lock_expires_at: datetime | None = None
85+
"""The expiration time of the lock on the request."""
86+
87+
forefront: bool = False
88+
"""Whether the request was added to the forefront of the queue."""

src/apify/apify_storage_client/_request_queue_client.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
from crawlee import Request
1414
from crawlee._utils.requests import unique_key_to_request_id
1515
from crawlee.storage_clients._base import RequestQueueClient
16-
from crawlee.storage_clients.models import (
17-
AddRequestsResponse,
18-
CachedRequest,
19-
ProcessedRequest,
20-
ProlongRequestLockResponse,
21-
RequestQueueHead,
22-
RequestQueueMetadata,
23-
)
16+
from crawlee.storage_clients.models import AddRequestsResponse, ProcessedRequest, RequestQueueMetadata
17+
18+
from ._models import CachedRequest, ProlongRequestLockResponse, RequestQueueHead
2419

2520
if TYPE_CHECKING:
2621
from collections.abc import Sequence

0 commit comments

Comments
 (0)