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
//! Interacting with Wit traits
use crate::{
client::WitClient,
common_types::{DeleteResponse, TraitBasic},
errors::Error,
};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Struct to use for creating a new trait
#[derive(Debug, Serialize)]
pub struct NewTrait {
name: String,
values: Vec<String>,
}
impl NewTrait {
/// Constructor for `NewTrait`
pub fn new(name: String, values: Vec<String>) -> Self {
Self { name, values }
}
}
/// A trait object returned from the Wit API
#[derive(Debug, Deserialize, PartialEq)]
pub struct TraitResponse {
/// The id of the trait
pub id: String,
/// The name of the trait
pub name: String,
/// Values that the trait may take on
pub values: Vec<TraitValue>,
}
/// A trait value
#[derive(Debug, Deserialize, PartialEq)]
pub struct TraitValue {
/// The id of the value
pub id: String,
/// The value itself
pub value: String,
}
impl WitClient {
/// Get all the traits from app associated with the current wit client
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::client::WitClient;
/// # use wit_ai_rs::TraitBasic;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: Vec<TraitBasic> = wit_client.get_traits().await.unwrap();
/// # })
/// ```
pub async fn get_traits(&self) -> Result<Vec<TraitBasic>, Error> {
let data = self
.make_request(Method::GET, "/traits", vec![], Option::<Value>::None)
.await?;
Ok(data)
}
/// Create a new trait
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::client::WitClient;
/// # use wit_ai_rs::traits::{TraitResponse, NewTrait};
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let new_trait = NewTrait::new("trait_name".to_string(), vec!["value1".to_string()]);
///
/// let response: TraitResponse = wit_client.create_trait(new_trait).await.unwrap();
/// # })
/// ```
pub async fn create_trait(&self, new_trait: NewTrait) -> Result<TraitResponse, Error> {
let data = self
.make_request(Method::POST, "/traits", vec![], Some(new_trait))
.await?;
Ok(data)
}
/// Get information about a given trait
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::client::WitClient;
/// # use wit_ai_rs::traits::TraitResponse;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: TraitResponse = wit_client.get_trait("intent_name").await.unwrap();
/// # })
/// ```
pub async fn get_trait(&self, trait_name: &str) -> Result<TraitResponse, Error> {
let endpoint = format!("/traits/{trait_name}");
let data = self
.make_request(Method::GET, &endpoint, vec![], Option::<Value>::None)
.await?;
Ok(data)
}
/// Delete a trait by name
///
/// Example:
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// # use wit_ai_rs::client::WitClient;
/// # use wit_ai_rs::DeleteResponse;
/// # let wit_client = WitClient::new(String::new(), String::new());
/// let response: DeleteResponse = wit_client.delete_trait("intent_name").await.unwrap();
/// # })
/// ```
pub async fn delete_trait(&self, trait_name: &str) -> Result<DeleteResponse, Error> {
let endpoint = format!("/traits/{trait_name}");
let data = self
.make_request(Method::DELETE, &endpoint, vec![], Option::<Value>::None)
.await?;
Ok(data)
}
}