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
#[deny(missing_docs)]
extern crate base64;
extern crate reqwest;
extern crate serde;
extern crate snafu;
use serde::Serialize;
use snafu::{ResultExt, Snafu};
pub mod types;
pub use types::Doc;
use types::{Me, SearchRequest, SearchResponse};
pub struct Client {
pub base_uri: String,
pub token: Option<String>,
client: reqwest::Client,
}
impl Client {
pub fn new() -> Self {
Self {
base_uri: "https://trace.moe/api".into(),
client: reqwest::Client::new(),
token: None,
}
}
pub fn with_token(token: String) -> Self {
Self {
token: Some(token),
..Self::new()
}
}
pub fn search(&self, image: Vec<u8>) -> Result<SearchResponse> {
let body = SearchRequest::new(image);
let mut response = self.request(reqwest::Method::POST, "search", &body)?;
match response.status().as_u16() {
400 => Err(Error::ImageEmpty),
403 => Err(Error::InvalidToken),
413 => Err(Error::ImageTooLarge),
429 => Err(Error::RateLimit {
message: response.text().context(ResponseEmpty)?,
}),
500 | 503 => Err(Error::InternalServerError),
_ => response.json().context(JsonFailed),
}
}
pub fn me(&self) -> Result<Me> {
self.request(reqwest::Method::GET, "me", "")?
.json()
.context(JsonFailed)
}
fn request<T>(&self, method: reqwest::Method, path: &str, body: &T) -> Result<reqwest::Response>
where
T: Serialize + ?Sized,
{
let url = format!("{}/{}", self.base_uri, path);
let mut request = self.client.request(method, &url);
if let Some(token) = &self.token {
request = request.query(&[("token", token)])
}
request.json(body).send().context(RequestFailed)
}
}
#[derive(Debug, Snafu)]
pub enum Error {
RequestFailed { source: reqwest::Error },
JsonFailed { source: reqwest::Error },
ResponseEmpty { source: reqwest::Error },
ImageEmpty,
InvalidToken,
ImageTooLarge,
RateLimit { message: String },
InternalServerError,
}
pub type Result<T, E = Error> = std::result::Result<T, E>;