1use 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#[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 pub fn after(&mut self, address_id: AddressID) -> &mut Self {
49 self.after = Some(address_id);
50 self
51 }
52
53 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 pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
64 self.order_by = Some(format!("{}[ASC]", field));
65 self
66 }
67
68 pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
70 self.order_by = Some(format!("{}[DESC]", field));
71 self
72 }
73
74 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 pub fn search(&mut self, term: impl Into<String>) -> &mut Self {
85 self.search = Some(term.into());
86 self
87 }
88
89 pub fn status(&mut self, status: Status) -> &mut Self {
91 self.status = Some(status);
92 self
93 }
94
95 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#[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 pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
147 self.description = Some(description.into());
148 self
149 }
150
151 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 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 pub fn city(&mut self, city: impl Into<String>) -> &mut Self {
165 self.city = Some(city.into());
166 self
167 }
168
169 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 pub fn region(&mut self, region: impl Into<String>) -> &mut Self {
177 self.region = Some(region.into());
178 self
179 }
180
181 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 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#[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 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#[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 pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
285 self.description = Some(description.into());
286 self
287 }
288
289 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 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 pub fn city(&mut self, city: impl Into<String>) -> &mut Self {
303 self.city = Some(city.into());
304 self
305 }
306
307 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 pub fn region(&mut self, region: impl Into<String>) -> &mut Self {
315 self.region = Some(region.into());
316 self
317 }
318
319 pub fn country_code(&mut self, country_code: CountryCodeSupported) -> &mut Self {
321 self.country_code = Some(country_code);
322 self
323 }
324
325 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 pub fn status(&mut self, status: Status) -> &mut Self {
333 self.status = Some(status);
334 self
335 }
336
337 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}