paddle_rust_sdk/
addresses.rs

1//! Builders for making requests to the Paddle API for customer addresses.
2//!
3//! See the [Paddle API](https://developer.paddle.com/api-reference/addresses/overview) documentation for more information.
4
5use std::collections::HashMap;
6
7use reqwest::Method;
8use serde::Serialize;
9use serde_with::skip_serializing_none;
10
11use crate::entities::Address;
12use crate::enums::{CountryCodeSupported, Status};
13use crate::ids::{AddressID, CustomerID};
14use crate::{Paddle, Result};
15
16/// Request builder for fetching addresses from Paddle API.
17#[skip_serializing_none]
18#[derive(Serialize)]
19pub struct AddressesList<'a> {
20    #[serde(skip)]
21    client: &'a Paddle,
22    #[serde(skip)]
23    customer_id: CustomerID,
24    after: Option<AddressID>,
25    #[serde(serialize_with = "crate::comma_separated")]
26    id: Option<Vec<AddressID>>,
27    order_by: Option<String>,
28    per_page: Option<usize>,
29    search: Option<String>,
30    status: Option<Status>,
31}
32
33impl<'a> AddressesList<'a> {
34    pub fn new(client: &'a Paddle, customer_id: impl Into<CustomerID>) -> Self {
35        Self {
36            client,
37            customer_id: customer_id.into(),
38            after: None,
39            id: None,
40            order_by: None,
41            per_page: None,
42            search: None,
43            status: None,
44        }
45    }
46
47    /// Return entities after the specified Paddle ID when working with paginated endpoints. Used in the `meta.pagination.next` URL in responses for list operations.
48    pub fn after(&mut self, address_id: AddressID) -> &mut Self {
49        self.after = Some(address_id);
50        self
51    }
52
53    /// Return only the IDs specified.
54    pub fn ids(
55        &mut self,
56        address_ids: impl IntoIterator<Item = impl Into<AddressID>>,
57    ) -> &mut Self {
58        self.id = Some(address_ids.into_iter().map(Into::into).collect());
59        self
60    }
61
62    /// Order returned entities by the specified field. Valid fields for ordering: id
63    pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
64        self.order_by = Some(format!("{}[ASC]", field));
65        self
66    }
67
68    /// Order returned entities by the specified field. Valid fields for ordering: id
69    pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
70        self.order_by = Some(format!("{}[DESC]", field));
71        self
72    }
73
74    /// Set how many entities are returned per page. Paddle returns the maximum number of results if a number greater than the maximum is requested.
75    /// Check `meta.pagination.per_page` in the response to see how many were returned.
76    ///
77    /// Default: `50`; Maximum: `200`.
78    pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
79        self.per_page = Some(entities_per_page);
80        self
81    }
82
83    /// Return entities that match a search query. Searches `status`, `created_at`, and `updated_at`.
84    pub fn search(&mut self, term: impl Into<String>) -> &mut Self {
85        self.search = Some(term.into());
86        self
87    }
88
89    /// Return only prices with the specified status.
90    pub fn status(&mut self, status: Status) -> &mut Self {
91        self.status = Some(status);
92        self
93    }
94
95    /// Send the request to Paddle and return the response.
96    pub async fn send(&self) -> Result<Vec<Address>> {
97        self.client
98            .send(
99                self,
100                Method::GET,
101                &format!("/customers/{}/addresses", self.customer_id.as_ref()),
102            )
103            .await
104    }
105}
106
107/// Request builder for creating customer addresses in Paddle API.
108#[skip_serializing_none]
109#[derive(Serialize)]
110pub struct AddressCreate<'a> {
111    #[serde(skip)]
112    client: &'a Paddle,
113    #[serde(skip)]
114    customer_id: CustomerID,
115    country_code: CountryCodeSupported,
116    description: Option<String>,
117    first_line: Option<String>,
118    second_line: Option<String>,
119    city: Option<String>,
120    postal_code: Option<String>,
121    region: Option<String>,
122    custom_data: Option<HashMap<String, String>>,
123}
124
125impl<'a> AddressCreate<'a> {
126    pub fn new(
127        client: &'a Paddle,
128        customer_id: impl Into<CustomerID>,
129        country_code: CountryCodeSupported,
130    ) -> Self {
131        Self {
132            client,
133            customer_id: customer_id.into(),
134            country_code,
135            description: None,
136            first_line: None,
137            second_line: None,
138            city: None,
139            postal_code: None,
140            region: None,
141            custom_data: None,
142        }
143    }
144
145    /// Memorable description for this address.
146    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
147        self.description = Some(description.into());
148        self
149    }
150
151    /// First line of the address.
152    pub fn first_line(&mut self, first_line: impl Into<String>) -> &mut Self {
153        self.first_line = Some(first_line.into());
154        self
155    }
156
157    /// Second line of the address.
158    pub fn second_line(&mut self, second_line: impl Into<String>) -> &mut Self {
159        self.second_line = Some(second_line.into());
160        self
161    }
162
163    /// City name.
164    pub fn city(&mut self, city: impl Into<String>) -> &mut Self {
165        self.city = Some(city.into());
166        self
167    }
168
169    /// Postal code. Required for US addresses
170    pub fn postal_code(&mut self, postal_code: impl Into<String>) -> &mut Self {
171        self.postal_code = Some(postal_code.into());
172        self
173    }
174
175    /// Region name.
176    pub fn region(&mut self, region: impl Into<String>) -> &mut Self {
177        self.region = Some(region.into());
178        self
179    }
180
181    /// Custom data to be stored with this address.
182    pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
183        self.custom_data = Some(custom_data);
184        self
185    }
186
187    /// Send the request to Paddle and return the response.
188    pub async fn send(&self) -> Result<Address> {
189        self.client
190            .send(
191                self,
192                Method::POST,
193                &format!("/customers/{}/addresses", self.customer_id.as_ref()),
194            )
195            .await
196    }
197}
198
199/// Request builder for fetching a single address from Paddle API.
200#[skip_serializing_none]
201#[derive(Serialize)]
202pub struct AddressGet<'a> {
203    #[serde(skip)]
204    client: &'a Paddle,
205    #[serde(skip)]
206    customer_id: CustomerID,
207    #[serde(skip)]
208    address_id: AddressID,
209}
210
211impl<'a> AddressGet<'a> {
212    pub fn new(
213        client: &'a Paddle,
214        customer_id: impl Into<CustomerID>,
215        address_id: impl Into<AddressID>,
216    ) -> Self {
217        Self {
218            client,
219            customer_id: customer_id.into(),
220            address_id: address_id.into(),
221        }
222    }
223
224    /// Send the request to Paddle and return the response.
225    pub async fn send(&self) -> Result<Address> {
226        self.client
227            .send(
228                self,
229                Method::GET,
230                &format!(
231                    "/customers/{}/addresses/{}",
232                    self.customer_id.as_ref(),
233                    self.address_id.as_ref()
234                ),
235            )
236            .await
237    }
238}
239
240/// Request builder for updating an address in Paddle API.
241#[skip_serializing_none]
242#[derive(Serialize)]
243pub struct AddressUpdate<'a> {
244    #[serde(skip)]
245    client: &'a Paddle,
246    #[serde(skip)]
247    customer_id: CustomerID,
248    #[serde(skip)]
249    address_id: AddressID,
250    description: Option<String>,
251    first_line: Option<String>,
252    second_line: Option<String>,
253    city: Option<String>,
254    postal_code: Option<String>,
255    region: Option<String>,
256    country_code: Option<CountryCodeSupported>,
257    custom_data: Option<HashMap<String, String>>,
258    status: Option<Status>,
259}
260
261impl<'a> AddressUpdate<'a> {
262    pub fn new(
263        client: &'a Paddle,
264        customer_id: impl Into<CustomerID>,
265        address_id: impl Into<AddressID>,
266    ) -> Self {
267        Self {
268            client,
269            customer_id: customer_id.into(),
270            address_id: address_id.into(),
271            description: None,
272            first_line: None,
273            second_line: None,
274            city: None,
275            postal_code: None,
276            region: None,
277            country_code: None,
278            custom_data: None,
279            status: None,
280        }
281    }
282
283    /// Memorable description for this address.
284    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
285        self.description = Some(description.into());
286        self
287    }
288
289    /// First line of the address.
290    pub fn first_line(&mut self, first_line: impl Into<String>) -> &mut Self {
291        self.first_line = Some(first_line.into());
292        self
293    }
294
295    /// Second line of the address.
296    pub fn second_line(&mut self, second_line: impl Into<String>) -> &mut Self {
297        self.second_line = Some(second_line.into());
298        self
299    }
300
301    /// City name.
302    pub fn city(&mut self, city: impl Into<String>) -> &mut Self {
303        self.city = Some(city.into());
304        self
305    }
306
307    /// Postal code. Required for US addresses
308    pub fn postal_code(&mut self, postal_code: impl Into<String>) -> &mut Self {
309        self.postal_code = Some(postal_code.into());
310        self
311    }
312
313    /// Region name.
314    pub fn region(&mut self, region: impl Into<String>) -> &mut Self {
315        self.region = Some(region.into());
316        self
317    }
318
319    /// Country code.
320    pub fn country_code(&mut self, country_code: CountryCodeSupported) -> &mut Self {
321        self.country_code = Some(country_code);
322        self
323    }
324
325    /// Custom data to be stored with this address.
326    pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
327        self.custom_data = Some(custom_data);
328        self
329    }
330
331    /// Status of the address.
332    pub fn status(&mut self, status: Status) -> &mut Self {
333        self.status = Some(status);
334        self
335    }
336
337    /// Send the request to Paddle and return the response.
338    pub async fn send(&self) -> Result<Address> {
339        self.client
340            .send(
341                self,
342                Method::PATCH,
343                &format!(
344                    "/customers/{}/addresses/{}",
345                    self.customer_id.as_ref(),
346                    self.address_id.as_ref()
347                ),
348            )
349            .await
350    }
351}