1
1
use reqwest:: Response ;
2
+ use serde:: Serialize ;
2
3
3
4
use crate :: common:: http:: { Query , QueryParam , ReqwestQuery } ;
4
5
use crate :: servers:: api:: connection_info:: ConnectionInfo ;
@@ -18,7 +19,11 @@ impl Client {
18
19
}
19
20
20
21
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
22
27
}
23
28
24
29
pub async fn delete_auth_key ( & self , key : & str ) -> Response {
@@ -30,7 +35,7 @@ impl Client {
30
35
}
31
36
32
37
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
34
39
}
35
40
36
41
pub async fn remove_torrent_from_whitelist ( & self , info_hash : & str ) -> Response {
@@ -63,10 +68,20 @@ impl Client {
63
68
self . get_request_with_query ( path, query) . await
64
69
}
65
70
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 {
67
81
reqwest:: Client :: new ( )
68
82
. post ( self . base_url ( path) . clone ( ) )
69
83
. query ( & ReqwestQuery :: from ( self . query_with_token ( ) ) )
84
+ . json ( & form)
70
85
. send ( )
71
86
. await
72
87
. unwrap ( )
@@ -114,3 +129,10 @@ pub async fn get(path: &str, query: Option<Query>) -> Response {
114
129
None => reqwest:: Client :: builder ( ) . build ( ) . unwrap ( ) . get ( path) . send ( ) . await . unwrap ( ) ,
115
130
}
116
131
}
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