|
| 1 | +pub mod requests; |
| 2 | +pub mod responses; |
| 3 | + |
| 4 | +use std::net::IpAddr; |
| 5 | + |
| 6 | +use requests::announce::{self, Query}; |
| 7 | +use requests::scrape; |
| 8 | +use reqwest::{Client as ReqwestClient, Response, Url}; |
| 9 | + |
| 10 | +use crate::core::auth::Key; |
| 11 | + |
| 12 | +/// HTTP Tracker Client |
| 13 | +pub struct Client { |
| 14 | + base_url: Url, |
| 15 | + reqwest: ReqwestClient, |
| 16 | + key: Option<Key>, |
| 17 | +} |
| 18 | + |
| 19 | +/// URL components in this context: |
| 20 | +/// |
| 21 | +/// ```text |
| 22 | +/// http://127.0.0.1:62304/announce/YZ....rJ?info_hash=%9C8B%22%13%E3%0B%FF%21%2B0%C3%60%D2o%9A%02%13d%22 |
| 23 | +/// \_____________________/\_______________/ \__________________________________________________________/ |
| 24 | +/// | | | |
| 25 | +/// base url path query |
| 26 | +/// ``` |
| 27 | +impl Client { |
| 28 | + /// # Panics |
| 29 | + /// |
| 30 | + /// This method fails if the client builder fails. |
| 31 | + #[must_use] |
| 32 | + pub fn new(base_url: Url) -> Self { |
| 33 | + Self { |
| 34 | + base_url, |
| 35 | + reqwest: reqwest::Client::builder().build().unwrap(), |
| 36 | + key: None, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Creates the new client binding it to an specific local address. |
| 41 | + /// |
| 42 | + /// # Panics |
| 43 | + /// |
| 44 | + /// This method fails if the client builder fails. |
| 45 | + #[must_use] |
| 46 | + pub fn bind(base_url: Url, local_address: IpAddr) -> Self { |
| 47 | + Self { |
| 48 | + base_url, |
| 49 | + reqwest: reqwest::Client::builder().local_address(local_address).build().unwrap(), |
| 50 | + key: None, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// # Panics |
| 55 | + /// |
| 56 | + /// This method fails if the client builder fails. |
| 57 | + #[must_use] |
| 58 | + pub fn authenticated(base_url: Url, key: Key) -> Self { |
| 59 | + Self { |
| 60 | + base_url, |
| 61 | + reqwest: reqwest::Client::builder().build().unwrap(), |
| 62 | + key: Some(key), |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + pub async fn announce(&self, query: &announce::Query) -> Response { |
| 67 | + self.get(&self.build_announce_path_and_query(query)).await |
| 68 | + } |
| 69 | + |
| 70 | + pub async fn scrape(&self, query: &scrape::Query) -> Response { |
| 71 | + self.get(&self.build_scrape_path_and_query(query)).await |
| 72 | + } |
| 73 | + |
| 74 | + pub async fn announce_with_header(&self, query: &Query, key: &str, value: &str) -> Response { |
| 75 | + self.get_with_header(&self.build_announce_path_and_query(query), key, value) |
| 76 | + .await |
| 77 | + } |
| 78 | + |
| 79 | + pub async fn health_check(&self) -> Response { |
| 80 | + self.get(&self.build_path("health_check")).await |
| 81 | + } |
| 82 | + |
| 83 | + /// # Panics |
| 84 | + /// |
| 85 | + /// This method fails if there was an error while sending request. |
| 86 | + pub async fn get(&self, path: &str) -> Response { |
| 87 | + self.reqwest.get(self.build_url(path)).send().await.unwrap() |
| 88 | + } |
| 89 | + |
| 90 | + /// # Panics |
| 91 | + /// |
| 92 | + /// This method fails if there was an error while sending request. |
| 93 | + pub async fn get_with_header(&self, path: &str, key: &str, value: &str) -> Response { |
| 94 | + self.reqwest |
| 95 | + .get(self.build_url(path)) |
| 96 | + .header(key, value) |
| 97 | + .send() |
| 98 | + .await |
| 99 | + .unwrap() |
| 100 | + } |
| 101 | + |
| 102 | + fn build_announce_path_and_query(&self, query: &announce::Query) -> String { |
| 103 | + format!("{}?{query}", self.build_path("announce")) |
| 104 | + } |
| 105 | + |
| 106 | + fn build_scrape_path_and_query(&self, query: &scrape::Query) -> String { |
| 107 | + format!("{}?{query}", self.build_path("scrape")) |
| 108 | + } |
| 109 | + |
| 110 | + fn build_path(&self, path: &str) -> String { |
| 111 | + match &self.key { |
| 112 | + Some(key) => format!("{path}/{key}"), |
| 113 | + None => path.to_string(), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fn build_url(&self, path: &str) -> String { |
| 118 | + let base_url = self.base_url(); |
| 119 | + format!("{base_url}{path}") |
| 120 | + } |
| 121 | + |
| 122 | + fn base_url(&self) -> String { |
| 123 | + self.base_url.to_string() |
| 124 | + } |
| 125 | +} |
0 commit comments