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    /// # Examples
22    ///
23    /// ```no_run
24    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
25    /// use rain_sdk::models::shipping_groups::{ListShippingGroupsParams, ShippingGroup};
26    ///
27    /// # #[cfg(feature = "async")]
28    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
29    /// let config = Config::new(Environment::Dev);
30    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
31    /// let client = RainClient::new(config, auth)?;
32    ///
33    /// let params = ListShippingGroupsParams {
34    ///     cursor: None,
35    ///     limit: Some(20),
36    /// };
37    /// let shipping_groups = client.list_shipping_groups(&params).await?;
38    /// # Ok(())
39    /// # }
40    /// ```
41    #[cfg(feature = "async")]
42    pub async fn list_shipping_groups(
43        &self,
44        params: &ListShippingGroupsParams,
45    ) -> Result<Vec<ShippingGroup>> {
46        let path = "/issuing/shipping-groups";
47        let query_string = serde_urlencoded::to_string(params)?;
48        let full_path = if query_string.is_empty() {
49            path.to_string()
50        } else {
51            format!("{path}?{query_string}")
52        };
53        self.get(&full_path).await
54    }
55
56    /// Create a bulk shipping group
57    ///
58    /// # Arguments
59    ///
60    /// * `request` - The shipping group creation request
61    ///
62    /// # Returns
63    ///
64    /// Returns a [`ShippingGroup`] containing the created shipping group information (202 Accepted).
65    ///
66    /// # Examples
67    ///
68    /// ```no_run
69    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
70    /// use rain_sdk::models::shipping_groups::CreateShippingGroupRequest;
71    /// use rain_sdk::models::common::Address;
72    ///
73    /// # #[cfg(feature = "async")]
74    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
75    /// let config = Config::new(Environment::Dev);
76    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
77    /// let client = RainClient::new(config, auth)?;
78    ///
79    /// let request = CreateShippingGroupRequest {
80    ///     recipient_first_name: "John".to_string(),
81    ///     recipient_last_name: Some("Doe".to_string()),
82    ///     recipient_phone_country_code: Some("1".to_string()),
83    ///     recipient_phone_number: Some("5555555555".to_string()),
84    ///     address: Address {
85    ///         line1: "123 Main St".to_string(),
86    ///         line2: None,
87    ///         city: "New York".to_string(),
88    ///         region: "NY".to_string(),
89    ///         postal_code: "10001".to_string(),
90    ///         country_code: "US".to_string(),
91    ///         country: None,
92    ///     },
93    /// };
94    /// let shipping_group = client.create_shipping_group(&request).await?;
95    /// # Ok(())
96    /// # }
97    /// ```
98    #[cfg(feature = "async")]
99    pub async fn create_shipping_group(
100        &self,
101        request: &CreateShippingGroupRequest,
102    ) -> Result<ShippingGroup> {
103        let path = "/issuing/shipping-groups";
104        // Returns 202 Accepted
105        self.post(path, request).await
106    }
107
108    /// Get a bulk shipping group by its id
109    ///
110    /// # Arguments
111    ///
112    /// * `shipping_group_id` - The unique identifier of the shipping group
113    ///
114    /// # Returns
115    ///
116    /// Returns a [`ShippingGroup`] containing the shipping group information.
117    #[cfg(feature = "async")]
118    pub async fn get_shipping_group(&self, shipping_group_id: &Uuid) -> Result<ShippingGroup> {
119        let path = format!("/issuing/shipping-groups/{shipping_group_id}");
120        self.get(&path).await
121    }
122
123    // ============================================================================
124    // Blocking Methods
125    // ============================================================================
126
127    /// Get all bulk shipping groups (blocking)
128    #[cfg(feature = "sync")]
129    pub fn list_shipping_groups_blocking(
130        &self,
131        params: &ListShippingGroupsParams,
132    ) -> Result<Vec<ShippingGroup>> {
133        let path = "/issuing/shipping-groups";
134        let query_string = serde_urlencoded::to_string(params)?;
135        let full_path = if query_string.is_empty() {
136            path.to_string()
137        } else {
138            format!("{path}?{query_string}")
139        };
140        self.get_blocking(&full_path)
141    }
142
143    /// Create a bulk shipping group (blocking)
144    #[cfg(feature = "sync")]
145    pub fn create_shipping_group_blocking(
146        &self,
147        request: &CreateShippingGroupRequest,
148    ) -> Result<ShippingGroup> {
149        let path = "/issuing/shipping-groups";
150        // Returns 202 Accepted
151        self.post_blocking(path, request)
152    }
153
154    /// Get a bulk shipping group by its id (blocking)
155    #[cfg(feature = "sync")]
156    pub fn get_shipping_group_blocking(&self, shipping_group_id: &Uuid) -> Result<ShippingGroup> {
157        let path = format!("/issuing/shipping-groups/{shipping_group_id}");
158        self.get_blocking(&path)
159    }
160}