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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use reqwest::Url;
use std::error::Error;
use std::sync::Arc;
use uuid::Uuid;
use crate::collections::{
classification::{ClassificationRequest, ClassificationResponse},
error::ClassificationError,
};
/// All classification related endpoints and functionality described in
/// [Weaviate meta API documentation](https://weaviate.io/developers/weaviate/api/rest/classification)
#[derive(Debug)]
pub struct Classification {
endpoint: Url,
client: Arc<reqwest::Client>,
}
impl Classification {
/// Create a new instance of the Classification endpoint struct. Should only be done by the
/// parent client.
pub(super) fn new(url: &Url, client: Arc<reqwest::Client>) -> Result<Self, Box<dyn Error>> {
let endpoint = url.join("/v1/classifications/")?;
Ok(Classification { endpoint, client })
}
/// Schedule a new classification
///
/// # Example
/// ```no_run
/// use weaviate_community::WeaviateClient;
/// use weaviate_community::collections::classification::{
/// ClassificationRequest,
/// ClassificationType
/// };
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = WeaviateClient::builder("http://localhost:8080").build()?;
///
/// let req = ClassificationRequest::builder()
/// .with_type(ClassificationType::KNN)
/// .with_class("Article")
/// .with_based_on_properties(vec!["summary"])
/// .with_classify_properties(vec!["hasPopularity"])
/// .with_filters(serde_json::json!({
/// "trainingSetWhere": {
/// "path": ["wordCount"],
/// "operator": "GreaterThan",
/// "valueInt": 100
/// }
/// }))
/// .with_settings(serde_json::json!({
/// "k": 3
/// }))
/// .build();
///
/// let res = client.classification.schedule(req).await?;
/// Ok(())
/// }
/// ```
pub async fn schedule(
&self,
request: ClassificationRequest,
) -> Result<ClassificationResponse, Box<dyn Error>> {
let res = self
.client
.post(self.endpoint.clone())
.json(&request)
.send()
.await?;
match res.status() {
reqwest::StatusCode::CREATED => {
let res: ClassificationResponse = res.json().await?;
Ok(res)
}
_ => Err(self.get_err_msg("schedule classification", res).await)
}
}
/// Get the status of a classification
///
/// # Example
/// ```no_run
/// use uuid::Uuid;
/// use weaviate_community::WeaviateClient;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let uuid = Uuid::parse_str("00037775-1432-35e5-bc59-443baaef7d80")?;
/// let client = WeaviateClient::builder("http://localhost:8080").build()?;
///
/// let res = client.classification.get(uuid).await?;
/// Ok(())
/// }
/// ```
pub async fn get(&self, id: Uuid) -> Result<ClassificationResponse, Box<dyn Error>> {
let endpoint = self.endpoint.join(&id.to_string())?;
let res = self.client.get(endpoint).send().await?;
match res.status() {
reqwest::StatusCode::OK => {
let res: ClassificationResponse = res.json().await?;
Ok(res)
}
_ => Err(self.get_err_msg("get classification", res).await)
}
}
/// Get the error message for the endpoint
///
/// Made to reduce the boilerplate error message building
async fn get_err_msg(
&self,
endpoint: &str,
res: reqwest::Response
) -> Box<ClassificationError> {
let status_code = res.status();
let msg: Result<serde_json::Value, reqwest::Error> = res.json().await;
let r_str: String;
if let Ok(json) = msg {
r_str = format!(
"Status code `{}` received when calling {} endpoint. Response: {}",
status_code,
endpoint,
json,
);
} else {
r_str = format!(
"Status code `{}` received when calling {} endpoint.",
status_code,
endpoint
);
}
Box::new(ClassificationError(r_str))
}
}
#[cfg(test)]
mod tests {
use uuid::Uuid;
use crate::{
WeaviateClient,
collections::classification::{ClassificationRequest, ClassificationType}
};
fn get_test_harness() -> (mockito::ServerGuard, WeaviateClient) {
let mock_server = mockito::Server::new();
let mut host = "http://".to_string();
host.push_str(&mock_server.host_with_port());
let client = WeaviateClient::builder(&host).build().unwrap();
(mock_server, client)
}
fn test_classification_req() -> ClassificationRequest {
ClassificationRequest::builder()
.with_class("Test")
.with_type(ClassificationType::KNN)
.with_based_on_properties(vec!["testProp"])
.with_classify_properties(vec!["hasPopularity"])
.with_filters(serde_json::json!({
"path": ["testPropTwo"],
"operator": "GreaterThan",
"valueInt": 100
}))
.with_settings(serde_json::json!({"k": 3}))
.build()
}
fn mock_post(
server: &mut mockito::ServerGuard,
endpoint: &str,
status_code: usize,
body: &str,
) -> mockito::Mock {
server
.mock("POST", endpoint)
.with_status(status_code)
.with_header("content-type", "application/json")
.with_body(body)
.create()
}
fn mock_get(
server: &mut mockito::ServerGuard,
endpoint: &str,
status_code: usize,
body: &str,
) -> mockito::Mock {
server
.mock("GET", endpoint)
.with_status(status_code)
.with_header("content-type", "application/json")
.with_body(body)
.create()
}
#[tokio::test]
async fn test_classification_schedule_ok() {}
#[tokio::test]
async fn test_classification_schedule_err() {
let req = test_classification_req();
let (mut mock_server, client) = get_test_harness();
let mock = mock_post(&mut mock_server, "/v1/classifications/", 401, "");
let res = client.classification.schedule(req).await;
mock.assert();
assert!(res.is_err());
}
#[tokio::test]
async fn test_classification_get_ok() {}
#[tokio::test]
async fn test_classification_get_err() {
let uuid = Uuid::new_v4();
let mut url = String::from("/v1/classifications/");
url.push_str(&uuid.to_string());
let (mut mock_server, client) = get_test_harness();
let mock = mock_get(&mut mock_server, &url, 401, "");
let res = client.classification.get(uuid).await;
mock.assert();
assert!(res.is_err());
}
}