rain_sdk/api/reports.rs
1//! Reports API
2//!
3//! This module provides functionality to get tenant reports.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::reports::*;
8
9impl RainClient {
10 /// Get a tenant's report
11 ///
12 /// # Arguments
13 ///
14 /// * `year` - Year of the report
15 /// * `month` - Month of the report
16 /// * `day` - Day of the report
17 /// * `params` - Optional query parameters (format)
18 ///
19 /// # Returns
20 ///
21 /// Returns the report as raw bytes (content type depends on format: csv, json, or ssrp).
22 ///
23 /// # Examples
24 ///
25 /// ```no_run
26 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
27 /// use rain_sdk::models::reports::{GetReportParams, ReportFormat};
28 ///
29 /// # #[cfg(feature = "async")]
30 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
31 /// let config = Config::new(Environment::Dev);
32 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
33 /// let client = RainClient::new(config, auth)?;
34 ///
35 /// let params = GetReportParams {
36 /// format: Some(ReportFormat::Csv),
37 /// };
38 /// let report = client.get_report("2024", "01", "15", ¶ms).await?;
39 /// # Ok(())
40 /// # }
41 /// ```
42 #[cfg(feature = "async")]
43 pub async fn get_report(
44 &self,
45 year: &str,
46 month: &str,
47 day: &str,
48 params: &GetReportParams,
49 ) -> Result<Vec<u8>> {
50 let base_url = self.base_url();
51 let mut url = base_url.clone();
52 url.path_segments_mut()
53 .map_err(|_| crate::error::RainError::Other(anyhow::anyhow!("Cannot be a base URL")))?
54 .pop_if_empty()
55 .push("issuing")
56 .push("reports")
57 .push(year)
58 .push(month)
59 .push(day);
60
61 let query_string = serde_urlencoded::to_string(params)?;
62 let full_path = if query_string.is_empty() {
63 format!("/issuing/reports/{year}/{month}/{day}")
64 } else {
65 format!("/issuing/reports/{year}/{month}/{day}?{query_string}")
66 };
67 self.get_bytes(&full_path).await
68 }
69
70 // ============================================================================
71 // Blocking Methods
72 // ============================================================================
73
74 /// Get a tenant's report (blocking)
75 #[cfg(feature = "sync")]
76 pub fn get_report_blocking(
77 &self,
78 year: &str,
79 month: &str,
80 day: &str,
81 params: &GetReportParams,
82 ) -> Result<Vec<u8>> {
83 let query_string = serde_urlencoded::to_string(params)?;
84 let full_path = if query_string.is_empty() {
85 format!("/issuing/reports/{year}/{month}/{day}")
86 } else {
87 format!("/issuing/reports/{year}/{month}/{day}?{query_string}")
88 };
89 self.get_bytes_blocking(&full_path)
90 }
91}