1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! Client for a single key-value store (`/v2/key-value-stores/{storeId}` and variants).
use serde::Serialize;
use crate::clients::base::{
delete_resource, get_raw, get_resource, get_resource_required, head_exists, put_raw,
update_resource, ResourceContext,
};
use crate::common::{
create_hmac_signature, encode_path_segment, sign_storage_content, QueryParams,
};
use crate::error::ApifyClientResult;
use crate::http_client::{HttpClient, HttpMethod, HttpRequest};
use crate::models::{KeyValueStore, KeyValueStoreKeysPage, KeyValueStoreRecord};
/// Options for listing keys in a key-value store.
#[derive(Debug, Default, Clone)]
pub struct ListKeysOptions {
/// Maximum number of keys to return.
pub limit: Option<i64>,
/// Start listing after this key (exclusive), for pagination.
pub exclusive_start_key: Option<String>,
/// Only return keys with this prefix.
pub prefix: Option<String>,
/// Only return keys belonging to this collection.
pub collection: Option<String>,
/// URL-signing signature granting access to a private store's key listing.
pub signature: Option<String>,
}
/// Options for downloading all records as a ZIP archive via
/// [`KeyValueStoreClient::get_records`].
///
/// Covers the spec query parameters of `GET /v2/key-value-stores/{storeId}/records`.
#[derive(Debug, Default, Clone)]
pub struct GetRecordsOptions {
/// Only include records belonging to this collection from the store schema.
pub collection: Option<String>,
/// Only include records whose key starts with this prefix.
pub prefix: Option<String>,
/// URL-signing signature granting access to a private store's records.
pub signature: Option<String>,
}
/// Options for reading a single record via [`KeyValueStoreClient::get_record_with_options`].
///
/// Covers the spec query parameters of
/// `GET /v2/key-value-stores/{storeId}/records/{recordKey}`.
#[derive(Debug, Default, Clone)]
pub struct GetRecordOptions {
/// Request the record with a `Content-Disposition: attachment` response header. When unset,
/// the client sends `attachment=true`, matching the reference client's unconditional default.
pub attachment: Option<bool>,
/// URL-signing signature granting access to a record in a private store.
pub signature: Option<String>,
}
/// Client for a specific key-value store.
#[derive(Debug, Clone)]
pub struct KeyValueStoreClient {
ctx: ResourceContext,
}
impl KeyValueStoreClient {
pub(crate) fn new(http: HttpClient, base_url: &str, resource_path: &str, id: &str) -> Self {
Self {
ctx: ResourceContext::single(http, base_url, resource_path, id),
}
}
/// Creates a KVS client for a run's default store (nested path, no ID).
pub(crate) fn nested(http: HttpClient, base_url: &str, sub_path: &str) -> Self {
Self {
ctx: ResourceContext::collection(http, base_url, sub_path),
}
}
/// Sets the public origin used when building shareable URLs.
pub(crate) fn with_public_base(mut self, public_base_url: &str) -> Self {
self.ctx = self.ctx.with_public_origin(public_base_url);
self
}
/// Fetches the store metadata, or `None` if it does not exist.
pub async fn get(&self) -> ApifyClientResult<Option<KeyValueStore>> {
get_resource(&self.ctx, None, &QueryParams::new()).await
}
/// Updates the store metadata (e.g. `name`, `title`).
pub async fn update<T: Serialize>(&self, new_fields: &T) -> ApifyClientResult<KeyValueStore> {
update_resource(&self.ctx, None, new_fields).await
}
/// Deletes the store.
pub async fn delete(&self) -> ApifyClientResult<()> {
delete_resource(&self.ctx, None).await
}
/// Lists the keys in the store (key-based pagination).
pub async fn list_keys(
&self,
options: ListKeysOptions,
) -> ApifyClientResult<KeyValueStoreKeysPage> {
let mut params = QueryParams::new();
params
.add_int("limit", options.limit)
.add_str("exclusiveStartKey", options.exclusive_start_key)
.add_str("prefix", options.prefix)
.add_str("collection", options.collection)
.add_str("signature", options.signature);
get_resource_required(&self.ctx, Some("keys"), ¶ms).await
}
/// Downloads all records from the store as a ZIP archive (raw bytes).
///
/// Each record is stored as a separate file in the archive, with the filename equal to the
/// record key. Use [`GetRecordsOptions`] to filter by `collection` or `prefix`, or to pass a
/// URL-signing `signature` for a private store. Wraps
/// `GET /v2/key-value-stores/{storeId}/records`.
pub async fn get_records(&self, options: GetRecordsOptions) -> ApifyClientResult<Vec<u8>> {
let mut params = QueryParams::new();
params
.add_str("collection", options.collection)
.add_str("prefix", options.prefix)
.add_str("signature", options.signature);
let url = self
.ctx
.merged_params(¶ms)
.apply_to_url(&self.ctx.url(Some("records")));
let response = self
.ctx
.http
.call(HttpRequest {
method: HttpMethod::Get,
url,
headers: Default::default(),
body: None,
timeout: crate::clients::base::DEFAULT_REQUEST_TIMEOUT,
})
.await?;
Ok(response.body)
}
/// Returns `true` if a record with the given key exists.
pub async fn record_exists(&self, key: &str) -> ApifyClientResult<bool> {
head_exists(
&self.ctx,
Some(&format!("records/{}", encode_path_segment(key))),
&QueryParams::new(),
)
.await
}
/// Gets a record's raw value (and content type), or `None` if it does not exist.
///
/// Like the reference client's `getRecord`, this sends `attachment=true`. Use
/// [`get_record_with_options`](Self::get_record_with_options) to override the attachment
/// behaviour or to pass a URL-signing `signature` for a private store.
pub async fn get_record(&self, key: &str) -> ApifyClientResult<Option<KeyValueStoreRecord>> {
self.get_record_with_options(key, GetRecordOptions::default())
.await
}
/// Gets a record with explicit options.
///
/// The `attachment` option controls the `Content-Disposition: attachment` response header;
/// when unset it defaults to `true`, matching the reference client's unconditional behaviour.
/// `signature` supplies a URL-signing signature for accessing a record in a private store.
pub async fn get_record_with_options(
&self,
key: &str,
options: GetRecordOptions,
) -> ApifyClientResult<Option<KeyValueStoreRecord>> {
let mut params = QueryParams::new();
// Default to `attachment=true` (reference parity); honour an explicit override.
params
.add_bool("attachment", Some(options.attachment.unwrap_or(true)))
.add_str("signature", options.signature);
let response = get_raw(
&self.ctx,
Some(&format!("records/{}", encode_path_segment(key))),
¶ms,
)
.await?;
Ok(response.map(|r| {
let content_type = r.header("content-type").map(|s| s.to_string());
KeyValueStoreRecord {
key: key.to_string(),
value: r.body,
content_type,
}
}))
}
/// Stores a record with raw bytes and an explicit content type.
pub async fn set_record_raw(
&self,
key: &str,
value: Vec<u8>,
content_type: &str,
) -> ApifyClientResult<()> {
put_raw(
&self.ctx,
Some(&format!("records/{}", encode_path_segment(key))),
&QueryParams::new(),
value,
content_type,
)
.await
}
/// Stores a record as JSON (the value is serialized and content type set to JSON).
pub async fn set_record_json<T: Serialize>(
&self,
key: &str,
value: &T,
) -> ApifyClientResult<()> {
let bytes = serde_json::to_vec(value)?;
self.set_record_raw(key, bytes, "application/json; charset=utf-8")
.await
}
/// Builds a public URL for reading the record with the given key.
///
/// Mirrors the reference client's `getRecordPublicUrl`: it fetches the store, and if the
/// store exposes a URL-signing secret key (private store), appends an HMAC-SHA256
/// `signature` over the record key so the URL works without an API token. The URL is
/// built from the configured public base URL.
pub async fn get_record_public_url(&self, key: &str) -> ApifyClientResult<String> {
let mut params = QueryParams::new();
if let Some(store) = self.get().await? {
if let Some(secret) = store
.extra
.get("urlSigningSecretKey")
.and_then(|v| v.as_str())
{
params.add_str("signature", Some(create_hmac_signature(secret, key)));
}
}
Ok(params.apply_to_url(
&self
.ctx
.public_url(Some(&format!("records/{}", encode_path_segment(key)))),
))
}
/// Builds a public URL for listing this store's keys.
///
/// Like [`get_record_public_url`](Self::get_record_public_url), signs the URL with an
/// HMAC-SHA256 `signature` for private stores. `expires_in_secs` optionally bounds a
/// signed URL's validity.
pub async fn create_keys_public_url(
&self,
expires_in_secs: Option<i64>,
) -> ApifyClientResult<String> {
let mut params = QueryParams::new();
if let Some(store) = self.get().await? {
if let Some(secret) = store
.extra
.get("urlSigningSecretKey")
.and_then(|v| v.as_str())
{
let signature = sign_storage_content(secret, &store.id, expires_in_secs);
params.add_str("signature", Some(signature));
}
}
Ok(params.apply_to_url(&self.ctx.public_url(Some("keys"))))
}
/// Deletes the record with the given key.
pub async fn delete_record(&self, key: &str) -> ApifyClientResult<()> {
let url = self
.ctx
.url(Some(&format!("records/{}", encode_path_segment(key))));
self.ctx
.http
.call(HttpRequest {
method: HttpMethod::Delete,
url,
headers: Default::default(),
body: None,
timeout: crate::clients::base::DEFAULT_REQUEST_TIMEOUT,
})
.await?;
Ok(())
}
}