Skip to content

Commit

Permalink
refactor(api): [torrust#143] rename response functions
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 13, 2023
1 parent 6955666 commit 02dfe3e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
48 changes: 24 additions & 24 deletions src/apis/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use axum::response::{Json, Response};
use serde::{de, Deserialize, Deserializer};

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

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

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

pub async fn get_torrent_handler(State(tracker): State<Arc<Tracker>>, Path(info_hash): Path<InfoHashParam>) -> Response {
match InfoHash::from_str(&info_hash.0) {
Err(_) => response_invalid_info_hash_param(&info_hash.0),
Err(_) => invalid_info_hash_param_response(&info_hash.0),
Ok(info_hash) => match get_torrent_info(tracker.clone(), &info_hash).await {
Some(info) => response_torrent_info(info),
None => response_torrent_not_known(),
Some(info) => torrent_info_response(info),
None => torrent_not_known_response(),
},
}
}
Expand All @@ -50,7 +50,7 @@ pub async fn get_torrents_handler(
State(tracker): State<Arc<Tracker>>,
pagination: Query<PaginationParams>,
) -> Json<Vec<ListItem>> {
response_torrent_list(
torrent_list_response(
&get_torrents(
tracker.clone(),
&Pagination::new_with_options(pagination.0.offset, pagination.0.limit),
Expand All @@ -64,10 +64,10 @@ pub async fn add_torrent_to_whitelist_handler(
Path(info_hash): Path<InfoHashParam>,
) -> Response {
match InfoHash::from_str(&info_hash.0) {
Err(_) => response_invalid_info_hash_param(&info_hash.0),
Err(_) => invalid_info_hash_param_response(&info_hash.0),
Ok(info_hash) => match tracker.add_torrent_to_whitelist(&info_hash).await {
Ok(..) => response_ok(),
Err(..) => response_failed_to_whitelist_torrent(),
Ok(..) => ok_response(),
Err(..) => failed_to_whitelist_torrent_response(),
},
}
}
Expand All @@ -77,26 +77,26 @@ pub async fn remove_torrent_from_whitelist_handler(
Path(info_hash): Path<InfoHashParam>,
) -> Response {
match InfoHash::from_str(&info_hash.0) {
Err(_) => response_invalid_info_hash_param(&info_hash.0),
Err(_) => invalid_info_hash_param_response(&info_hash.0),
Ok(info_hash) => match tracker.remove_torrent_from_whitelist(&info_hash).await {
Ok(..) => response_ok(),
Err(..) => response_failed_to_remove_torrent_from_whitelist(),
Ok(..) => ok_response(),
Err(..) => failed_to_remove_torrent_from_whitelist_response(),
},
}
}

pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_whitelist().await {
Ok(..) => response_ok(),
Err(..) => response_failed_to_reload_whitelist(),
Ok(..) => ok_response(),
Err(..) => failed_to_reload_whitelist_response(),
}
}

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

Expand All @@ -108,18 +108,18 @@ pub async fn delete_auth_key_handler(
Path(seconds_valid_or_key): Path<KeyIdParam>,
) -> Response {
match KeyId::from_str(&seconds_valid_or_key.0) {
Err(_) => response_invalid_auth_key_param(&seconds_valid_or_key.0),
Err(_) => invalid_auth_key_param_response(&seconds_valid_or_key.0),
Ok(key_id) => match tracker.remove_auth_key(&key_id.to_string()).await {
Ok(_) => response_ok(),
Err(_) => response_failed_to_delete_key(),
Ok(_) => ok_response(),
Err(_) => failed_to_delete_key_response(),
},
}
}

pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_keys().await {
Ok(..) => response_ok(),
Err(..) => response_failed_to_reload_keys(),
Ok(..) => ok_response(),
Err(..) => failed_to_reload_keys_response(),
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/apis/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ pub enum ActionStatus<'a> {
// Resource responses

#[must_use]
pub fn response_stats(tracker_metrics: TrackerMetrics) -> Json<Stats> {
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> {
Json(Stats::from(tracker_metrics))
}

#[must_use]
pub fn response_torrent_list(basic_infos: &[BasicInfo]) -> Json<Vec<ListItem>> {
pub fn torrent_list_response(basic_infos: &[BasicInfo]) -> Json<Vec<ListItem>> {
Json(ListItem::new_vec(basic_infos))
}

#[must_use]
pub fn response_torrent_info(info: Info) -> Response {
pub fn torrent_info_response(info: Info) -> Response {
Json(Torrent::from(info)).into_response()
}

/// # Panics
///
/// Will panic if it can't convert the `AuthKey` resource to json
#[must_use]
pub fn response_auth_key(auth_key: &AuthKey) -> Response {
pub fn auth_key_response(auth_key: &AuthKey) -> Response {
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json; charset=utf-8")],
Expand All @@ -72,7 +72,7 @@ pub fn response_auth_key(auth_key: &AuthKey) -> Response {
///
/// Will panic if it can't convert the `ActionStatus` to json
#[must_use]
pub fn response_ok() -> Response {
pub fn ok_response() -> Response {
(
StatusCode::OK,
[(header::CONTENT_TYPE, "application/json")],
Expand All @@ -84,19 +84,19 @@ pub fn response_ok() -> Response {
// Error responses

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 02dfe3e

Please sign in to comment.