Skip to content

Commit

Permalink
refactor: [#61] use TorrentListItemResource in torrent list API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 28, 2022
1 parent 284c91b commit b974ce0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/api/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions src/api/resources/torrent_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ pub struct TorrentResource {
pub peers: Option<Vec<TorrentPeerResource>>,
}

#[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<Vec<TorrentPeerResource>>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct TorrentPeerResource {
pub peer_id: PeerIdResource,
Expand Down
18 changes: 3 additions & 15 deletions src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,16 +18,6 @@ struct TorrentInfoQuery {
limit: Option<u32>,
}

#[derive(Serialize)]
struct Torrent<'a> {
info_hash: &'a InfoHash,
seeders: u32,
completed: u32,
leechers: u32,
#[serde(skip_serializing_if = "Option::is_none")]
peers: Option<Vec<&'a TorrentPeer>>,
}

#[derive(Serialize)]
struct Stats {
torrents: u32,
Expand Down Expand Up @@ -110,9 +99,8 @@ pub fn start(socket_addr: SocketAddr, tracker: Arc<TorrentTracker>) -> 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,
Expand Down

0 comments on commit b974ce0

Please sign in to comment.