Skip to content

Commit 8ec795a

Browse files
committed
add: support for read-event-type
Signed-off-by: Raphael Höser <[email protected]>
1 parent a7a7225 commit 8ec795a

File tree

5 files changed

+96
-15
lines changed

5 files changed

+96
-15
lines changed

src/client.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ mod precondition;
2222
pub mod request_options;
2323

2424
use crate::{
25+
client::client_request::ReadEventTypeRequest,
2526
error::ClientError,
2627
event::{Event, EventCandidate, ManagementEvent},
28+
request_options::EventType,
2729
};
2830
use client_request::{
2931
ClientRequest, ListEventTypesRequest, ListSubjectsRequest, ObserveEventsRequest,
3032
OneShotRequest, PingRequest, ReadEventsRequest, RegisterEventSchemaRequest,
3133
RunEventqlQueryRequest, StreamingRequest, VerifyApiTokenRequest, WriteEventsRequest,
32-
list_event_types::EventType,
3334
};
3435
use futures::Stream;
3536
pub use precondition::Precondition;
@@ -211,6 +212,52 @@ impl Client {
211212
Ok(response)
212213
}
213214

215+
/// Reads a specific event type from the DB instance.
216+
///
217+
/// ```
218+
/// use eventsourcingdb::event::EventCandidate;
219+
/// use futures::StreamExt;
220+
/// # use serde_json::json;
221+
/// # tokio_test::block_on(async {
222+
/// # let container = eventsourcingdb::container::Container::start_default().await.unwrap();
223+
/// let db_url = "http://localhost:3000/";
224+
/// let api_token = "secrettoken";
225+
/// # let db_url = container.get_base_url().await.unwrap();
226+
/// # let api_token = container.get_api_token();
227+
/// let client = eventsourcingdb::client::Client::new(db_url, api_token);
228+
/// let event_type = "io.eventsourcingdb.test";
229+
/// let schema = json!({
230+
/// "type": "object",
231+
/// "properties": {
232+
/// "id": {
233+
/// "type": "string"
234+
/// },
235+
/// "name": {
236+
/// "type": "string"
237+
/// }
238+
/// },
239+
/// "required": ["id", "name"]
240+
/// });
241+
/// client.register_event_schema(event_type, &schema).await.expect("Failed to register event types");
242+
/// let type_info = client.read_event_type(event_type).await.expect("Failed to read event type");
243+
/// println!("Found Type {} with schema {:?}", type_info.name, type_info.schema);
244+
/// # })
245+
/// ```
246+
///
247+
/// # Errors
248+
/// This function will return an error if the request fails or if the URL is invalid.
249+
pub async fn read_event_type(
250+
&self,
251+
event_type: &str,
252+
) -> Result<EventType, ClientError> {
253+
let response = self
254+
.request_oneshot(ReadEventTypeRequest {
255+
event_type: event_type.to_string(),
256+
})
257+
.await?;
258+
Ok(response)
259+
}
260+
214261
/// Observe events from the DB instance.
215262
///
216263
/// ```

src/client/client_request.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod list_subjects;
55
mod observe_events;
66
mod ping;
77
mod read_events;
8+
mod read_event_type;
89
mod register_event_schema;
910
mod run_eventql_query;
1011
mod verify_api_token;
@@ -15,6 +16,7 @@ pub use list_subjects::ListSubjectsRequest;
1516
pub use observe_events::ObserveEventsRequest;
1617
pub use ping::PingRequest;
1718
pub use read_events::ReadEventsRequest;
19+
pub use read_event_type::ReadEventTypeRequest;
1820
pub use register_event_schema::RegisterEventSchemaRequest;
1921
pub use run_eventql_query::RunEventqlQueryRequest;
2022
use serde_json::Value;

src/client/client_request/list_event_types.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1+
use super::{ClientRequest, StreamingRequest};
12
use reqwest::Method;
2-
use serde::{Deserialize, Serialize};
3-
use serde_json::Value;
4-
5-
use crate::error::ClientError;
3+
use serde::Serialize;
64

7-
use super::{ClientRequest, StreamingRequest};
8-
#[derive(Deserialize, Debug)]
9-
#[serde(rename_all = "camelCase")]
10-
pub struct EventType {
11-
#[serde(rename = "eventType")]
12-
pub name: String,
13-
pub is_phantom: bool,
14-
pub schema: Option<Value>,
15-
}
5+
use crate::{client::request_options::EventType, error::ClientError};
166

177
#[derive(Debug, Clone, Serialize)]
188
#[serde(rename_all = "camelCase")]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use reqwest::Method;
2+
use serde::Serialize;
3+
4+
use crate::{
5+
client::client_request::{ClientRequest, OneShotRequest},
6+
error::ClientError,
7+
request_options::EventType,
8+
};
9+
10+
#[derive(Debug, Clone, Serialize)]
11+
#[serde(rename_all = "camelCase")]
12+
pub struct ReadEventTypeRequest {
13+
#[serde(rename = "eventType")]
14+
/// The name of the event type to read
15+
pub event_type: String,
16+
}
17+
18+
impl ClientRequest for ReadEventTypeRequest {
19+
const URL_PATH: &'static str = "/api/v1/read-event-type";
20+
const METHOD: Method = Method::POST;
21+
22+
fn body(&self) -> Option<Result<impl Serialize, ClientError>> {
23+
Some(Ok(self))
24+
}
25+
}
26+
impl OneShotRequest for ReadEventTypeRequest {
27+
type Response = EventType;
28+
}

src/client/request_options.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! This module contains supporting options for the client requests.
22
3-
use serde::Serialize;
3+
use serde::{Deserialize, Serialize};
4+
use serde_json::Value;
45

56
/// Options for reading events from the database
67
#[derive(Debug, Default, Clone, Serialize)]
@@ -112,3 +113,16 @@ pub struct ObserveFromLatestEventOptions<'a> {
112113
#[serde(rename = "type")]
113114
pub ty: &'a str,
114115
}
116+
117+
/// Represents an event type in the database
118+
#[derive(Deserialize, Debug)]
119+
#[serde(rename_all = "camelCase")]
120+
pub struct EventType {
121+
/// The name of the event type
122+
#[serde(rename = "eventType")]
123+
pub name: String,
124+
/// Whether the event type is a phantom type
125+
pub is_phantom: bool,
126+
/// The schema of the event type, if available
127+
pub schema: Option<Value>,
128+
}

0 commit comments

Comments
 (0)