From b974ce0eba7614bbe1ce79b03a73ab20143b75f6 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 28 Nov 2022 16:05:41 +0000 Subject: [PATCH] refactor: [#61] use TorrentListItemResource in torrent list API endpoint --- src/api/resources/mod.rs | 4 ++-- src/api/resources/torrent_resource.rs | 10 ++++++++++ src/api/server.rs | 18 +++--------------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/api/resources/mod.rs b/src/api/resources/mod.rs index a229539d..e139207b 100644 --- a/src/api/resources/mod.rs +++ b/src/api/resources/mod.rs @@ -3,8 +3,8 @@ //! WIP. Not all endpoints have their resource structs. //! //! - [x] AuthKeys -//! - [ ] ... -//! - [ ] ... +//! - [ ] TorrentResource, TorrentListItemResource, TorrentPeerResource, PeerIdResource +//! - [ ] StatsResource //! - [ ] ... pub mod auth_key_resource; pub mod torrent_resource; diff --git a/src/api/resources/torrent_resource.rs b/src/api/resources/torrent_resource.rs index ecf2a3fd..88d0463c 100644 --- a/src/api/resources/torrent_resource.rs +++ b/src/api/resources/torrent_resource.rs @@ -13,6 +13,16 @@ pub struct TorrentResource { pub peers: Option>, } +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct TorrentListItemResource { + pub info_hash: String, + pub seeders: u32, + pub completed: u32, + pub leechers: u32, + // todo: this is always None. Remove field from endpoint? + pub peers: Option>, +} + #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct TorrentPeerResource { pub peer_id: PeerIdResource, diff --git a/src/api/server.rs b/src/api/server.rs index 85c177b8..ef514749 100644 --- a/src/api/server.rs +++ b/src/api/server.rs @@ -8,8 +8,7 @@ use serde::{Deserialize, Serialize}; use warp::{filters, reply, serve, Filter}; use super::resources::auth_key_resource::AuthKeyResource; -use super::resources::torrent_resource::{TorrentPeerResource, TorrentResource}; -use crate::peer::TorrentPeer; +use super::resources::torrent_resource::{TorrentListItemResource, TorrentPeerResource, TorrentResource}; use crate::protocol::common::*; use crate::tracker::TorrentTracker; @@ -19,16 +18,6 @@ struct TorrentInfoQuery { limit: Option, } -#[derive(Serialize)] -struct Torrent<'a> { - info_hash: &'a InfoHash, - seeders: u32, - completed: u32, - leechers: u32, - #[serde(skip_serializing_if = "Option::is_none")] - peers: Option>, -} - #[derive(Serialize)] struct Stats { torrents: u32, @@ -110,9 +99,8 @@ pub fn start(socket_addr: SocketAddr, tracker: Arc) -> impl warp .iter() .map(|(info_hash, torrent_entry)| { let (seeders, completed, leechers) = torrent_entry.get_stats(); - // todo: use TorrentResource - Torrent { - info_hash, + TorrentListItemResource { + info_hash: info_hash.to_string(), seeders, completed, leechers,