1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Methods and types for interacting with wit entities
//!
//! Includes methods for CRUD operations so that entities can be
//! managed programmatically
use crate::{client::WitClient, errors::Error, DeleteResponse, EntityBasic, EntityKeyword};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// A struct to use for creating a new entity
#[derive(Debug, Serialize)]
pub struct NewEntity {
name: String,
roles: Vec<String>,
lookups: Option<Vec<String>>,
keywords: Option<Vec<EntityKeyword>>,
}
/// Builder for `NewEntity`--use for creating entities
#[derive(Debug)]
pub struct NewEntityBuilder {
new_entity: NewEntity,
}
impl NewEntityBuilder {
/// Create a `NewEntityBuilder` with the given name, empty lookups and keywords, and the default role
/// * `name` - Name for the entity. For built-in entities, use the wit$ prefix.
pub fn new(name: String) -> Self {
Self {
new_entity: NewEntity {
name: name.clone(),
roles: vec![name],
lookups: None,
keywords: None,
},
}
}
/// A list of roles to create for the entity
pub fn roles(mut self, roles: Vec<String>) -> Self {
self.new_entity.roles = roles;
self
}
/// Set the lookup strategies for a custom entity (free-text, keywords).
/// Both lookup strategies will be created if this is left empty.
pub fn lookups(mut self, lookups: Vec<String>) -> Self {
self.new_entity.lookups = Some(lookups);
self
}
/// Set the keywords associated with this entity
pub fn keywords(mut self, keywords: Vec<EntityKeyword>) -> Self {
self.new_entity.keywords = Some(keywords);
self
}
/// Create a `NewEntity` from this `NewEntityBuilder`
pub fn build(self) -> NewEntity {
self.new_entity
}
}
/// A response from creating, updating, or getting an entity
#[derive(Debug, Deserialize, PartialEq)]
pub struct EntityResponse {
/// The id of the entity
pub id: String,
/// The name of the entity
pub name: String,
/// Roles of the entity
pub roles: Vec<EntityRole>,
/// Lookup strategies for the entity. Does not exist when the entity is built into Wit
pub lookups: Option<Vec<String>>,
/// Keywords associated with the entity. Does not exist when the entity is built into Wit
pub keywords: Option<Vec<EntityKeyword>>,
}
/// A role for an entity
#[derive(Debug, Deserialize, PartialEq)]
pub struct EntityRole {
/// The id of the role
pub id: String,
/// The name of the role
pub name: String,
}
impl WitClient {
/// Returns basic information about all entities
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::EntityBasic;
/// # use wit_ai_rs::client::WitClient;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: Vec<EntityBasic> = wit_client.get_entities().await.unwrap();
/// # })
/// ```
pub async fn get_entities(&self) -> Result<Vec<EntityBasic>, Error> {
self.make_request(Method::GET, "/entities", vec![], Option::<Value>::None)
.await
}
/// Creates a new entity
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::entities::{EntityResponse, NewEntityBuilder};
/// # use wit_ai_rs::client::WitClient;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let new_entity = NewEntityBuilder::new("entity_name".to_string())
/// .roles(vec!["role".to_string()])
/// .build();
///
/// let response: EntityResponse = wit_client.create_entity(new_entity).await.unwrap();
/// # })
/// ```
pub async fn create_entity(&self, new_entity: NewEntity) -> Result<EntityResponse, Error> {
self.make_request(Method::POST, "/entities", vec![], Some(new_entity))
.await
}
/// Returns information about the entity with the given name
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::entities::EntityResponse;
/// # use wit_ai_rs::client::WitClient;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: EntityResponse = wit_client.get_entity("entity".to_string()).await.unwrap();
/// # })
/// ```
pub async fn get_entity(&self, entity_name: String) -> Result<EntityResponse, Error> {
let endpoint = format!("/entities/{}", entity_name);
self.make_request(Method::GET, &endpoint, vec![], Option::<Value>::None)
.await
}
/// Update information about an entity with the current name `old_name`, overwriting its
/// data with `updated_entity`
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::entities::{EntityResponse, NewEntityBuilder};
/// # use wit_ai_rs::client::WitClient;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let updated_entity = NewEntityBuilder::new("updated_name".to_string())
/// .roles(vec!["updated_role".to_string()])
/// .build();
///
/// let response: EntityResponse = wit_client
/// .update_entity("entity_name", updated_entity).await.unwrap();
/// # })
/// ```
pub async fn update_entity(
&self,
old_name: &str,
updated_entity: NewEntity,
) -> Result<EntityResponse, Error> {
let endpoint = format!("/entities/{}", old_name);
self.make_request(Method::PUT, &endpoint, vec![], Some(updated_entity))
.await
}
/// Deletes the entity with the given name
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::DeleteResponse;
/// # use wit_ai_rs::client::WitClient;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: DeleteResponse = wit_client.delete_entity("entity_name").await.unwrap();
/// # })
/// ```
pub async fn delete_entity(&self, entity_name: &str) -> Result<DeleteResponse, Error> {
let endpoint = format!("/entities/{}", entity_name);
self.make_request(Method::DELETE, &endpoint, vec![], Option::<Value>::None)
.await
}
}