This commit is contained in:
Aleksandr Bochev 2022-10-02 11:11:53 +02:00 committed by Aleksandr
parent fc0f3bd023
commit cf97ab0eea
5 changed files with 26 additions and 17 deletions

View File

@ -39,7 +39,7 @@ jobs:
with: with:
command: build command: build
- name: Setup Test Enviroment - name: Setup Test Environment
run: docker-compose up -d run: docker-compose up -d
- name: Cargo Test - name: Cargo Test

View File

@ -17,7 +17,7 @@ reqwest = { version = "0.11.11", features = [
"rustls-tls", "rustls-tls",
], default-features = false } ], default-features = false }
serde = { version = "1.0.144", features = ["derive"] } serde = { version = "1.0.144", features = ["derive"] }
enum-iterator = "1.2.0" enum-iterator = "0.8.1"
dotenv = "0.15.0" dotenv = "0.15.0"
log = "0.4.17" log = "0.4.17"

View File

@ -12,7 +12,8 @@ pub mod types;
use types::{ use types::{
BasicAuth, BlocklistUpdate, FreeSpace, Id, Nothing, PortTest, Result, RpcRequest, RpcResponse, BasicAuth, BlocklistUpdate, FreeSpace, Id, Nothing, PortTest, Result, RpcRequest, RpcResponse,
RpcResponseArgument, SessionClose, SessionGet, SessionStats, Torrent, TorrentAction, RpcResponseArgument, SessionClose, SessionGet, SessionStats, Torrent, TorrentAction,
TorrentAddArgs, TorrentAddedOrDuplicate, TorrentGetField, TorrentRenamePath, Torrents, TorrentAddArgs, TorrentAddedOrDuplicate, TorrentGetField, TorrentRenamePath, TorrentSetArgs,
Torrents,
}; };
const MAX_RETRIES: usize = 5; const MAX_RETRIES: usize = 5;
@ -381,9 +382,9 @@ impl TransClient {
/// ///
/// let args = TorrentSetArgs { /// let args = TorrentSetArgs {
/// labels: Some(vec![String::from("blue")]), /// labels: Some(vec![String::from("blue")]),
/// ..Default::default(), /// ..Default::default()
/// }; /// };
/// assert!(client.torrent_set(args, vec![Id::Id(0)]).await?.is_ok()) /// assert!(client.torrent_set(args, Some(vec![Id::Id(0)])).await?.is_ok());
/// ///
/// Ok(()) /// Ok(())
/// } /// }
@ -636,12 +637,12 @@ impl TransClient {
let rsp: reqwest::Response = rq.send().await?; let rsp: reqwest::Response = rq.send().await?;
if matches!(rsp.status(), StatusCode::CONFLICT) { if matches!(rsp.status(), StatusCode::CONFLICT) {
let session_id = rsp let session_id = rsp
.headers() .headers()
.get("X-Transmission-Session-Id") .get("X-Transmission-Session-Id")
.ok_or(TransError::NoSessionIdReceived)? .ok_or(TransError::NoSessionIdReceived)?
.to_str()?; .to_str()?;
self.session_id = Some(String::from(session_id)); self.session_id = Some(String::from(session_id));
info!("Got new session_id: {}. Retrying request.", session_id); info!("Got new session_id: {}. Retrying request.", session_id);
} else { } else {

View File

@ -12,6 +12,7 @@ pub struct BasicAuth {
pub(crate) use self::request::RpcRequest; pub(crate) use self::request::RpcRequest;
pub use self::request::{ pub use self::request::{
ArgumentFields, Id, TorrentAction, TorrentAddArgs, TorrentGetField, TorrentRenamePathArgs, ArgumentFields, Id, TorrentAction, TorrentAddArgs, TorrentGetField, TorrentRenamePathArgs,
TorrentSetArgs,
}; };
pub(crate) use self::response::RpcResponseArgument; pub(crate) use self::response::RpcResponseArgument;

View File

@ -1,4 +1,4 @@
use enum_iterator::{all, Sequence}; use enum_iterator::IntoEnumIterator;
use serde::Serialize; use serde::Serialize;
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
@ -53,7 +53,7 @@ impl RpcRequest {
pub fn torrent_get(fields: Option<Vec<TorrentGetField>>, ids: Option<Vec<Id>>) -> RpcRequest { pub fn torrent_get(fields: Option<Vec<TorrentGetField>>, ids: Option<Vec<Id>>) -> RpcRequest {
let string_fields = fields let string_fields = fields
.unwrap_or(all::<TorrentGetField>().collect()) .unwrap_or(TorrentGetField::all())
.iter() .iter()
.map(TorrentGetField::to_str) .map(TorrentGetField::to_str)
.collect(); .collect();
@ -70,7 +70,7 @@ impl RpcRequest {
args.ids = ids; args.ids = ids;
RpcRequest { RpcRequest {
method: String::from("torrent-set"), method: String::from("torrent-set"),
arguments: Some(Args::TorrentSetArgs(args)), arguments: Some(Args::TorrentSet(args)),
} }
} }
@ -135,6 +135,7 @@ pub enum Args {
TorrentAction(TorrentActionArgs), TorrentAction(TorrentActionArgs),
TorrentRemove(TorrentRemoveArgs), TorrentRemove(TorrentRemoveArgs),
TorrentAdd(TorrentAddArgs), TorrentAdd(TorrentAddArgs),
TorrentSet(TorrentSetArgs),
TorrentSetLocation(TorrentSetLocationArgs), TorrentSetLocation(TorrentSetLocationArgs),
TorrentRenamePath(TorrentRenamePathArgs), TorrentRenamePath(TorrentRenamePathArgs),
} }
@ -154,7 +155,9 @@ pub struct TorrentGetArgs {
impl Default for TorrentGetArgs { impl Default for TorrentGetArgs {
fn default() -> Self { fn default() -> Self {
let all_fields = all::<TorrentGetField>().map(|it| it.to_str()).collect(); let all_fields = TorrentGetField::into_enum_iter()
.map(|it| it.to_str())
.collect();
TorrentGetArgs { TorrentGetArgs {
fields: Some(all_fields), fields: Some(all_fields),
ids: None, ids: None,
@ -195,7 +198,7 @@ pub enum Id {
Hash(String), Hash(String),
} }
#[derive(Serialize, Debug, Default, Clone, Default)] #[derive(Serialize, Debug, Clone, Default)]
pub struct TorrentAddArgs { pub struct TorrentAddArgs {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub cookies: Option<String>, pub cookies: Option<String>,
@ -236,7 +239,7 @@ pub struct TorrentAddArgs {
pub labels: Option<Vec<String>>, pub labels: Option<Vec<String>>,
} }
#[derive(Clone, Sequence)] #[derive(Clone, Copy, IntoEnumIterator)]
pub enum TorrentGetField { pub enum TorrentGetField {
ActivityDate, ActivityDate,
AddedDate, AddedDate,
@ -280,6 +283,10 @@ pub enum TorrentGetField {
} }
impl TorrentGetField { impl TorrentGetField {
pub fn all() -> Vec<TorrentGetField> {
TorrentGetField::into_enum_iter().collect()
}
#[must_use] #[must_use]
pub fn to_str(&self) -> String { pub fn to_str(&self) -> String {
match self { match self {