Skip to content

Commit 0198361

Browse files
committed
refactor: rename structs and functions, and add docs
1 parent 00926e1 commit 0198361

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::config::Configuration;
1414
use crate::databases::database::connect_database;
1515
use crate::mailer::MailerService;
1616
use crate::routes;
17-
use crate::tracker::service::TrackerService;
17+
use crate::tracker::service::Service;
1818

1919
pub struct Running {
2020
pub api_server: Server,
@@ -44,7 +44,7 @@ pub async fn run(configuration: Configuration) -> Running {
4444

4545
let database = Arc::new(connect_database(&database_connect_url).await.expect("Database error."));
4646
let auth = Arc::new(AuthorizationService::new(cfg.clone(), database.clone()));
47-
let tracker_service = Arc::new(TrackerService::new(cfg.clone(), database.clone()).await);
47+
let tracker_service = Arc::new(Service::new(cfg.clone(), database.clone()).await);
4848
let mailer_service = Arc::new(MailerService::new(cfg.clone()).await);
4949
let image_cache_service = Arc::new(ImageCacheService::new(cfg.clone()).await);
5050

src/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::cache::image::manager::ImageCacheService;
55
use crate::config::Configuration;
66
use crate::databases::database::Database;
77
use crate::mailer::MailerService;
8-
use crate::tracker::service::TrackerService;
8+
use crate::tracker::service::Service;
99

1010
pub type Username = String;
1111

@@ -15,7 +15,7 @@ pub struct AppData {
1515
pub cfg: Arc<Configuration>,
1616
pub database: Arc<Box<dyn Database>>,
1717
pub auth: Arc<AuthorizationService>,
18-
pub tracker: Arc<TrackerService>,
18+
pub tracker: Arc<Service>,
1919
pub mailer: Arc<MailerService>,
2020
pub image_cache_manager: Arc<ImageCacheService>,
2121
}
@@ -25,7 +25,7 @@ impl AppData {
2525
cfg: Arc<Configuration>,
2626
database: Arc<Box<dyn Database>>,
2727
auth: Arc<AuthorizationService>,
28-
tracker: Arc<TrackerService>,
28+
tracker: Arc<Service>,
2929
mailer: Arc<MailerService>,
3030
image_cache_manager: Arc<ImageCacheService>,
3131
) -> AppData {

src/console/commands/import_tracker_statistics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use text_colorizer::*;
99
use crate::bootstrap::config::init_configuration;
1010
use crate::bootstrap::logging;
1111
use crate::databases::database::connect_database;
12-
use crate::tracker::service::TrackerService;
12+
use crate::tracker::service::Service;
1313

1414
const NUMBER_OF_ARGUMENTS: usize = 0;
1515

@@ -76,7 +76,7 @@ pub async fn import(_args: &Arguments) {
7676
.expect("Database error."),
7777
);
7878

79-
let tracker_service = Arc::new(TrackerService::new(cfg.clone(), database.clone()).await);
79+
let tracker_service = Arc::new(Service::new(cfg.clone(), database.clone()).await);
8080

8181
tracker_service.update_torrents().await.unwrap();
8282
}

src/tracker/api.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
use reqwest::{Error, Response};
2-
pub struct ApiConnectionInfo {
2+
pub struct ConnectionInfo {
3+
/// The URL of the tracker. Eg: <https://tracker:7070> or <udp://tracker:6969>
34
pub url: String,
5+
/// The token used to authenticate with the tracker API.
46
pub token: String,
57
}
68

7-
impl ApiConnectionInfo {
9+
impl ConnectionInfo {
10+
#[must_use]
811
pub fn new(url: String, token: String) -> Self {
912
Self { url, token }
1013
}
1114
}
1215

13-
pub struct ApiClient {
14-
pub connection_info: ApiConnectionInfo,
16+
pub struct Client {
17+
pub connection_info: ConnectionInfo,
1518
base_url: String,
1619
}
1720

18-
impl ApiClient {
19-
pub fn new(connection_info: ApiConnectionInfo) -> Self {
21+
impl Client {
22+
#[must_use]
23+
pub fn new(connection_info: ConnectionInfo) -> Self {
2024
let base_url = format!("{}/api/v1", connection_info.url);
2125
Self {
2226
connection_info,
2327
base_url,
2428
}
2529
}
2630

27-
pub async fn whitelist_info_hash(&self, info_hash: &str) -> Result<Response, Error> {
31+
/// Add a torrent to the tracker whitelist.
32+
///
33+
/// # Errors
34+
///
35+
/// Will return an error if the HTTP request fails.
36+
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
2837
let request_url = format!(
2938
"{}/whitelist/{}?token={}",
3039
self.base_url, info_hash, self.connection_info.token
@@ -35,7 +44,12 @@ impl ApiClient {
3544
client.post(request_url).send().await
3645
}
3746

38-
pub async fn remove_info_hash_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
47+
/// Remove a torrent from the tracker whitelist.
48+
///
49+
/// # Errors
50+
///
51+
/// Will return an error if the HTTP request fails.
52+
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
3953
let request_url = format!(
4054
"{}/whitelist/{}?token={}",
4155
self.base_url, info_hash, self.connection_info.token
@@ -46,6 +60,11 @@ impl ApiClient {
4660
client.delete(request_url).send().await
4761
}
4862

63+
/// Retrieve a new tracker key.
64+
///
65+
/// # Errors
66+
///
67+
/// Will return an error if the HTTP request fails.
4968
pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result<Response, Error> {
5069
let request_url = format!(
5170
"{}/key/{}?token={}",
@@ -57,6 +76,11 @@ impl ApiClient {
5776
client.post(request_url).send().await
5877
}
5978

79+
/// Retrieve the info for a torrent.
80+
///
81+
/// # Errors
82+
///
83+
/// Will return an error if the HTTP request fails.
6084
pub async fn get_torrent_info(&self, info_hash: &str) -> Result<Response, Error> {
6185
let request_url = format!("{}/torrent/{}?token={}", self.base_url, info_hash, self.connection_info.token);
6286

src/tracker/service.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use log::{error, info};
44
use serde::{Deserialize, Serialize};
55

6-
use super::api::{ApiClient, ApiConnectionInfo};
6+
use super::api::{Client, ConnectionInfo};
77
use crate::config::Configuration;
88
use crate::databases::database::Database;
99
use crate::errors::ServiceError;
@@ -35,24 +35,24 @@ pub struct PeerId {
3535
pub client: Option<String>,
3636
}
3737

38-
pub struct TrackerService {
38+
pub struct Service {
3939
database: Arc<Box<dyn Database>>,
40-
api_client: ApiClient,
40+
api_client: Client,
4141
token_valid_seconds: u64,
4242
tracker_url: String,
4343
}
4444

45-
impl TrackerService {
46-
pub async fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> TrackerService {
45+
impl Service {
46+
pub async fn new(cfg: Arc<Configuration>, database: Arc<Box<dyn Database>>) -> Service {
4747
let settings = cfg.settings.read().await;
48-
let api_client = ApiClient::new(ApiConnectionInfo::new(
48+
let api_client = Client::new(ConnectionInfo::new(
4949
settings.tracker.api_url.clone(),
5050
settings.tracker.token.clone(),
5151
));
5252
let token_valid_seconds = settings.tracker.token_valid_seconds;
5353
let tracker_url = settings.tracker.url.clone();
5454
drop(settings);
55-
TrackerService {
55+
Service {
5656
database,
5757
api_client,
5858
token_valid_seconds,
@@ -67,7 +67,7 @@ impl TrackerService {
6767
/// Will return an error if the HTTP request failed (for example if the
6868
/// tracker API is offline) or if the tracker API returned an error.
6969
pub async fn whitelist_info_hash(&self, info_hash: String) -> Result<(), ServiceError> {
70-
let response = self.api_client.whitelist_info_hash(&info_hash).await;
70+
let response = self.api_client.whitelist_torrent(&info_hash).await;
7171

7272
match response {
7373
Ok(response) => {
@@ -88,7 +88,7 @@ impl TrackerService {
8888
/// Will return an error if the HTTP request failed (for example if the
8989
/// tracker API is offline) or if the tracker API returned an error.
9090
pub async fn remove_info_hash_from_whitelist(&self, info_hash: String) -> Result<(), ServiceError> {
91-
let response = self.api_client.remove_info_hash_from_whitelist(&info_hash).await;
91+
let response = self.api_client.remove_torrent_from_whitelist(&info_hash).await;
9292

9393
match response {
9494
Ok(response) => {

0 commit comments

Comments
 (0)