11use reqwest:: Response ;
2+ use serde:: Serialize ;
23
34use crate :: common:: http:: { Query , QueryParam , ReqwestQuery } ;
45use crate :: servers:: api:: connection_info:: ConnectionInfo ;
@@ -18,7 +19,11 @@ impl Client {
1819 }
1920
2021 pub async fn generate_auth_key ( & self , seconds_valid : i32 ) -> Response {
21- self . post ( & format ! ( "key/{}" , & seconds_valid) ) . await
22+ self . post_empty ( & format ! ( "key/{}" , & seconds_valid) ) . await
23+ }
24+
25+ pub async fn add_auth_key ( & self , add_key_form : AddKeyForm ) -> Response {
26+ self . post_form ( "keys" , & add_key_form) . await
2227 }
2328
2429 pub async fn delete_auth_key ( & self , key : & str ) -> Response {
@@ -30,7 +35,7 @@ impl Client {
3035 }
3136
3237 pub async fn whitelist_a_torrent ( & self , info_hash : & str ) -> Response {
33- self . post ( & format ! ( "whitelist/{}" , & info_hash) ) . await
38+ self . post_empty ( & format ! ( "whitelist/{}" , & info_hash) ) . await
3439 }
3540
3641 pub async fn remove_torrent_from_whitelist ( & self , info_hash : & str ) -> Response {
@@ -63,10 +68,20 @@ impl Client {
6368 self . get_request_with_query ( path, query) . await
6469 }
6570
66- pub async fn post ( & self , path : & str ) -> Response {
71+ pub async fn post_empty ( & self , path : & str ) -> Response {
72+ reqwest:: Client :: new ( )
73+ . post ( self . base_url ( path) . clone ( ) )
74+ . query ( & ReqwestQuery :: from ( self . query_with_token ( ) ) )
75+ . send ( )
76+ . await
77+ . unwrap ( )
78+ }
79+
80+ pub async fn post_form < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> Response {
6781 reqwest:: Client :: new ( )
6882 . post ( self . base_url ( path) . clone ( ) )
6983 . query ( & ReqwestQuery :: from ( self . query_with_token ( ) ) )
84+ . json ( & form)
7085 . send ( )
7186 . await
7287 . unwrap ( )
@@ -114,3 +129,10 @@ pub async fn get(path: &str, query: Option<Query>) -> Response {
114129 None => reqwest:: Client :: builder ( ) . build ( ) . unwrap ( ) . get ( path) . send ( ) . await . unwrap ( ) ,
115130 }
116131}
132+
133+ #[ derive( Serialize , Debug ) ]
134+ pub struct AddKeyForm {
135+ #[ serde( rename = "key" ) ]
136+ pub opt_key : Option < String > ,
137+ pub seconds_valid : u64 ,
138+ }
0 commit comments