Skip to content

Commit 4bed98a

Browse files
committed
refactor(api): [#182] Axum API, torrent context, get torrent info
1 parent b998a16 commit 4bed98a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/web/api/v1/contexts/torrent/handlers.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ pub async fn get_torrents_handler(State(app_data): State<Arc<AppData>>, Query(cr
8888
}
8989
}
9090

91+
#[allow(clippy::unused_async)]
92+
pub async fn get_torrent_info_handler(
93+
State(app_data): State<Arc<AppData>>,
94+
Extract(maybe_bearer_token): Extract,
95+
Path(info_hash): Path<InfoHashParam>,
96+
) -> Response {
97+
let Ok(info_hash) = InfoHash::from_str(&info_hash.0) else { return ServiceError::BadRequest.into_response() };
98+
99+
let opt_user_id = match get_optional_logged_in_user(maybe_bearer_token, app_data.clone()).await {
100+
Ok(opt_user_id) => opt_user_id,
101+
Err(err) => return err.into_response(),
102+
};
103+
104+
match app_data.torrent_service.get_torrent_info(&info_hash, opt_user_id).await {
105+
Ok(torrent_response) => Json(OkResponseData { data: torrent_response }).into_response(),
106+
Err(err) => err.into_response(),
107+
}
108+
}
109+
91110
/// If the user is logged in, returns the user's ID. Otherwise, returns `None`.
92111
///
93112
/// # Errors

src/web/api/v1/contexts/torrent/routes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ use std::sync::Arc;
66
use axum::routing::{get, post};
77
use axum::Router;
88

9-
use super::handlers::{download_torrent_handler, get_torrents_handler, upload_torrent_handler};
9+
use super::handlers::{download_torrent_handler, get_torrent_info_handler, get_torrents_handler, upload_torrent_handler};
1010
use crate::common::AppData;
1111

1212
/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for single resources.
1313
pub fn router_for_single_resources(app_data: Arc<AppData>) -> Router {
14+
let torrent_info_routes = Router::new().route("/", get(get_torrent_info_handler).with_state(app_data.clone()));
15+
1416
Router::new()
1517
.route("/upload", post(upload_torrent_handler).with_state(app_data.clone()))
1618
.route("/download/:info_hash", get(download_torrent_handler).with_state(app_data))
19+
.nest("/:info_hash", torrent_info_routes)
1720
}
1821

1922
/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for multiple resources.

tests/e2e/contexts/torrent/contract.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,12 @@ mod with_axum_implementation {
632632
use torrust_index_backend::web::api;
633633

634634
use crate::common::client::Client;
635-
//use crate::common::contexts::category::fixtures::software_predefined_category_id;
636-
//use crate::common::contexts::torrent::asserts::assert_expected_torrent_details;
635+
use crate::common::contexts::category::fixtures::software_predefined_category_id;
636+
use crate::common::contexts::torrent::asserts::assert_expected_torrent_details;
637637
use crate::common::contexts::torrent::requests::InfoHash;
638-
use crate::common::contexts::torrent::responses::TorrentListResponse;
639-
//use crate::common::contexts::torrent::responses::{
640-
// Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse,
641-
//};
638+
use crate::common::contexts::torrent::responses::{
639+
Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse,
640+
};
642641
use crate::common::http::{Query, QueryParam};
643642
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL;
644643
use crate::e2e::contexts::torrent::asserts::expected_torrent;
@@ -785,8 +784,6 @@ mod with_axum_implementation {
785784
assert!(response.is_json_and_ok());
786785
}
787786

788-
/*
789-
790787
#[tokio::test]
791788
async fn it_should_allow_guests_to_get_torrent_details_searching_by_info_hash() {
792789
let mut env = TestEnv::new();
@@ -855,8 +852,6 @@ mod with_axum_implementation {
855852
assert!(response.is_json_and_ok());
856853
}
857854

858-
*/
859-
860855
#[tokio::test]
861856
async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash() {
862857
let mut env = TestEnv::new();

0 commit comments

Comments
 (0)