apify-client 0.2.0

Typed wrapper for Apify API
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::marker::PhantomData;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::client::{ApifyClient, ApifyApiError, ApifyClientError, ApifyClientResult, IdOrName};
use crate::utils::{stringify_resource, json_content_headers, parse_pagination_header, is_resource_by_name};
use crate::generic_types::{SimpleBuilder, PaginationList, NoContent};

pub const BASE_URL: &str = "https://api.apify.com/v2/datasets";

impl ApifyClient {
    /// List datasets of the provided account.
    /// Requires API token.
    pub fn list_datasets(&self) -> ListDatasetsBuilder<'_> {
        ListDatasetsBuilder {
            client: self,
            options: ListDatasetsParams::default(),
        }
    }

    /// Requires API token
    pub fn create_dataset(&self, dataset_name: &str) -> SimpleBuilder<'_, Dataset> {
        let url = format!("{}?name={}", BASE_URL, dataset_name);
        SimpleBuilder {
            client: self,
            requires_token: true,
            url,
            method: reqwest::Method::POST,
            body: Ok(None),
            headers: None,
            phantom: PhantomData,
        }
    }

    /// Gets a dataset info object
    /// If you provide dataset ID, you don't need a token
    /// If you provide username~datasetName, you need a token (otherwise it will return an Error)
    pub fn get_dataset(&self, dataset_id_or_name: &IdOrName) -> SimpleBuilder<'_, Dataset> {
        let url = format!("{}/{}?", BASE_URL, stringify_resource(dataset_id_or_name));
    
        SimpleBuilder {
            client: self,
            url,
            requires_token: is_resource_by_name(dataset_id_or_name),
            method: reqwest::Method::GET,
            body: Ok(None),
            headers: None,
            phantom: PhantomData,
        }
    }

    /// Requires API token
    pub fn update_dataset(&self, dataset_id_or_name: &IdOrName, new_dataset_name: &str) -> SimpleBuilder<'_, Dataset> {
        let url = format!("{}/{}?", BASE_URL, stringify_resource(dataset_id_or_name));
        let json_body = json!({
            "name": new_dataset_name
        });
        let bytes = serde_json::to_vec(&json_body).expect("Parsing just defined JSON should never fail!"); 
        SimpleBuilder {
            client: self,
            url,
            requires_token: true,
            method: reqwest::Method::PUT,
            body: Ok(Some(bytes)),
            headers: Some(json_content_headers()),
            phantom: PhantomData,
        }
    }

    /// Requires API token
    pub fn delete_dataset(&self, dataset_id_or_name: &IdOrName) -> SimpleBuilder<'_, NoContent> {
        let url = format!("{}/{}?", BASE_URL, stringify_resource(dataset_id_or_name));
        SimpleBuilder {
            client: self,
            url,
            requires_token: true,
            method: reqwest::Method::DELETE,
            body: Ok(None),
            headers: None,
            phantom: PhantomData,
        }
    }

    /// Appends item(s) at the end of the dataset.
    /// `items` must serialize into JSON object or array of objects and the JSON must have size less than 5 MB.
    /// Otherwise the Apify API returns an error.
    /// Requires API token.
    /// [API reference](https://docs.apify.com/api/v2#/reference/datasets/item-collection/put-items)
    pub fn put_items<T: Serialize>(&self, dataset_id_or_name: &IdOrName, items: &T) -> SimpleBuilder<'_, NoContent> {
        let url = format!("{}/{}/items?", BASE_URL, stringify_resource(dataset_id_or_name));
        let wrapped_bytes = Some(serde_json::to_vec(items)).transpose();
        
        SimpleBuilder {
            client: self,
            url,
            requires_token: true,
            method: reqwest::Method::POST,
            body: wrapped_bytes,
            headers: Some(json_content_headers()),
            phantom: PhantomData,
        }
    }

    /// Gets items from the dataset in JSON format and parses them into `PaginationList<T>`.
    /// If you need non-parsed String and/or different formats choose `get_items_raw` instead.
    /// [API reference](https://docs.apify.com/api/v2#/reference/datasets/item-collection/get-items).
    pub fn get_items<T: serde::de::DeserializeOwned>(&self, dataset_id_or_name: IdOrName) -> GetItemsBuilder<'_, T> {
        GetItemsBuilder {
            client: self,
            dataset_id_or_name,
            options: GetItemsParams::default(),
            _phantom: PhantomData,
        }
    }

    /// Gets items from the dataset in any format and return them as `String` (no PaginationList). 
    /// [API reference](https://docs.apify.com/api/v2#/reference/datasets/item-collection/get-items).
    pub fn get_items_raw(&self, dataset_id_or_name: IdOrName) -> GetItemsBuilderRaw<'_> {
        GetItemsBuilderRaw {
            client: self,
            dataset_id_or_name,
            options: GetItemsParams::default(),
        }
    }
}

fn get_items_prepare_url(client: &ApifyClient, dataset_id_or_name: &IdOrName, params: &GetItemsParams) -> Result<String, ApifyClientError> {
    let url = format!("{}/{}/items?{}", BASE_URL, stringify_resource(&dataset_id_or_name), params.to_query_params());
    let url = if is_resource_by_name(dataset_id_or_name) {
        let token = client.optional_token.as_ref().ok_or(ApifyApiError::MissingToken)?;
        format!("{}&token={}", &url, token)
    } else {
        url
    };
    Ok(url)
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Dataset {
    pub id: String,
    pub name: Option<String>,
    pub user_id: String,
    pub created_at: String,
    pub modified_at: String,
    pub accessed_at: String,
    pub item_count: u32,
    pub clean_item_count: Option<u32>,
    pub act_id: Option<String>,
    pub act_run_id: Option<String>
}

#[derive(Debug)]
pub enum Format {
    Json,
    Jsonl,
    Xml,
    Html,
    Csv,
    Xlsx,
    Rss,
}

impl Default for Format {
    fn default() -> Self {
        Format::Json
    }
}

impl std::fmt::Display for Format {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        let string_repr = match self {
            Format::Json => "json",
            Format::Jsonl => "jsonl",
            Format::Xml => "xml",
            Format::Html => "html",
            Format::Csv => "csv",
            Format::Xlsx => "xlsx",
            Format::Rss => "ss",
        };
        write!(f, "{}", string_repr)
    }
}

#[derive(Default, QueryParams)]
#[allow(non_snake_case)]
struct GetItemsParams {
    format: Format,
    clean: Option<bool>,
    offset: Option<u64>,
    limit: Option<u64>,
    // Just string so QueryParams work, we parse it ourselves
    fields: Option<String>,
    // Just string so QueryParams work, we parse it ourselves
    omit: Option<String>,
    unwind: Option<String>,
    desc: Option<bool>, 
    attachment: Option<bool>,
    delimiter: Option<String>,
    bom: Option<bool>,
    xmlRoot: Option<String>,
    xmlRow: Option<String>,
    skipHeaderRow: Option<bool>,
    skipHidden: Option<bool>, 
    skipEmpty: Option<bool>, 
    simplified: Option<bool>,
    skipFailedPages: Option<bool>,
}

pub struct GetItemsBuilder<'a, T> {
    client: &'a ApifyClient,
    dataset_id_or_name: IdOrName,
    options: GetItemsParams,
    _phantom: PhantomData<T>,
}

pub struct GetItemsBuilderRaw<'a> {
    client: &'a ApifyClient,
    dataset_id_or_name: IdOrName,
    options: GetItemsParams,
}

impl <'a, T: DeserializeOwned> GetItemsBuilder<'a, T> {
    pub fn clean(& mut self, clean: bool) -> &'_ mut Self {
        self.options.clean = Some(clean);
        self
    }
    pub fn offset(& mut self, offset: u64) -> &'_ mut Self {
        self.options.offset = Some(offset);
        self
    }
    pub fn limit(& mut self, limit: u64) -> &'_ mut Self {
        self.options.limit = Some(limit);
        self
    }
    pub fn fields(& mut self, fields: Vec<String>) -> &'_ mut Self {
        self.options.fields = Some(fields.join(","));
        self
    }
    pub fn omit(& mut self, omit: Vec<String>) -> &'_ mut Self {
        self.options.omit = Some(omit.join(","));
        self
    }
    pub fn unwind(& mut self, unwind: String) -> &'_ mut Self {
        self.options.unwind = Some(unwind);
        self
    }
    pub fn desc(& mut self, desc: bool) -> &'_ mut Self {
        self.options.desc = Some(desc);
        self
    }
    pub fn attachment(& mut self, attachment: bool) -> &'_ mut Self {
        self.options.attachment = Some(attachment);
        self
    }
    pub fn delimiter(& mut self, delimiter: String) -> &'_ mut Self {
        self.options.delimiter = Some(delimiter);
        self
    }
    pub fn bom(& mut self, bom: bool) -> &'_ mut Self {
        self.options.bom = Some(bom);
        self
    }
    pub fn xml_root(& mut self, xml_root: String) -> &'_ mut Self {
        self.options.xmlRoot = Some(xml_root);
        self
    }
    pub fn xml_row(& mut self, xml_row: String) -> &'_ mut Self {
        self.options.xmlRow = Some(xml_row);
        self
    }
    pub fn skip_header_row(& mut self, skip_header_row: bool) -> &'_ mut Self {
        self.options.skipHeaderRow = Some(skip_header_row);
        self
    }
    pub fn skip_hidden(& mut self, skip_hidden: bool) -> &'_ mut Self {
        self.options.skipHidden = Some(skip_hidden);
        self
    }
    pub fn skip_empty(& mut self, skip_empty: bool) -> &'_ mut Self {
        self.options.skipEmpty = Some(skip_empty);
        self
    }
    pub fn simplified(& mut self, simplified: bool) -> &'_ mut Self {
        self.options.simplified = Some(simplified);
        self
    }
    pub fn skip_failed_pages(& mut self, skip_failed_pages: bool) -> &'_ mut Self {
        self.options.skipFailedPages = Some(skip_failed_pages);
        self
    }

    pub async fn send(&self) -> Result<PaginationList<T>, ApifyClientError> {
        let url = get_items_prepare_url(self.client, &self.dataset_id_or_name, &self.options)?;
        let resp = self.client.retrying_request(&url, &reqwest::Method::GET, &None, &None).await?;
        // For this endpoint, we have to reconstruct PaginationList manually
        let headers = resp.headers().clone();
        let bytes = resp.bytes().await.map_err(
            |err| ApifyApiError::ApiFailure(format!("Apify API did not return bytes. Something is very wrong. Please contact support@apify.com\n{}", err))

        )?;
        let items: Vec<T> = serde_json::from_slice(&bytes)?;
        println!("{:?}", headers);
        
        let total: u64 = parse_pagination_header(&headers, "X-Apify-Pagination-Total")?;
        let limit: u64 = parse_pagination_header(&headers, "X-Apify-Pagination-Limit")?;
        let offset: u64 = parse_pagination_header(&headers, "X-Apify-Pagination-Offset")?;
        // Because x-apify-pagination-count returns invalid values when hidden/empty items are skipped
        let count: u64 = items.len() as u64;

        let pagination_list = PaginationList {
            total,
            limit: Some(limit),
            count,
            offset,
            desc: false,
            items,
        };
        return Ok(pagination_list);  
    }
}

// TODO: Dedup this code
impl <'a> GetItemsBuilderRaw<'a> {
    pub fn format(& mut self, format: Format) -> &'_ mut Self {
        self.options.format = format;
        self
    }
    pub fn clean(& mut self, clean: bool) -> &'_ mut Self {
        self.options.clean = Some(clean);
        self
    }
    pub fn offset(& mut self, offset: u64) -> &'_ mut Self {
        self.options.offset = Some(offset);
        self
    }
    pub fn limit(& mut self, limit: u64) -> &'_ mut Self {
        self.options.limit = Some(limit);
        self
    }
    pub fn fields(& mut self, fields: Vec<String>) -> &'_ mut Self {
        self.options.fields = Some(fields.join(","));
        self
    }
    pub fn omit(& mut self, omit: Vec<String>) -> &'_ mut Self {
        self.options.omit = Some(omit.join(","));
        self
    }
    pub fn unwind(& mut self, unwind: String) -> &'_ mut Self {
        self.options.unwind = Some(unwind);
        self
    }
    pub fn desc(& mut self, desc: bool) -> &'_ mut Self {
        self.options.desc = Some(desc);
        self
    }
    pub fn attachment(& mut self, attachment: bool) -> &'_ mut Self {
        self.options.attachment = Some(attachment);
        self
    }
    pub fn delimiter(& mut self, delimiter: String) -> &'_ mut Self {
        self.options.delimiter = Some(delimiter);
        self
    }
    pub fn bom(& mut self, bom: bool) -> &'_ mut Self {
        self.options.bom = Some(bom);
        self
    }
    pub fn xml_root(& mut self, xml_root: String) -> &'_ mut Self {
        self.options.xmlRoot = Some(xml_root);
        self
    }
    pub fn xml_row(& mut self, xml_row: String) -> &'_ mut Self {
        self.options.xmlRow = Some(xml_row);
        self
    }
    pub fn skip_header_row(& mut self, skip_header_row: bool) -> &'_ mut Self {
        self.options.skipHeaderRow = Some(skip_header_row);
        self
    }
    pub fn skip_hidden(& mut self, skip_hidden: bool) -> &'_ mut Self {
        self.options.skipHidden = Some(skip_hidden);
        self
    }
    pub fn skip_empty(& mut self, skip_empty: bool) -> &'_ mut Self {
        self.options.skipEmpty = Some(skip_empty);
        self
    }
    pub fn simplified(& mut self, simplified: bool) -> &'_ mut Self {
        self.options.simplified = Some(simplified);
        self
    }
    pub fn skip_failed_pages(& mut self, skip_failed_pages: bool) -> &'_ mut Self {
        self.options.skipFailedPages = Some(skip_failed_pages);
        self
    }

    pub async fn send(&self) -> Result<String, ApifyClientError> {
        let url = get_items_prepare_url(self.client, &self.dataset_id_or_name, &self.options)?;
        let resp = self.client.retrying_request(&url, &reqwest::Method::GET, &None, &None).await?;
        
        let output = resp.text().await.map_err(
            |err| ApifyApiError::ApiFailure(format!("Apify API did not return valid UTF-8. Something is very wrong. Please contact support@apify.com\n{}", err))

        )?;
        return Ok(output);
    }
}

#[derive(QueryParams, Default)]
struct ListDatasetsParams {
    offset: Option<u32>,
    limit: Option<u32>,
    desc: Option<bool>,
    unnamed: Option<bool>,
}

pub struct ListDatasetsBuilder<'a> {
    client: &'a ApifyClient,
    options: ListDatasetsParams
}

impl <'a> ListDatasetsBuilder<'a> {
    pub fn offset(& mut self, offset: u32) -> &'_ mut Self {
        self.options.offset = Some(offset);
        self
    }
    pub fn limit(& mut self, limit: u32) -> &'_ mut Self {
        self.options.limit = Some(limit);
        self
    }
    pub fn desc(& mut self, desc: bool) -> &'_ mut Self {
        self.options.desc = Some(desc);
        self
    }
    pub fn unnamed(& mut self, unnamed: bool) -> &'_ mut Self {
        self.options.unnamed = Some(unnamed);
        self
    }

    pub async fn send(&self) -> Result<PaginationList<Dataset>, ApifyClientError> {
        let query_string = self.options.to_query_params();
        let url = format!(
            "{}?{}&token={}",
            BASE_URL,
            query_string,
            self.client.optional_token.as_ref().ok_or(ApifyApiError::MissingToken)?
        );
        let resp = self.client.retrying_request(&url, &reqwest::Method::GET, &None, &None).await?;
        let bytes = resp.bytes().await.map_err(
            |err| ApifyApiError::ApiFailure(format!("Apify API did not return bytes. Something is very wrong. Please contact support@apify.com\n{}", err))

        )?;

        let apify_client_result: ApifyClientResult<PaginationList<Dataset>> = serde_json::from_slice(&bytes)?;
        return Ok(apify_client_result.data);
    }
}