Skip to content

Commit a3172b1

Browse files
committed
refactor(http-protocol): merge announce_builder::Query into announce::Announce
- Merged `announce_builder::Query` and its builder into `announce::Announce` - Added `peer_addr: Option<IpAddr>` field per BEP 3 - Added `Display` impl for URL query string serialization - Added `AnnounceBuilder` with ergonomic construction (u64→NumberOfBytes conversion) - Added `extract_peer_addr()` — invalid peer_addr values are silently ignored (BEP 3 optional) - Removed `announce_builder` module (deleted announce_builder.rs and mod.rs reference) - Updated ~54 call sites across 7 files - All linters and tests pass
1 parent b3cb02d commit a3172b1

14 files changed

Lines changed: 372 additions & 370 deletions

File tree

console/tracker-client/src/console/clients/checker/checks/http.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::time::Duration;
44
use serde::Serialize;
55
use torrust_info_hash::InfoHash;
66
use torrust_tracker_client::http::client::Client;
7-
use torrust_tracker_http_protocol::v1::requests::{announce_builder, scrape_builder};
7+
use torrust_tracker_http_protocol::v1::requests::announce::AnnounceBuilder;
8+
use torrust_tracker_http_protocol::v1::requests::scrape_builder;
89
use torrust_tracker_http_protocol::v1::responses::announce_deserialization::Announce;
910
use torrust_tracker_http_protocol::v1::responses::scrape_deserialization;
1011
use url::Url;
@@ -68,11 +69,7 @@ async fn check_http_announce(url: &Url, timeout: Duration) -> Result<Announce, E
6869
let client = Client::new(url.clone(), timeout).map_err(|err| Error::HttpClientError { err })?;
6970

7071
let response = client
71-
.announce(
72-
&announce_builder::QueryBuilder::with_default_values()
73-
.with_info_hash(&info_hash)
74-
.query(),
75-
)
72+
.announce(&AnnounceBuilder::with_default_values().with_info_hash(&info_hash).query())
7673
.await
7774
.map_err(|err| Error::HttpClientError { err })?;
7875

console/tracker-client/src/console/clients/http/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use reqwest::Url;
7979
use torrust_info_hash::InfoHash;
8080
use torrust_peer_id::PeerId;
8181
use torrust_tracker_client::http::client::Client;
82-
use torrust_tracker_http_protocol::v1::requests::announce_builder::{Compact, Event, QueryBuilder};
82+
use torrust_tracker_http_protocol::v1::requests::announce::{AnnounceBuilder, Compact, Event};
8383
use torrust_tracker_http_protocol::v1::requests::scrape_builder;
8484
use torrust_tracker_http_protocol::v1::responses::announce_deserialization::{Announce, DeserializedCompact};
8585
use torrust_tracker_http_protocol::v1::responses::scrape_deserialization;
@@ -238,7 +238,7 @@ async fn announce_command(options: AnnounceOptions, timeout: Duration) -> anyhow
238238
)
239239
})?;
240240

241-
let mut query_builder = QueryBuilder::with_default_values().with_info_hash(&info_hash);
241+
let mut query_builder = AnnounceBuilder::with_default_values().with_info_hash(&info_hash);
242242

243243
if let Some(event) = options.event {
244244
query_builder = query_builder.with_event(event.into());
@@ -256,7 +256,7 @@ async fn announce_command(options: AnnounceOptions, timeout: Duration) -> anyhow
256256
query_builder = query_builder.with_port(port);
257257
}
258258
if let Some(peer_addr) = options.peer_addr {
259-
query_builder = query_builder.with_peer_addr(&peer_addr);
259+
query_builder = query_builder.with_peer_addr(peer_addr);
260260
}
261261
if let Some(peer_id) = options.peer_id {
262262
query_builder = query_builder.with_peer_id(&peer_id);

console/tracker-client/src/console/clients/unified/http.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use reqwest::Url;
99
use torrust_info_hash::InfoHash;
1010
use torrust_peer_id::PeerId;
1111
use torrust_tracker_client::http::client::Client;
12-
use torrust_tracker_http_protocol::v1::requests::announce_builder::{Compact, Event, QueryBuilder};
12+
use torrust_tracker_http_protocol::v1::requests::announce::{AnnounceBuilder, Compact, Event};
1313
use torrust_tracker_http_protocol::v1::requests::scrape_builder;
1414
use torrust_tracker_http_protocol::v1::responses::announce_deserialization::{Announce, DeserializedCompact};
1515
use torrust_tracker_http_protocol::v1::responses::scrape_deserialization;
@@ -154,7 +154,7 @@ async fn announce_command(options: AnnounceOptions, timeout: Duration) -> anyhow
154154
)
155155
})?;
156156

157-
let mut query_builder = QueryBuilder::with_default_values().with_info_hash(&info_hash);
157+
let mut query_builder = AnnounceBuilder::with_default_values().with_info_hash(&info_hash);
158158

159159
if let Some(event) = options.event {
160160
query_builder = query_builder.with_event(event.into());
@@ -172,7 +172,7 @@ async fn announce_command(options: AnnounceOptions, timeout: Duration) -> anyhow
172172
query_builder = query_builder.with_port(port);
173173
}
174174
if let Some(peer_addr) = options.peer_addr {
175-
query_builder = query_builder.with_peer_addr(&peer_addr);
175+
query_builder = query_builder.with_peer_addr(peer_addr);
176176
}
177177
if let Some(peer_id) = options.peer_id {
178178
query_builder = query_builder.with_peer_id(&peer_id);

packages/axum-http-server/src/v1/extractors/announce_request.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ fn extract_announce_from(maybe_raw_query: Option<&str>) -> Result<Announce, resp
8484

8585
#[cfg(test)]
8686
mod tests {
87+
use std::net::{IpAddr, Ipv4Addr};
8788
use std::str::FromStr;
8889

8990
use torrust_info_hash::InfoHash;
@@ -112,6 +113,7 @@ mod tests {
112113
info_hash: InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap(), // DevSkim: ignore DS173237
113114
peer_id: PeerId(*b"-qB00000000000000001"),
114115
port: 17548,
116+
peer_addr: Some(IpAddr::V4(Ipv4Addr::new(2, 137, 87, 41))),
115117
downloaded: Some(NumberOfBytes::new(0)),
116118
uploaded: Some(NumberOfBytes::new(0)),
117119
left: Some(NumberOfBytes::new(0)),

packages/axum-http-server/src/v1/handlers/announce.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ mod tests {
220220
info_hash: sample_info_hash(),
221221
peer_id: PeerId(*b"-qB00000000000000001"),
222222
port: 17548,
223+
peer_addr: None,
223224
downloaded: None,
224225
uploaded: None,
225226
left: None,

packages/axum-http-server/tests/server/client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::net::IpAddr;
22

33
use reqwest::{Client as ReqwestClient, Response};
44
use torrust_tracker_core::authentication::Key;
5-
use torrust_tracker_http_protocol::v1::requests::{announce_builder, scrape_builder};
5+
use torrust_tracker_http_protocol::v1::requests::announce::Announce;
6+
use torrust_tracker_http_protocol::v1::requests::scrape_builder;
67

78
/// HTTP Tracker Client
89
pub struct Client {
@@ -45,15 +46,15 @@ impl Client {
4546
}
4647
}
4748

48-
pub async fn announce(&self, query: &announce_builder::Query) -> Response {
49+
pub async fn announce(&self, query: &Announce) -> Response {
4950
self.get(&self.build_announce_path_and_query(query)).await
5051
}
5152

5253
pub async fn scrape(&self, query: &scrape_builder::Query) -> Response {
5354
self.get(&self.build_scrape_path_and_query(query)).await
5455
}
5556

56-
pub async fn announce_with_header(&self, query: &announce_builder::Query, key: &str, value: &str) -> Response {
57+
pub async fn announce_with_header(&self, query: &Announce, key: &str, value: &str) -> Response {
5758
self.get_with_header(&self.build_announce_path_and_query(query), key, value)
5859
.await
5960
}
@@ -75,7 +76,7 @@ impl Client {
7576
.unwrap()
7677
}
7778

78-
fn build_announce_path_and_query(&self, query: &announce_builder::Query) -> String {
79+
fn build_announce_path_and_query(&self, query: &Announce) -> String {
7980
format!("{}?{query}", self.build_path("announce"))
8081
}
8182

0 commit comments

Comments
 (0)