Skip to content

Commit

Permalink
refactor(api): [torrust#182] Axum API, torrent context, search for to…
Browse files Browse the repository at this point in the history
…rrents
  • Loading branch information
josecelano committed Jun 16, 2023
1 parent ed533b7 commit b998a16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::io::{Cursor, Write};
use std::str::FromStr;
use std::sync::Arc;

use axum::extract::{Multipart, Path, State};
use axum::extract::{Multipart, Path, Query, State};
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde::Deserialize;

use super::responses::{new_torrent_response, torrent_file_response};
Expand All @@ -16,8 +17,10 @@ use crate::models::torrent::TorrentRequest;
use crate::models::torrent_tag::TagId;
use crate::models::user::UserId;
use crate::routes::torrent::Create;
use crate::services::torrent::ListingRequest;
use crate::utils::parse_torrent;
use crate::web::api::v1::extractors::bearer_token::{BearerToken, Extract};
use crate::web::api::v1::responses::OkResponseData;

/// Upload a new torrent file to the Index
///
Expand Down Expand Up @@ -77,6 +80,14 @@ pub async fn download_torrent_handler(
torrent_file_response(bytes)
}

#[allow(clippy::unused_async)]
pub async fn get_torrents_handler(State(app_data): State<Arc<AppData>>, Query(criteria): Query<ListingRequest>) -> Response {
match app_data.torrent_service.generate_torrent_info_listing(&criteria).await {
Ok(torrents_response) => Json(OkResponseData { data: torrents_response }).into_response(),
Err(err) => err.into_response(),
}
}

/// If the user is logged in, returns the user's ID. Otherwise, returns `None`.
///
/// # Errors
Expand Down
9 changes: 7 additions & 2 deletions src/web/api/v1/contexts/torrent/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ use std::sync::Arc;
use axum::routing::{get, post};
use axum::Router;

use super::handlers::{download_torrent_handler, upload_torrent_handler};
use super::handlers::{download_torrent_handler, get_torrents_handler, upload_torrent_handler};
use crate::common::AppData;

/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context.
/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for single resources.
pub fn router_for_single_resources(app_data: Arc<AppData>) -> Router {
Router::new()
.route("/upload", post(upload_torrent_handler).with_state(app_data.clone()))
.route("/download/:info_hash", get(download_torrent_handler).with_state(app_data))
}

/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for multiple resources.
pub fn router_for_multiple_resources(app_data: Arc<AppData>) -> Router {
Router::new().route("/", get(get_torrents_handler).with_state(app_data))
}
8 changes: 5 additions & 3 deletions src/web/api/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ pub fn router(app_data: Arc<AppData>) -> Router {
.nest("/tag", tag::routes::router_for_single_resources(app_data.clone()))
.nest("/tags", tag::routes::router_for_multiple_resources(app_data.clone()))
.nest("/settings", settings::routes::router(app_data.clone()))
.nest("/torrent", torrent::routes::router_for_single_resources(app_data.clone()));
.nest("/torrent", torrent::routes::router_for_single_resources(app_data.clone()))
.nest("/torrents", torrent::routes::router_for_multiple_resources(app_data.clone()));

Router::new()
.route("/", get(about_page_handler).with_state(app_data))
.nest("/v1", v1_api_routes)

// For development purposes only.
//
//.layer(CorsLayer::permissive()) // Uncomment this line and the `use` import.
//
// It allows calling the API on a different port. For example
// API: http://localhost:3000/v1
// Webapp: http://localhost:8080
//Router::new().nest("/v1", api_routes).layer(CorsLayer::permissive())
}
7 changes: 4 additions & 3 deletions tests/e2e/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,18 +635,17 @@ mod with_axum_implementation {
//use crate::common::contexts::category::fixtures::software_predefined_category_id;
//use crate::common::contexts::torrent::asserts::assert_expected_torrent_details;
use crate::common::contexts::torrent::requests::InfoHash;
use crate::common::contexts::torrent::responses::TorrentListResponse;
//use crate::common::contexts::torrent::responses::{
// Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse,
//};
//use crate::common::http::{Query, QueryParam};
use crate::common::http::{Query, QueryParam};
use crate::e2e::config::ENV_VAR_E2E_EXCLUDE_AXUM_IMPL;
use crate::e2e::contexts::torrent::asserts::expected_torrent;
use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index;
use crate::e2e::contexts::user::steps::new_logged_in_user;
use crate::e2e::environment::TestEnv;

/*
#[tokio::test]
async fn it_should_allow_guests_to_get_torrents() {
let mut env = TestEnv::new();
Expand Down Expand Up @@ -786,6 +785,8 @@ mod with_axum_implementation {
assert!(response.is_json_and_ok());
}

/*
#[tokio::test]
async fn it_should_allow_guests_to_get_torrent_details_searching_by_info_hash() {
let mut env = TestEnv::new();
Expand Down

0 comments on commit b998a16

Please sign in to comment.