paddle_rust_sdk/
reports.rs

1//! Request builders for creating and listing reports.
2//!
3//! See the [Paddle API](https://developer.paddle.com/api-reference/reports/overview) documentation for more information.
4
5use paddle_rust_sdk_types::reports::ReportType;
6use reqwest::Method;
7use serde::de::DeserializeOwned;
8use serde::Serialize;
9use serde_with::skip_serializing_none;
10
11use crate::entities::{ReportBase, ReportFilter, ReportFilterValue};
12use crate::enums::{FilterOperator, ReportStatus};
13use crate::ids::PaddleID;
14use crate::paginated::Paginated;
15use crate::{Paddle, Result};
16
17/// Request builder for querying Paddle for reports.
18#[skip_serializing_none]
19#[derive(Serialize)]
20pub struct ReportsList<'a> {
21    #[serde(skip)]
22    client: &'a Paddle,
23    after: Option<PaddleID>,
24    order_by: Option<String>,
25    per_page: Option<usize>,
26    #[serde(serialize_with = "crate::comma_separated_enum")]
27    status: Option<Vec<ReportStatus>>,
28}
29
30impl<'a> ReportsList<'a> {
31    pub fn new(client: &'a Paddle) -> Self {
32        Self {
33            client,
34            after: None,
35            order_by: None,
36            per_page: None,
37            status: None,
38        }
39    }
40
41    /// Return entities after the specified Paddle ID when working with paginated endpoints. Used in the `meta.pagination.next` URL in responses for list operations.
42    pub fn after(&mut self, report_id: impl Into<PaddleID>) -> &mut Self {
43        self.after = Some(report_id.into());
44        self
45    }
46
47    /// Order returned entities by the specified field. Valid fields for ordering: `id`
48    pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
49        self.order_by = Some(format!("{}[ASC]", field));
50        self
51    }
52
53    /// Order returned entities by the specified field. Valid fields for ordering: `id`
54    pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
55        self.order_by = Some(format!("{}[DESC]", field));
56        self
57    }
58
59    /// Set how many entities are returned per page. Paddle returns the maximum number of results if a number greater than the maximum is requested.
60    /// Check `meta.pagination.per_page` in the response to see how many were returned.
61    ///
62    /// Default: `50`; Maximum: `200`.
63    pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
64        self.per_page = Some(entities_per_page);
65        self
66    }
67
68    /// Return entities that match the specified status.
69    pub fn status(&mut self, statuses: impl IntoIterator<Item = ReportStatus>) -> &mut Self {
70        self.status = Some(statuses.into_iter().collect());
71        self
72    }
73
74    /// Returns a paginator for fetching pages of entities from Paddle
75    pub fn send(&self) -> Paginated<'_, Vec<ReportBase>> {
76        Paginated::new(self.client, "/reports", self)
77    }
78}
79
80/// Request builder for creating reports in Paddle.
81#[skip_serializing_none]
82#[derive(Serialize)]
83pub struct ReportCreate<'a, T: ReportType> {
84    #[serde(skip)]
85    client: &'a Paddle,
86    r#type: T,
87    filters: Vec<ReportFilter<T::FilterName>>,
88}
89
90impl<'a, T: ReportType + DeserializeOwned> ReportCreate<'a, T> {
91    pub fn new(client: &'a Paddle, r#type: T) -> Self {
92        Self {
93            client,
94            r#type,
95            filters: Vec::new(),
96        }
97    }
98
99    /// Add filter criteria for this report. If omitted, reports are filtered to include data updated in the last 30 days. This means `updated_at` is greater than or equal to (`gte`) the date 30 days ago from the time the report was generated.
100    pub fn append_filter(
101        &mut self,
102        name: T::FilterName,
103        operator: Option<FilterOperator>,
104        value: impl Into<ReportFilterValue>,
105    ) -> &mut Self {
106        self.filters.push(ReportFilter {
107            name,
108            operator,
109            value: value.into(),
110        });
111
112        self
113    }
114
115    /// Clear all report filters
116    pub fn clear_filters(&mut self) {
117        self.filters.clear();
118    }
119
120    /// Set all filter criteria for this report. This overrides any previously set filters.
121    pub fn set_filters(
122        &mut self,
123        filters: impl IntoIterator<Item = (T::FilterName, Option<FilterOperator>, ReportFilterValue)>,
124    ) -> &mut Self {
125        self.filters = filters
126            .into_iter()
127            .map(|(name, operator, value)| ReportFilter {
128                name,
129                operator,
130                value,
131            })
132            .collect();
133        self
134    }
135
136    /// Send the request to Paddle and return the response.
137    pub async fn send(&self) -> Result<ReportBase> {
138        self.client.send(self, Method::POST, "/reports").await
139    }
140}