lago_client/queries/event.rs
1use lago_types::{
2 error::Result,
3 requests::event::{CreateEventRequest, GetEventRequest},
4 responses::event::{CreateEventResponse, GetEventResponse},
5};
6
7use crate::client::LagoClient;
8
9impl LagoClient {
10 /// Retrieves a specific event by its transaction ID
11 ///
12 /// # Arguments
13 /// * `request` - The request containing the transaction ID to retrieve
14 ///
15 /// # Returns
16 /// A `Result` containing the event data or an error
17 ///
18 /// # Example
19 /// ```no_run
20 /// use lago_client::LagoClient;
21 /// use lago_types::requests::event::GetEventRequest;
22 ///
23 /// # async fn example() -> lago_types::error::Result<()> {
24 /// let client = LagoClient::from_env()?;
25 /// let request = GetEventRequest::new("transaction_123".to_string());
26 /// let response = client.get_event(request).await?;
27 /// println!("Event code: {}", response.event.code);
28 /// # Ok(())
29 /// # }
30 /// ```
31 pub async fn get_event(&self, request: GetEventRequest) -> Result<GetEventResponse> {
32 let region = self.config.region()?;
33 // URL encode the transaction_id to handle special characters
34 let encoded_transaction_id = urlencoding::encode(&request.transaction_id).into_owned();
35 let url = format!("{}/events/{}", region.endpoint(), encoded_transaction_id);
36
37 self.make_request("GET", &url, None::<&()>).await
38 }
39
40 /// Creates a new usage event
41 ///
42 /// This endpoint is used for transmitting usage measurement events to either
43 /// a designated customer or a specific subscription.
44 ///
45 /// # Arguments
46 /// * `request` - The request containing the event data to create
47 ///
48 /// # Returns
49 /// A `Result` containing the created event acknowledgment or an error
50 ///
51 /// # Example
52 /// ```no_run
53 /// use lago_client::LagoClient;
54 /// use lago_types::requests::event::{CreateEventInput, CreateEventRequest};
55 /// use serde_json::json;
56 ///
57 /// # async fn example() -> lago_types::error::Result<()> {
58 /// let client = LagoClient::from_env()?;
59 /// let event_input = CreateEventInput::for_customer(
60 /// "transaction_456".to_string(),
61 /// "customer_123".to_string(),
62 /// "api_calls".to_string(),
63 /// )
64 /// .with_properties(json!({"calls": 150}));
65 ///
66 /// let request = CreateEventRequest::new(event_input);
67 /// let response = client.create_event(request).await?;
68 /// println!("Event created: {}", response.event.transaction_id);
69 /// # Ok(())
70 /// # }
71 /// ```
72 pub async fn create_event(&self, request: CreateEventRequest) -> Result<CreateEventResponse> {
73 let region = self.config.region()?;
74 let url = format!("{}/events", region.endpoint());
75
76 self.make_request("POST", &url, Some(&request)).await
77 }
78}