apify_client/
generic_types.rs1use crate::client::{ApifyClient, ApifyApiError, ApifyClientError, ApifyClientResult};
2use std::marker::PhantomData;
3use serde::{Deserialize};
4
5#[derive(Debug, PartialEq)]
6pub struct NoContent;
7
8impl NoContent {
9 pub fn new() -> Self {
10 NoContent {}
11 }
12}
13
14#[derive(Deserialize, Debug, PartialEq)]
15pub struct PaginationList<T> {
16 pub total: u64,
17 pub offset: u64,
18 pub limit: Option<u64>,
19 pub count: u64,
20 pub desc: bool,
21 pub items: Vec<T>
22}
23
24pub struct SimpleBuilder <'a, T> {
25 pub client: &'a ApifyClient,
26 pub url: String,
27 pub requires_token: bool,
28 pub method: reqwest::Method,
29 pub body: Result<Option<Vec<u8>>, serde_json::error::Error>,
31 pub headers: Option<reqwest::header::HeaderMap>,
32 pub phantom: PhantomData<T>,
33}
34
35impl<'a, T: serde::de::DeserializeOwned> SimpleBuilder<'a, T> {
37 pub async fn send(self) -> Result<T, ApifyClientError> {
38 let url = if self.requires_token {
39 let token = self.client.optional_token.as_ref().ok_or(ApifyApiError::MissingToken)?;
40 format!("{}&token={}", &self.url, token)
41 } else {
42 self.url
43 };
44 println!("size of: {}", std::mem::size_of::<T>());
45 let body = self.body?;
46 let resp = self.client.retrying_request(&url, &self.method, &body, &self.headers).await?;
47 let bytes = resp.bytes().await.map_err(
48 |err| ApifyApiError::ApiFailure(format!("Apify API did not return bytes. Something is very wrong. Please contact support@apify.com\n{}", err))
49 )?;
50 let apify_client_result: ApifyClientResult<T> = serde_json::from_slice(&bytes)?;
51 Ok(apify_client_result.data)
52
53 }
54}
55
56impl<'a> SimpleBuilder<'a, NoContent> {
58 pub async fn send(self) -> Result<NoContent, ApifyClientError> {
59 let url = if self.requires_token {
60 let token = self.client.optional_token.as_ref().ok_or(ApifyApiError::MissingToken)?;
61 format!("{}&token={}", &self.url, token)
62 } else {
63 self.url
64 };
65 let body = self.body?;
66 self.client.retrying_request(&url, &self.method, &body, &self.headers).await?;
67 Ok(NoContent::new())
68 }
69}