rain_sdk/api/
shipping_groups.rs

1//! Shipping Groups API
2//!
3//! This module provides functionality to manage bulk shipping groups.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::shipping_groups::*;
8use uuid::Uuid;
9
10impl RainClient {
11    /// Get all bulk shipping groups
12    ///
13    /// # Arguments
14    ///
15    /// * `params` - Query parameters for filtering shipping groups
16    ///
17    /// # Returns
18    ///
19    /// Returns a [`Vec<ShippingGroup>`] containing the list of shipping groups.
20    ///
21    /// # Errors
22    ///
23    /// This method can return the following errors:
24    /// - `401` - Invalid authorization
25    /// - `500` - Internal server error
26    ///
27    /// # Examples
28    ///
29    /// ```no_run
30    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
31    /// use rain_sdk::models::shipping_groups::{ListShippingGroupsParams, ShippingGroup};
32    ///
33    /// # #[cfg(feature = "async")]
34    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
35    /// let config = Config::new(Environment::Dev);
36    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
37    /// let client = RainClient::new(config, auth)?;
38    ///
39    /// let params = ListShippingGroupsParams {
40    ///     cursor: None,
41    ///     limit: Some(20),
42    /// };
43    /// let shipping_groups = client.list_shipping_groups(&params).await?;
44    /// # Ok(())
45    /// # }
46    /// ```
47    #[cfg(feature = "async")]
48    pub async fn list_shipping_groups(
49        &self,
50        params: &ListShippingGroupsParams,
51    ) -> Result<Vec<ShippingGroup>> {
52        let path = "/shipping-groups";
53        let query_string = serde_urlencoded::to_string(params)?;
54        let full_path = if query_string.is_empty() {
55            path.to_string()
56        } else {
57            format!("{path}?{query_string}")
58        };
59        self.get(&full_path).await
60    }
61
62    /// Create a bulk shipping group
63    ///
64    /// # Arguments
65    ///
66    /// * `request` - The shipping group creation request
67    ///
68    /// # Returns
69    ///
70    /// Returns a [`ShippingGroup`] containing the created shipping group information (202 Accepted).
71    ///
72    /// # Errors
73    ///
74    /// This method can return the following errors:
75    /// - `400` - Invalid request
76    /// - `401` - Invalid authorization
77    /// - `500` - Internal server error
78    ///
79    /// # Examples
80    ///
81    /// ```no_run
82    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
83    /// use rain_sdk::models::shipping_groups::CreateShippingGroupRequest;
84    /// use rain_sdk::models::common::Address;
85    ///
86    /// # #[cfg(feature = "async")]
87    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
88    /// let config = Config::new(Environment::Dev);
89    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
90    /// let client = RainClient::new(config, auth)?;
91    ///
92    /// let request = CreateShippingGroupRequest {
93    ///     recipient_first_name: "John".to_string(),
94    ///     recipient_last_name: Some("Doe".to_string()),
95    ///     recipient_phone_country_code: Some("1".to_string()),
96    ///     recipient_phone_number: Some("5555555555".to_string()),
97    ///     address: Address {
98    ///         line1: "123 Main St".to_string(),
99    ///         line2: None,
100    ///         city: "New York".to_string(),
101    ///         region: "NY".to_string(),
102    ///         postal_code: "10001".to_string(),
103    ///         country_code: "US".to_string(),
104    ///         country: None,
105    ///     },
106    /// };
107    /// let shipping_group = client.create_shipping_group(&request).await?;
108    /// # Ok(())
109    /// # }
110    /// ```
111    #[cfg(feature = "async")]
112    pub async fn create_shipping_group(
113        &self,
114        request: &CreateShippingGroupRequest,
115    ) -> Result<ShippingGroup> {
116        let path = "/shipping-groups";
117        // Returns 202 Accepted
118        self.post(path, request).await
119    }
120
121    /// Get a bulk shipping group by its id
122    ///
123    /// # Arguments
124    ///
125    /// * `shipping_group_id` - The unique identifier of the shipping group
126    ///
127    /// # Returns
128    ///
129    /// Returns a [`ShippingGroup`] containing the shipping group information.
130    ///
131    /// # Errors
132    ///
133    /// This method can return the following errors:
134    /// - `401` - Invalid authorization
135    /// - `404` - Shipping group not found
136    /// - `500` - Internal server error
137    #[cfg(feature = "async")]
138    pub async fn get_shipping_group(&self, shipping_group_id: &Uuid) -> Result<ShippingGroup> {
139        let path = format!("/shipping-groups/{shipping_group_id}");
140        self.get(&path).await
141    }
142
143    // ============================================================================
144    // Blocking Methods
145    // ============================================================================
146
147    /// Get all bulk shipping groups (blocking)
148    #[cfg(feature = "sync")]
149    pub fn list_shipping_groups_blocking(
150        &self,
151        params: &ListShippingGroupsParams,
152    ) -> Result<Vec<ShippingGroup>> {
153        let path = "/shipping-groups";
154        let query_string = serde_urlencoded::to_string(params)?;
155        let full_path = if query_string.is_empty() {
156            path.to_string()
157        } else {
158            format!("{path}?{query_string}")
159        };
160        self.get_blocking(&full_path)
161    }
162
163    /// Create a bulk shipping group (blocking)
164    #[cfg(feature = "sync")]
165    pub fn create_shipping_group_blocking(
166        &self,
167        request: &CreateShippingGroupRequest,
168    ) -> Result<ShippingGroup> {
169        let path = "/shipping-groups";
170        // Returns 202 Accepted
171        self.post_blocking(path, request)
172    }
173
174    /// Get a bulk shipping group by its id (blocking)
175    #[cfg(feature = "sync")]
176    pub fn get_shipping_group_blocking(&self, shipping_group_id: &Uuid) -> Result<ShippingGroup> {
177        let path = format!("/shipping-groups/{shipping_group_id}");
178        self.get_blocking(&path)
179    }
180}