Skip to content

Commit

Permalink
Merge #483: Add timeouts to Tracker API Client requests
Browse files Browse the repository at this point in the history
3f629c2 refactor: [#473] tracker API client. Remove duplicate code (Jose Celano)
7256b46 feat: [#473] add timeout to Tracker API Client requests (Jose Celano)

Pull request description:

  Ass default timeout of 5 seconds for Tracker API requests.

  In the future, we could use the official Tracker API client. See torrust/torrust-tracker#689.

  However, it's also fine to use this reduced client. Because we do not need all the PAI endpoints.

ACKs for top commit:
  josecelano:
    ACK 3f629c2

Tree-SHA512: fc22ceac6ef234fb1bd20b6ae5099cea17ddfe2cb599faa3ad6f3e2ae04d93043e6af0b9a85dc41785f0a99bf7ace13f8e5e0cb656058390bf20585b236bf5e6
  • Loading branch information
josecelano committed Feb 12, 2024
2 parents 5a854f9 + 3f629c2 commit 384e66d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
57 changes: 26 additions & 31 deletions src/tracker/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::time::Duration;

use reqwest::{Error, Response};
pub struct ConnectionInfo {
/// The URL of the tracker. Eg: <https://tracker:7070> or <udp://tracker:6969>
/// The URL of the tracker API. Eg: <https://tracker:1212>.
pub url: String,
/// The token used to authenticate with the tracker API.
pub token: String,
Expand All @@ -15,17 +17,26 @@ impl ConnectionInfo {

pub struct Client {
pub connection_info: ConnectionInfo,
base_url: String,
api_base_url: String,
client: reqwest::Client,
token_param: [(String, String); 1],
}

impl Client {
#[must_use]
pub fn new(connection_info: ConnectionInfo) -> Self {
/// # Errors
///
/// Will fails if it can't build a HTTP client with a timeout.
pub fn new(connection_info: ConnectionInfo) -> Result<Self, Error> {
let base_url = format!("{}/api/v1", connection_info.url);
Self {
let client = reqwest::Client::builder().timeout(Duration::from_secs(5)).build()?;
let token_param = [("token".to_string(), connection_info.token.to_string())];

Ok(Self {
connection_info,
base_url,
}
api_base_url: base_url,
client,
token_param,
})
}

/// Add a torrent to the tracker whitelist.
Expand All @@ -34,13 +45,9 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

let params = [("token", &self.connection_info.token)];
let request_url = format!("{}/whitelist/{}", self.api_base_url, info_hash);

client.post(request_url).query(&params).send().await
self.client.post(request_url).query(&self.token_param).send().await
}

/// Remove a torrent from the tracker whitelist.
Expand All @@ -49,13 +56,9 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);
let request_url = format!("{}/whitelist/{}", self.api_base_url, info_hash);

let client = reqwest::Client::new();

let params = [("token", &self.connection_info.token)];

client.delete(request_url).query(&params).send().await
self.client.delete(request_url).query(&self.token_param).send().await
}

/// Retrieve a new tracker key.
Expand All @@ -64,13 +67,9 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result<Response, Error> {
let request_url = format!("{}/key/{}", self.base_url, token_valid_seconds);

let client = reqwest::Client::new();
let request_url = format!("{}/key/{}", self.api_base_url, token_valid_seconds);

let params = [("token", &self.connection_info.token)];

client.post(request_url).query(&params).send().await
self.client.post(request_url).query(&self.token_param).send().await
}

/// Retrieve the info for a torrent.
Expand All @@ -79,12 +78,8 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn get_torrent_info(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!("{}/torrent/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

let params = [("token", &self.connection_info.token)];
let request_url = format!("{}/torrent/{}", self.api_base_url, info_hash);

client.get(request_url).query(&params).send().await
self.client.get(request_url).query(&self.token_param).send().await
}
}
6 changes: 5 additions & 1 deletion src/tracker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ pub struct Service {
}

impl Service {
/// # Panics
///
/// Will panic if it can't build a Tracker API client.
pub async fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> Service {
let settings = cfg.settings.read().await;
let api_client = Client::new(ConnectionInfo::new(
settings.tracker.api_url.clone(),
settings.tracker.token.clone(),
));
))
.expect("a reqwest client should be provided");
let token_valid_seconds = settings.tracker.token_valid_seconds;
let tracker_url = settings.tracker.url.clone();
drop(settings);
Expand Down

0 comments on commit 384e66d

Please sign in to comment.