rain_sdk/api/
transactions.rs

1//! Transactions API
2//!
3//! This module provides functionality to manage transactions.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::transactions::*;
8use uuid::Uuid;
9
10impl RainClient {
11    /// Get all transactions
12    ///
13    /// # Arguments
14    ///
15    /// * `params` - Query parameters to filter transactions
16    ///
17    /// # Returns
18    ///
19    /// Returns a [`Vec<Transaction>`] containing the list of transactions.
20    #[cfg(feature = "async")]
21    pub async fn list_transactions(
22        &self,
23        params: &ListTransactionsParams,
24    ) -> Result<Vec<Transaction>> {
25        let path = "/issuing/transactions";
26        let query_string = serde_urlencoded::to_string(params)?;
27        let full_path = if query_string.is_empty() {
28            path.to_string()
29        } else {
30            format!("{path}?{query_string}")
31        };
32        self.get(&full_path).await
33    }
34
35    /// Get a transaction by its id
36    ///
37    /// # Arguments
38    ///
39    /// * `transaction_id` - The unique identifier of the transaction
40    ///
41    /// # Returns
42    ///
43    /// Returns a [`Transaction`] containing the transaction information.
44    #[cfg(feature = "async")]
45    pub async fn get_transaction(&self, transaction_id: &Uuid) -> Result<Transaction> {
46        let path = format!("/issuing/transactions/{transaction_id}");
47        self.get(&path).await
48    }
49
50    /// Update a transaction
51    ///
52    /// # Arguments
53    ///
54    /// * `transaction_id` - The unique identifier of the transaction
55    /// * `request` - The update request
56    ///
57    /// # Returns
58    ///
59    /// Returns success (204 No Content) with no response body.
60    #[cfg(feature = "async")]
61    pub async fn update_transaction(
62        &self,
63        transaction_id: &Uuid,
64        request: &UpdateTransactionRequest,
65    ) -> Result<()> {
66        let path = format!("/issuing/transactions/{transaction_id}");
67        let _: serde_json::Value = self.patch(&path, request).await?;
68        Ok(())
69    }
70
71    /// Get a transaction's receipt
72    ///
73    /// # Arguments
74    ///
75    /// * `transaction_id` - The unique identifier of the transaction
76    ///
77    /// # Returns
78    ///
79    /// Returns the receipt as raw bytes (application/octet-stream).
80    #[cfg(feature = "async")]
81    pub async fn get_transaction_receipt(&self, transaction_id: &Uuid) -> Result<Vec<u8>> {
82        let path = format!("/issuing/transactions/{transaction_id}/receipt");
83        self.get_bytes(&path).await
84    }
85
86    /// Upload a transaction's receipt
87    ///
88    /// # Arguments
89    ///
90    /// * `transaction_id` - The unique identifier of the transaction
91    /// * `request` - The receipt upload request
92    ///
93    /// # Returns
94    ///
95    /// Returns success (204 No Content) with no response body.
96    #[cfg(feature = "async")]
97    pub async fn upload_transaction_receipt(
98        &self,
99        transaction_id: &Uuid,
100        request: &UploadReceiptRequest,
101    ) -> Result<()> {
102        let path = format!("/issuing/transactions/{transaction_id}/receipt");
103
104        use reqwest::multipart::{Form, Part};
105        let form = Form::new().part(
106            "receipt",
107            Part::bytes(request.receipt.clone()).file_name(request.file_name.clone()),
108        );
109
110        self.put_multipart_no_content(&path, form).await
111    }
112
113    // ============================================================================
114    // Blocking Methods
115    // ============================================================================
116
117    /// Get all transactions (blocking)
118    #[cfg(feature = "sync")]
119    pub fn list_transactions_blocking(
120        &self,
121        params: &ListTransactionsParams,
122    ) -> Result<Vec<Transaction>> {
123        let path = "/issuing/transactions";
124        let query_string = serde_urlencoded::to_string(params)?;
125        let full_path = if query_string.is_empty() {
126            path.to_string()
127        } else {
128            format!("{path}?{query_string}")
129        };
130        self.get_blocking(&full_path)
131    }
132
133    /// Get a transaction by its id (blocking)
134    #[cfg(feature = "sync")]
135    pub fn get_transaction_blocking(&self, transaction_id: &Uuid) -> Result<Transaction> {
136        let path = format!("/issuing/transactions/{transaction_id}");
137        self.get_blocking(&path)
138    }
139
140    /// Update a transaction (blocking)
141    #[cfg(feature = "sync")]
142    pub fn update_transaction_blocking(
143        &self,
144        transaction_id: &Uuid,
145        request: &UpdateTransactionRequest,
146    ) -> Result<()> {
147        let path = format!("/issuing/transactions/{transaction_id}");
148        let _: serde_json::Value = self.patch_blocking(&path, request)?;
149        Ok(())
150    }
151
152    /// Get a transaction's receipt (blocking)
153    #[cfg(feature = "sync")]
154    pub fn get_transaction_receipt_blocking(&self, transaction_id: &Uuid) -> Result<Vec<u8>> {
155        let path = format!("/issuing/transactions/{transaction_id}/receipt");
156        self.get_bytes_blocking(&path)
157    }
158
159    /// Upload a transaction's receipt (blocking)
160    #[cfg(feature = "sync")]
161    pub fn upload_transaction_receipt_blocking(
162        &self,
163        transaction_id: &Uuid,
164        request: &UploadReceiptRequest,
165    ) -> Result<()> {
166        let path = format!("/issuing/transactions/{transaction_id}/receipt");
167
168        use reqwest::blocking::multipart::{Form, Part};
169        let form = Form::new().part(
170            "receipt",
171            Part::bytes(request.receipt.clone()).file_name(request.file_name.clone()),
172        );
173
174        self.put_multipart_blocking_no_content(&path, form)
175    }
176}