forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
138 lines (113 loc) · 4.06 KB
/
client.rs
File metadata and controls
138 lines (113 loc) · 4.06 KB
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
use reqwest::Response;
use serde::Serialize;
use crate::common::http::{Query, QueryParam, ReqwestQuery};
use crate::servers::api::connection_info::ConnectionInfo;
/// API Client
pub struct Client {
connection_info: ConnectionInfo,
base_path: String,
}
impl Client {
pub fn new(connection_info: ConnectionInfo) -> Self {
Self {
connection_info,
base_path: "/api/v1/".to_string(),
}
}
pub async fn generate_auth_key(&self, seconds_valid: i32) -> Response {
self.post_empty(&format!("key/{}", &seconds_valid)).await
}
pub async fn add_auth_key(&self, add_key_form: AddKeyForm) -> Response {
self.post_form("keys", &add_key_form).await
}
pub async fn delete_auth_key(&self, key: &str) -> Response {
self.delete(&format!("key/{}", &key)).await
}
pub async fn reload_keys(&self) -> Response {
self.get("keys/reload", Query::default()).await
}
pub async fn whitelist_a_torrent(&self, info_hash: &str) -> Response {
self.post_empty(&format!("whitelist/{}", &info_hash)).await
}
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Response {
self.delete(&format!("whitelist/{}", &info_hash)).await
}
pub async fn reload_whitelist(&self) -> Response {
self.get("whitelist/reload", Query::default()).await
}
pub async fn get_torrent(&self, info_hash: &str) -> Response {
self.get(&format!("torrent/{}", &info_hash), Query::default()).await
}
pub async fn get_torrents(&self, params: Query) -> Response {
self.get("torrents", params).await
}
pub async fn get_tracker_statistics(&self) -> Response {
self.get("stats", Query::default()).await
}
pub async fn get(&self, path: &str, params: Query) -> Response {
let mut query: Query = params;
if let Some(token) = &self.connection_info.api_token {
query.add_param(QueryParam::new("token", token));
};
self.get_request_with_query(path, query).await
}
pub async fn post_empty(&self, path: &str) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
}
pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.json(&form)
.send()
.await
.unwrap()
}
async fn delete(&self, path: &str) -> Response {
reqwest::Client::new()
.delete(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
}
pub async fn get_request_with_query(&self, path: &str, params: Query) -> Response {
get(&self.base_url(path), Some(params)).await
}
pub async fn get_request(&self, path: &str) -> Response {
get(&self.base_url(path), None).await
}
fn query_with_token(&self) -> Query {
match &self.connection_info.api_token {
Some(token) => Query::params([QueryParam::new("token", token)].to_vec()),
None => Query::default(),
}
}
fn base_url(&self, path: &str) -> String {
format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path)
}
}
pub async fn get(path: &str, query: Option<Query>) -> Response {
match query {
Some(params) => reqwest::Client::builder()
.build()
.unwrap()
.get(path)
.query(&ReqwestQuery::from(params))
.send()
.await
.unwrap(),
None => reqwest::Client::builder().build().unwrap().get(path).send().await.unwrap(),
}
}
#[derive(Serialize, Debug)]
pub struct AddKeyForm {
#[serde(rename = "key")]
pub opt_key: Option<String>,
pub seconds_valid: Option<u64>,
}