Skip to content

Commit 02dfe3e

Browse files
committed
refactor(api): [torrust#143] rename response functions
1 parent 6955666 commit 02dfe3e

2 files changed

Lines changed: 48 additions & 48 deletions

File tree

src/apis/handlers.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use axum::response::{Json, Response};
88
use serde::{de, Deserialize, Deserializer};
99

1010
use super::responses::{
11-
response_auth_key, response_failed_to_delete_key, response_failed_to_generate_key, response_failed_to_reload_keys,
12-
response_failed_to_reload_whitelist, response_failed_to_remove_torrent_from_whitelist, response_failed_to_whitelist_torrent,
13-
response_invalid_auth_key_param, response_invalid_info_hash_param, response_ok, response_stats, response_torrent_info,
14-
response_torrent_list, response_torrent_not_known,
11+
auth_key_response, failed_to_delete_key_response, failed_to_generate_key_response, failed_to_reload_keys_response,
12+
failed_to_reload_whitelist_response, failed_to_remove_torrent_from_whitelist_response, failed_to_whitelist_torrent_response,
13+
invalid_auth_key_param_response, invalid_info_hash_param_response, ok_response, stats_response, torrent_info_response,
14+
torrent_list_response, torrent_not_known_response,
1515
};
1616
use crate::apis::resources::auth_key::AuthKey;
1717
use crate::apis::resources::stats::Stats;
@@ -23,18 +23,18 @@ use crate::tracker::services::torrent::{get_torrent_info, get_torrents, Paginati
2323
use crate::tracker::Tracker;
2424

2525
pub async fn get_stats_handler(State(tracker): State<Arc<Tracker>>) -> Json<Stats> {
26-
response_stats(get_metrics(tracker.clone()).await)
26+
stats_response(get_metrics(tracker.clone()).await)
2727
}
2828

2929
#[derive(Deserialize)]
3030
pub struct InfoHashParam(String);
3131

3232
pub async fn get_torrent_handler(State(tracker): State<Arc<Tracker>>, Path(info_hash): Path<InfoHashParam>) -> Response {
3333
match InfoHash::from_str(&info_hash.0) {
34-
Err(_) => response_invalid_info_hash_param(&info_hash.0),
34+
Err(_) => invalid_info_hash_param_response(&info_hash.0),
3535
Ok(info_hash) => match get_torrent_info(tracker.clone(), &info_hash).await {
36-
Some(info) => response_torrent_info(info),
37-
None => response_torrent_not_known(),
36+
Some(info) => torrent_info_response(info),
37+
None => torrent_not_known_response(),
3838
},
3939
}
4040
}
@@ -50,7 +50,7 @@ pub async fn get_torrents_handler(
5050
State(tracker): State<Arc<Tracker>>,
5151
pagination: Query<PaginationParams>,
5252
) -> Json<Vec<ListItem>> {
53-
response_torrent_list(
53+
torrent_list_response(
5454
&get_torrents(
5555
tracker.clone(),
5656
&Pagination::new_with_options(pagination.0.offset, pagination.0.limit),
@@ -64,10 +64,10 @@ pub async fn add_torrent_to_whitelist_handler(
6464
Path(info_hash): Path<InfoHashParam>,
6565
) -> Response {
6666
match InfoHash::from_str(&info_hash.0) {
67-
Err(_) => response_invalid_info_hash_param(&info_hash.0),
67+
Err(_) => invalid_info_hash_param_response(&info_hash.0),
6868
Ok(info_hash) => match tracker.add_torrent_to_whitelist(&info_hash).await {
69-
Ok(..) => response_ok(),
70-
Err(..) => response_failed_to_whitelist_torrent(),
69+
Ok(..) => ok_response(),
70+
Err(..) => failed_to_whitelist_torrent_response(),
7171
},
7272
}
7373
}
@@ -77,26 +77,26 @@ pub async fn remove_torrent_from_whitelist_handler(
7777
Path(info_hash): Path<InfoHashParam>,
7878
) -> Response {
7979
match InfoHash::from_str(&info_hash.0) {
80-
Err(_) => response_invalid_info_hash_param(&info_hash.0),
80+
Err(_) => invalid_info_hash_param_response(&info_hash.0),
8181
Ok(info_hash) => match tracker.remove_torrent_from_whitelist(&info_hash).await {
82-
Ok(..) => response_ok(),
83-
Err(..) => response_failed_to_remove_torrent_from_whitelist(),
82+
Ok(..) => ok_response(),
83+
Err(..) => failed_to_remove_torrent_from_whitelist_response(),
8484
},
8585
}
8686
}
8787

8888
pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response {
8989
match tracker.load_whitelist().await {
90-
Ok(..) => response_ok(),
91-
Err(..) => response_failed_to_reload_whitelist(),
90+
Ok(..) => ok_response(),
91+
Err(..) => failed_to_reload_whitelist_response(),
9292
}
9393
}
9494

9595
pub async fn generate_auth_key_handler(State(tracker): State<Arc<Tracker>>, Path(seconds_valid_or_key): Path<u64>) -> Response {
9696
let seconds_valid = seconds_valid_or_key;
9797
match tracker.generate_auth_key(Duration::from_secs(seconds_valid)).await {
98-
Ok(auth_key) => response_auth_key(&AuthKey::from(auth_key)),
99-
Err(_) => response_failed_to_generate_key(),
98+
Ok(auth_key) => auth_key_response(&AuthKey::from(auth_key)),
99+
Err(_) => failed_to_generate_key_response(),
100100
}
101101
}
102102

@@ -108,18 +108,18 @@ pub async fn delete_auth_key_handler(
108108
Path(seconds_valid_or_key): Path<KeyIdParam>,
109109
) -> Response {
110110
match KeyId::from_str(&seconds_valid_or_key.0) {
111-
Err(_) => response_invalid_auth_key_param(&seconds_valid_or_key.0),
111+
Err(_) => invalid_auth_key_param_response(&seconds_valid_or_key.0),
112112
Ok(key_id) => match tracker.remove_auth_key(&key_id.to_string()).await {
113-
Ok(_) => response_ok(),
114-
Err(_) => response_failed_to_delete_key(),
113+
Ok(_) => ok_response(),
114+
Err(_) => failed_to_delete_key_response(),
115115
},
116116
}
117117
}
118118

119119
pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
120120
match tracker.load_keys().await {
121-
Ok(..) => response_ok(),
122-
Err(..) => response_failed_to_reload_keys(),
121+
Ok(..) => ok_response(),
122+
Err(..) => failed_to_reload_keys_response(),
123123
}
124124
}
125125

src/apis/responses.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ pub enum ActionStatus<'a> {
3939
// Resource responses
4040

4141
#[must_use]
42-
pub fn response_stats(tracker_metrics: TrackerMetrics) -> Json<Stats> {
42+
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> {
4343
Json(Stats::from(tracker_metrics))
4444
}
4545

4646
#[must_use]
47-
pub fn response_torrent_list(basic_infos: &[BasicInfo]) -> Json<Vec<ListItem>> {
47+
pub fn torrent_list_response(basic_infos: &[BasicInfo]) -> Json<Vec<ListItem>> {
4848
Json(ListItem::new_vec(basic_infos))
4949
}
5050

5151
#[must_use]
52-
pub fn response_torrent_info(info: Info) -> Response {
52+
pub fn torrent_info_response(info: Info) -> Response {
5353
Json(Torrent::from(info)).into_response()
5454
}
5555

5656
/// # Panics
5757
///
5858
/// Will panic if it can't convert the `AuthKey` resource to json
5959
#[must_use]
60-
pub fn response_auth_key(auth_key: &AuthKey) -> Response {
60+
pub fn auth_key_response(auth_key: &AuthKey) -> Response {
6161
(
6262
StatusCode::OK,
6363
[(header::CONTENT_TYPE, "application/json; charset=utf-8")],
@@ -72,7 +72,7 @@ pub fn response_auth_key(auth_key: &AuthKey) -> Response {
7272
///
7373
/// Will panic if it can't convert the `ActionStatus` to json
7474
#[must_use]
75-
pub fn response_ok() -> Response {
75+
pub fn ok_response() -> Response {
7676
(
7777
StatusCode::OK,
7878
[(header::CONTENT_TYPE, "application/json")],
@@ -84,19 +84,19 @@ pub fn response_ok() -> Response {
8484
// Error responses
8585

8686
#[must_use]
87-
pub fn response_invalid_info_hash_param(info_hash: &str) -> Response {
88-
response_bad_request(&format!(
87+
pub fn invalid_info_hash_param_response(info_hash: &str) -> Response {
88+
bad_request_response(&format!(
8989
"Invalid URL: invalid infohash param: string \"{}\", expected a 40 character long string",
9090
info_hash
9191
))
9292
}
9393

9494
#[must_use]
95-
pub fn response_invalid_auth_key_param(invalid_key: &str) -> Response {
96-
response_bad_request(&format!("Invalid auth key id param \"{invalid_key}\""))
95+
pub fn invalid_auth_key_param_response(invalid_key: &str) -> Response {
96+
bad_request_response(&format!("Invalid auth key id param \"{invalid_key}\""))
9797
}
9898

99-
fn response_bad_request(body: &str) -> Response {
99+
fn bad_request_response(body: &str) -> Response {
100100
(
101101
StatusCode::BAD_REQUEST,
102102
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
@@ -106,41 +106,41 @@ fn response_bad_request(body: &str) -> Response {
106106
}
107107

108108
#[must_use]
109-
pub fn response_torrent_not_known() -> Response {
109+
pub fn torrent_not_known_response() -> Response {
110110
Json(json!("torrent not known")).into_response()
111111
}
112112

113113
#[must_use]
114-
pub fn response_failed_to_remove_torrent_from_whitelist() -> Response {
115-
response_unhandled_rejection("failed to remove torrent from whitelist".to_string())
114+
pub fn failed_to_remove_torrent_from_whitelist_response() -> Response {
115+
unhandled_rejection_response("failed to remove torrent from whitelist".to_string())
116116
}
117117

118118
#[must_use]
119-
pub fn response_failed_to_whitelist_torrent() -> Response {
120-
response_unhandled_rejection("failed to whitelist torrent".to_string())
119+
pub fn failed_to_whitelist_torrent_response() -> Response {
120+
unhandled_rejection_response("failed to whitelist torrent".to_string())
121121
}
122122

123123
#[must_use]
124-
pub fn response_failed_to_reload_whitelist() -> Response {
125-
response_unhandled_rejection("failed to reload whitelist".to_string())
124+
pub fn failed_to_reload_whitelist_response() -> Response {
125+
unhandled_rejection_response("failed to reload whitelist".to_string())
126126
}
127127

128128
#[must_use]
129-
pub fn response_failed_to_generate_key() -> Response {
130-
response_unhandled_rejection("failed to generate key".to_string())
129+
pub fn failed_to_generate_key_response() -> Response {
130+
unhandled_rejection_response("failed to generate key".to_string())
131131
}
132132

133133
#[must_use]
134-
pub fn response_failed_to_delete_key() -> Response {
135-
response_unhandled_rejection("failed to delete key".to_string())
134+
pub fn failed_to_delete_key_response() -> Response {
135+
unhandled_rejection_response("failed to delete key".to_string())
136136
}
137137

138138
#[must_use]
139-
pub fn response_failed_to_reload_keys() -> Response {
140-
response_unhandled_rejection("failed to reload keys".to_string())
139+
pub fn failed_to_reload_keys_response() -> Response {
140+
unhandled_rejection_response("failed to reload keys".to_string())
141141
}
142142

143-
fn response_unhandled_rejection(reason: String) -> Response {
143+
fn unhandled_rejection_response(reason: String) -> Response {
144144
(
145145
StatusCode::INTERNAL_SERVER_ERROR,
146146
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],

0 commit comments

Comments
 (0)