Store connection info as &str, implement Default for TorrentState

This commit is contained in:
seanomik 2022-06-20 15:44:00 -04:00
parent 4c907b940e
commit d0e087af2b
2 changed files with 17 additions and 11 deletions

View File

@ -1,20 +1,20 @@
use crate::{error::ClientError, TorrentInfo, TorrentTracker, TorrentUpload}; use crate::{error::ClientError, TorrentInfo, TorrentTracker, TorrentUpload};
pub struct ConnectionInfo { pub struct ConnectionInfo<'a> {
pub url: String, pub url: &'a str,
pub username: String, pub username: &'a str,
pub password: String, pub password: &'a str,
} }
pub type ClientResult<T> = Result<T, ClientError>; pub type ClientResult<T> = Result<T, ClientError>;
pub struct QBittorrentClient { pub struct QBittorrentClient<'a> {
client: reqwest::Client, client: reqwest::Client,
connection_info: Option<ConnectionInfo>, connection_info: Option<ConnectionInfo<'a>>,
auth_string: Option<String>, auth_string: Option<String>,
} }
impl QBittorrentClient { impl<'a> QBittorrentClient<'a> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
client: reqwest::Client::new(), client: reqwest::Client::new(),
@ -24,7 +24,7 @@ impl QBittorrentClient {
} }
/// Login to qBittorrent. This must be ran so that the client can make requests. /// Login to qBittorrent. This must be ran so that the client can make requests.
pub async fn login(&mut self, url: String, username: String, password: String) -> ClientResult<()> { pub async fn login(&mut self, url: &'a str, username: &'a str, password: &'a str) -> ClientResult<()> {
// Send response to get auth string // Send response to get auth string
let resp = self.client.post(format!("{}/api/v2/auth/login", url.clone())) let resp = self.client.post(format!("{}/api/v2/auth/login", url.clone()))
.form(&[ .form(&[
@ -185,7 +185,7 @@ impl QBittorrentClient {
} }
/// Remove multiple torrents at once. `delete_files` applies to *all* torrents. /// Remove multiple torrents at once. `delete_files` applies to *all* torrents.
pub async fn remove_torrents(&self, torrents: Vec<&TorrentInfo>, delete_files: bool ) -> ClientResult<()> { pub async fn remove_torrents(&self, torrents: Vec<TorrentInfo>, delete_files: bool ) -> ClientResult<()> {
if let (Some(auth_string), Some(conn)) = (self.auth_string.as_ref(), self.connection_info.as_ref()) { if let (Some(auth_string), Some(conn)) = (self.auth_string.as_ref(), self.connection_info.as_ref()) {
// Convert the hashes into a string concatenated with `|` // Convert the hashes into a string concatenated with `|`
let hashes = torrents.iter() let hashes = torrents.iter()

View File

@ -3,7 +3,7 @@ use serde_repr::*;
use serde_with::{CommaSeparator}; use serde_with::{CommaSeparator};
/// A torrent's information from the qbittorrent client. /// A torrent's information from the qbittorrent client.
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct TorrentInfo { pub struct TorrentInfo {
/// Time (Unix Epoch) when the torrent was added to the client /// Time (Unix Epoch) when the torrent was added to the client
pub added_on: u64, pub added_on: u64,
@ -224,6 +224,12 @@ pub enum TorrentState {
Unknown, Unknown,
} }
impl Default for TorrentState {
fn default() -> Self {
TorrentState::Unknown
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct TorrentTracker { pub struct TorrentTracker {
/// Tracker URL /// Tracker URL
@ -254,7 +260,7 @@ pub struct TorrentTracker {
pub message: String, pub message: String,
} }
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Clone)]
#[repr(u8)] #[repr(u8)]
pub enum TrackerStatus { pub enum TrackerStatus {
/// Tracker is disabled (used for DHT, PeX, and LSD) /// Tracker is disabled (used for DHT, PeX, and LSD)