1use anyhow::Result;
2
3use crate::Client;
4#[derive(Clone, Debug)]
5pub struct Analytics {
6 pub client: Client,
7}
8
9impl Analytics {
10 #[doc(hidden)]
11 pub fn new(client: Client) -> Self {
12 Self { client }
13 }
14
15 #[doc = "Create a new analytics report.\n\nCreate a new analytics report for a set of metrics over a specific time span.\nThe report will be executed asynchronously. The response will include a link that can be used to retrieve the\nreport status & result.\n\n\n```rust,no_run\nasync fn example_analytics_create_report() -> anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let result: front_api::types::AnalyticsReportResponse2 = client\n .analytics()\n .create_report(&serde_json::Value::String(\"some-string\".to_string()))\n .await?;\n println!(\"{:?}\", result);\n Ok(())\n}\n```"]
16 #[tracing::instrument]
17 pub async fn create_report<'a>(
18 &'a self,
19 body: &crate::types::AnalyticsReportRequest2,
20 ) -> Result<crate::types::AnalyticsReportResponse2, crate::types::error::Error> {
21 let mut req = self.client.client.request(
22 http::Method::POST,
23 &format!("{}/{}", self.client.base_url, "analytics/reports"),
24 );
25 req = req.bearer_auth(&self.client.token);
26 req = req.json(body);
27 let resp = req.send().await?;
28 let status = resp.status();
29 if status.is_success() {
30 let text = resp.text().await.unwrap_or_default();
31 serde_json::from_str(&text).map_err(|err| {
32 crate::types::error::Error::from_serde_error(
33 format_serde_error::SerdeError::new(text.to_string(), err),
34 status,
35 )
36 })
37 } else {
38 Err(crate::types::error::Error::UnexpectedResponse(resp))
39 }
40 }
41
42 #[doc = "Fetch an analytics report.\n\n**Parameters:**\n\n- `report_uid: &'astr`: The report \
43 UID. (required)\n\n```rust,no_run\nasync fn example_analytics_get_report() -> \
44 anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let \
45 result: front_api::types::AnalyticsReportResponse2 =\n \
46 client.analytics().get_report(\"some-string\").await?;\n println!(\"{:?}\", \
47 result);\n Ok(())\n}\n```"]
48 #[tracing::instrument]
49 pub async fn get_report<'a>(
50 &'a self,
51 report_uid: &'a str,
52 ) -> Result<crate::types::AnalyticsReportResponse2, crate::types::error::Error> {
53 let mut req = self.client.client.request(
54 http::Method::GET,
55 &format!(
56 "{}/{}",
57 self.client.base_url,
58 "analytics/reports/{report_uid}".replace("{report_uid}", report_uid)
59 ),
60 );
61 req = req.bearer_auth(&self.client.token);
62 let resp = req.send().await?;
63 let status = resp.status();
64 if status.is_success() {
65 let text = resp.text().await.unwrap_or_default();
66 serde_json::from_str(&text).map_err(|err| {
67 crate::types::error::Error::from_serde_error(
68 format_serde_error::SerdeError::new(text.to_string(), err),
69 status,
70 )
71 })
72 } else {
73 Err(crate::types::error::Error::UnexpectedResponse(resp))
74 }
75 }
76
77 #[doc = "Create a new analytics export.\n\nCreate a new analytics export of messages or activities over a specific time span.\nThe export will be executed asynchronously. The response will include a link that can be used to retrieve the\nexport status & result.\n\n\n```rust,no_run\nasync fn example_analytics_create_export() -> anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let result: front_api::types::AnalyticsExportResponse2 = client\n .analytics()\n .create_export(&serde_json::Value::String(\"some-string\".to_string()))\n .await?;\n println!(\"{:?}\", result);\n Ok(())\n}\n```"]
78 #[tracing::instrument]
79 pub async fn create_export<'a>(
80 &'a self,
81 body: &crate::types::AnalyticsExportRequest2,
82 ) -> Result<crate::types::AnalyticsExportResponse2, crate::types::error::Error> {
83 let mut req = self.client.client.request(
84 http::Method::POST,
85 &format!("{}/{}", self.client.base_url, "analytics/exports"),
86 );
87 req = req.bearer_auth(&self.client.token);
88 req = req.json(body);
89 let resp = req.send().await?;
90 let status = resp.status();
91 if status.is_success() {
92 let text = resp.text().await.unwrap_or_default();
93 serde_json::from_str(&text).map_err(|err| {
94 crate::types::error::Error::from_serde_error(
95 format_serde_error::SerdeError::new(text.to_string(), err),
96 status,
97 )
98 })
99 } else {
100 Err(crate::types::error::Error::UnexpectedResponse(resp))
101 }
102 }
103
104 #[doc = "Fetch an analytics export.\n\n**Parameters:**\n\n- `export_id: &'astr`: The export \
105 ID. (required)\n\n```rust,no_run\nasync fn example_analytics_get_export() -> \
106 anyhow::Result<()> {\n let client = front_api::Client::new_from_env();\n let \
107 result: front_api::types::AnalyticsExportResponse2 =\n \
108 client.analytics().get_export(\"some-string\").await?;\n println!(\"{:?}\", \
109 result);\n Ok(())\n}\n```"]
110 #[tracing::instrument]
111 pub async fn get_export<'a>(
112 &'a self,
113 export_id: &'a str,
114 ) -> Result<crate::types::AnalyticsExportResponse2, crate::types::error::Error> {
115 let mut req = self.client.client.request(
116 http::Method::GET,
117 &format!(
118 "{}/{}",
119 self.client.base_url,
120 "analytics/exports/{export_id}".replace("{export_id}", export_id)
121 ),
122 );
123 req = req.bearer_auth(&self.client.token);
124 let resp = req.send().await?;
125 let status = resp.status();
126 if status.is_success() {
127 let text = resp.text().await.unwrap_or_default();
128 serde_json::from_str(&text).map_err(|err| {
129 crate::types::error::Error::from_serde_error(
130 format_serde_error::SerdeError::new(text.to_string(), err),
131 status,
132 )
133 })
134 } else {
135 Err(crate::types::error::Error::UnexpectedResponse(resp))
136 }
137 }
138}