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
use enum_as_inner::EnumAsInner;
use reqwest::Response;
use serde::Deserialize;
use serde_json::Value;
use std::error::Error;
pub type APIResponse = Result<Response, Box<dyn Error>>;
pub type IndexResults = Result<Vec<IndexResult>, Box<dyn Error>>;
pub type IndexResultsBatch = Result<Vec<Vec<IndexResult>>, Box<dyn Error>>;
pub type Strings = Result<String, Box<dyn Error>>;
pub type StringsBatch = Result<Vec<String>, Box<dyn Error>>;
pub type Texts = Result<Text, Box<dyn Error>>;
pub type TextsBatch = Result<Vec<Text>, Box<dyn Error>>;
pub struct API {
url: String
}
impl API {
pub fn new(url: &str) -> API {
API { url: url.to_string()}
}
pub async fn get(&self, method: &str, params: &[(&str, &str)]) -> APIResponse {
let url = format!("{url}/{method}", url=self.url, method=method);
let client = reqwest::Client::new();
let request = client.get(&url).query(¶ms);
Ok(request.send().await?)
}
pub async fn post(&self, method: &str, json: &Value) -> APIResponse {
let url = format!("{url}/{method}", url=self.url, method=method);
let client = reqwest::Client::new();
let request = client.post(&url).json(&json);
Ok(request.send().await?)
}
}
#[derive(Debug, Deserialize)]
pub struct IndexResult {
pub id: usize,
pub score: f32
}
#[derive(Debug, Deserialize, EnumAsInner)]
#[serde(untagged)]
pub enum Text {
String(String),
List(Vec<String>)
}