|
| 1 | +use hyper::HeaderMap; |
| 2 | +use reqwest::Response; |
| 3 | +use serde::Serialize; |
| 4 | +use uuid::Uuid; |
| 5 | + |
| 6 | +use crate::common::http::{Query, QueryParam, ReqwestQuery}; |
| 7 | +use crate::connection_info::ConnectionInfo; |
| 8 | + |
| 9 | +/// API Client |
| 10 | +pub struct Client { |
| 11 | + connection_info: ConnectionInfo, |
| 12 | + base_path: String, |
| 13 | +} |
| 14 | + |
| 15 | +impl Client { |
| 16 | + #[must_use] |
| 17 | + pub fn new(connection_info: ConnectionInfo) -> Self { |
| 18 | + Self { |
| 19 | + connection_info, |
| 20 | + base_path: "/api/v1/".to_string(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub async fn generate_auth_key(&self, seconds_valid: i32, headers: Option<HeaderMap>) -> Response { |
| 25 | + self.post_empty(&format!("key/{}", &seconds_valid), headers).await |
| 26 | + } |
| 27 | + |
| 28 | + pub async fn add_auth_key(&self, add_key_form: AddKeyForm, headers: Option<HeaderMap>) -> Response { |
| 29 | + self.post_form("keys", &add_key_form, headers).await |
| 30 | + } |
| 31 | + |
| 32 | + pub async fn delete_auth_key(&self, key: &str, headers: Option<HeaderMap>) -> Response { |
| 33 | + self.delete(&format!("key/{}", &key), headers).await |
| 34 | + } |
| 35 | + |
| 36 | + pub async fn reload_keys(&self, headers: Option<HeaderMap>) -> Response { |
| 37 | + self.get("keys/reload", Query::default(), headers).await |
| 38 | + } |
| 39 | + |
| 40 | + pub async fn whitelist_a_torrent(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response { |
| 41 | + self.post_empty(&format!("whitelist/{}", &info_hash), headers).await |
| 42 | + } |
| 43 | + |
| 44 | + pub async fn remove_torrent_from_whitelist(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response { |
| 45 | + self.delete(&format!("whitelist/{}", &info_hash), headers).await |
| 46 | + } |
| 47 | + |
| 48 | + pub async fn reload_whitelist(&self, headers: Option<HeaderMap>) -> Response { |
| 49 | + self.get("whitelist/reload", Query::default(), headers).await |
| 50 | + } |
| 51 | + |
| 52 | + pub async fn get_torrent(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response { |
| 53 | + self.get(&format!("torrent/{}", &info_hash), Query::default(), headers).await |
| 54 | + } |
| 55 | + |
| 56 | + pub async fn get_torrents(&self, params: Query, headers: Option<HeaderMap>) -> Response { |
| 57 | + self.get("torrents", params, headers).await |
| 58 | + } |
| 59 | + |
| 60 | + pub async fn get_tracker_statistics(&self, headers: Option<HeaderMap>) -> Response { |
| 61 | + self.get("stats", Query::default(), headers).await |
| 62 | + } |
| 63 | + |
| 64 | + pub async fn get(&self, path: &str, params: Query, headers: Option<HeaderMap>) -> Response { |
| 65 | + let mut query: Query = params; |
| 66 | + |
| 67 | + if let Some(token) = &self.connection_info.api_token { |
| 68 | + query.add_param(QueryParam::new("token", token)); |
| 69 | + }; |
| 70 | + |
| 71 | + self.get_request_with_query(path, query, headers).await |
| 72 | + } |
| 73 | + |
| 74 | + /// # Panics |
| 75 | + /// |
| 76 | + /// Will panic if the request can't be sent |
| 77 | + pub async fn post_empty(&self, path: &str, headers: Option<HeaderMap>) -> Response { |
| 78 | + let builder = reqwest::Client::new() |
| 79 | + .post(self.base_url(path).clone()) |
| 80 | + .query(&ReqwestQuery::from(self.query_with_token())); |
| 81 | + |
| 82 | + let builder = match headers { |
| 83 | + Some(headers) => builder.headers(headers), |
| 84 | + None => builder, |
| 85 | + }; |
| 86 | + |
| 87 | + builder.send().await.unwrap() |
| 88 | + } |
| 89 | + |
| 90 | + /// # Panics |
| 91 | + /// |
| 92 | + /// Will panic if the request can't be sent |
| 93 | + pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T, headers: Option<HeaderMap>) -> Response { |
| 94 | + let builder = reqwest::Client::new() |
| 95 | + .post(self.base_url(path).clone()) |
| 96 | + .query(&ReqwestQuery::from(self.query_with_token())) |
| 97 | + .json(&form); |
| 98 | + |
| 99 | + let builder = match headers { |
| 100 | + Some(headers) => builder.headers(headers), |
| 101 | + None => builder, |
| 102 | + }; |
| 103 | + |
| 104 | + builder.send().await.unwrap() |
| 105 | + } |
| 106 | + |
| 107 | + /// # Panics |
| 108 | + /// |
| 109 | + /// Will panic if the request can't be sent |
| 110 | + async fn delete(&self, path: &str, headers: Option<HeaderMap>) -> Response { |
| 111 | + let builder = reqwest::Client::new() |
| 112 | + .delete(self.base_url(path).clone()) |
| 113 | + .query(&ReqwestQuery::from(self.query_with_token())); |
| 114 | + |
| 115 | + let builder = match headers { |
| 116 | + Some(headers) => builder.headers(headers), |
| 117 | + None => builder, |
| 118 | + }; |
| 119 | + |
| 120 | + builder.send().await.unwrap() |
| 121 | + } |
| 122 | + |
| 123 | + pub async fn get_request_with_query(&self, path: &str, params: Query, headers: Option<HeaderMap>) -> Response { |
| 124 | + get(&self.base_url(path), Some(params), headers).await |
| 125 | + } |
| 126 | + |
| 127 | + pub async fn get_request(&self, path: &str) -> Response { |
| 128 | + get(&self.base_url(path), None, None).await |
| 129 | + } |
| 130 | + |
| 131 | + fn query_with_token(&self) -> Query { |
| 132 | + match &self.connection_info.api_token { |
| 133 | + Some(token) => Query::params([QueryParam::new("token", token)].to_vec()), |
| 134 | + None => Query::default(), |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + fn base_url(&self, path: &str) -> String { |
| 139 | + format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// # Panics |
| 144 | +/// |
| 145 | +/// Will panic if the request can't be sent |
| 146 | +pub async fn get(path: &str, query: Option<Query>, headers: Option<HeaderMap>) -> Response { |
| 147 | + let builder = reqwest::Client::builder().build().unwrap(); |
| 148 | + |
| 149 | + let builder = match query { |
| 150 | + Some(params) => builder.get(path).query(&ReqwestQuery::from(params)), |
| 151 | + None => builder.get(path), |
| 152 | + }; |
| 153 | + |
| 154 | + let builder = match headers { |
| 155 | + Some(headers) => builder.headers(headers), |
| 156 | + None => builder, |
| 157 | + }; |
| 158 | + |
| 159 | + builder.send().await.unwrap() |
| 160 | +} |
| 161 | + |
| 162 | +/// Returns a `HeaderMap` with a request id header |
| 163 | +/// |
| 164 | +/// # Panics |
| 165 | +/// |
| 166 | +/// Will panic if the request ID can't be parsed into a string. |
| 167 | +#[must_use] |
| 168 | +pub fn headers_with_request_id(request_id: Uuid) -> HeaderMap { |
| 169 | + let mut headers = HeaderMap::new(); |
| 170 | + headers.insert("x-request-id", request_id.to_string().parse().unwrap()); |
| 171 | + headers |
| 172 | +} |
| 173 | + |
| 174 | +#[derive(Serialize, Debug)] |
| 175 | +pub struct AddKeyForm { |
| 176 | + #[serde(rename = "key")] |
| 177 | + pub opt_key: Option<String>, |
| 178 | + pub seconds_valid: Option<u64>, |
| 179 | +} |
0 commit comments