1+ use reqwest:: multipart;
12use serde:: Serialize ;
23
34use super :: contexts:: category:: { AddCategoryForm , DeleteCategoryForm } ;
45use super :: contexts:: settings:: UpdateSettingsForm ;
6+ use super :: contexts:: torrent:: requests:: { TorrentId , UpdateTorrentFrom } ;
57use super :: contexts:: user:: { LoginForm , RegistrationForm , TokenRenewalForm , TokenVerificationForm , Username } ;
8+ use super :: responses:: { self , BinaryResponse } ;
69use crate :: e2e:: connection_info:: ConnectionInfo ;
710use crate :: e2e:: http:: { Query , ReqwestQuery } ;
8- use crate :: e2e:: response :: Response ;
11+ use crate :: e2e:: responses :: TextResponse ;
912
1013/// API Client
1114pub struct Client {
@@ -21,71 +24,99 @@ impl Client {
2124
2225 // Context: about
2326
24- pub async fn about ( & self ) -> Response {
27+ pub async fn about ( & self ) -> TextResponse {
2528 self . http_client . get ( "about" , Query :: empty ( ) ) . await
2629 }
2730
28- pub async fn license ( & self ) -> Response {
31+ pub async fn license ( & self ) -> TextResponse {
2932 self . http_client . get ( "about/license" , Query :: empty ( ) ) . await
3033 }
3134
3235 // Context: category
3336
34- pub async fn get_categories ( & self ) -> Response {
37+ pub async fn get_categories ( & self ) -> TextResponse {
3538 self . http_client . get ( "category" , Query :: empty ( ) ) . await
3639 }
3740
38- pub async fn add_category ( & self , add_category_form : AddCategoryForm ) -> Response {
41+ pub async fn add_category ( & self , add_category_form : AddCategoryForm ) -> TextResponse {
3942 self . http_client . post ( "category" , & add_category_form) . await
4043 }
4144
42- pub async fn delete_category ( & self , delete_category_form : DeleteCategoryForm ) -> Response {
45+ pub async fn delete_category ( & self , delete_category_form : DeleteCategoryForm ) -> TextResponse {
4346 self . http_client . delete_with_body ( "category" , & delete_category_form) . await
4447 }
4548
4649 // Context: root
4750
48- pub async fn root ( & self ) -> Response {
51+ pub async fn root ( & self ) -> TextResponse {
4952 self . http_client . get ( "" , Query :: empty ( ) ) . await
5053 }
5154
5255 // Context: settings
5356
54- pub async fn get_public_settings ( & self ) -> Response {
57+ pub async fn get_public_settings ( & self ) -> TextResponse {
5558 self . http_client . get ( "settings/public" , Query :: empty ( ) ) . await
5659 }
5760
58- pub async fn get_site_name ( & self ) -> Response {
61+ pub async fn get_site_name ( & self ) -> TextResponse {
5962 self . http_client . get ( "settings/name" , Query :: empty ( ) ) . await
6063 }
6164
62- pub async fn get_settings ( & self ) -> Response {
65+ pub async fn get_settings ( & self ) -> TextResponse {
6366 self . http_client . get ( "settings" , Query :: empty ( ) ) . await
6467 }
6568
66- pub async fn update_settings ( & self , update_settings_form : UpdateSettingsForm ) -> Response {
69+ pub async fn update_settings ( & self , update_settings_form : UpdateSettingsForm ) -> TextResponse {
6770 self . http_client . post ( "settings" , & update_settings_form) . await
6871 }
6972
73+ // Context: torrent
74+
75+ pub async fn get_torrents ( & self ) -> TextResponse {
76+ self . http_client . get ( "torrents" , Query :: empty ( ) ) . await
77+ }
78+
79+ pub async fn get_torrent ( & self , id : TorrentId ) -> TextResponse {
80+ self . http_client . get ( & format ! ( "torrent/{id}" ) , Query :: empty ( ) ) . await
81+ }
82+
83+ pub async fn delete_torrent ( & self , id : TorrentId ) -> TextResponse {
84+ self . http_client . delete ( & format ! ( "torrent/{id}" ) ) . await
85+ }
86+
87+ pub async fn update_torrent ( & self , id : TorrentId , update_torrent_form : UpdateTorrentFrom ) -> TextResponse {
88+ self . http_client . put ( & format ! ( "torrent/{id}" ) , & update_torrent_form) . await
89+ }
90+
91+ pub async fn upload_torrent ( & self , form : multipart:: Form ) -> TextResponse {
92+ self . http_client . post_multipart ( "torrent/upload" , form) . await
93+ }
94+
95+ pub async fn download_torrent ( & self , id : TorrentId ) -> responses:: BinaryResponse {
96+ self . http_client
97+ . get_binary ( & format ! ( "torrent/download/{id}" ) , Query :: empty ( ) )
98+ . await
99+ }
100+
70101 // Context: user
71102
72- pub async fn register_user ( & self , registration_form : RegistrationForm ) -> Response {
103+ pub async fn register_user ( & self , registration_form : RegistrationForm ) -> TextResponse {
73104 self . http_client . post ( "user/register" , & registration_form) . await
74105 }
75106
76- pub async fn login_user ( & self , registration_form : LoginForm ) -> Response {
107+ pub async fn login_user ( & self , registration_form : LoginForm ) -> TextResponse {
77108 self . http_client . post ( "user/login" , & registration_form) . await
78109 }
79110
80- pub async fn verify_token ( & self , token_verification_form : TokenVerificationForm ) -> Response {
111+ pub async fn verify_token ( & self , token_verification_form : TokenVerificationForm ) -> TextResponse {
81112 self . http_client . post ( "user/token/verify" , & token_verification_form) . await
82113 }
83114
84- pub async fn renew_token ( & self , token_verification_form : TokenRenewalForm ) -> Response {
115+ pub async fn renew_token ( & self , token_verification_form : TokenRenewalForm ) -> TextResponse {
85116 self . http_client . post ( "user/token/renew" , & token_verification_form) . await
86117 }
87118
88- pub async fn ban_user ( & self , username : Username ) -> Response {
119+ pub async fn ban_user ( & self , username : Username ) -> TextResponse {
89120 self . http_client . delete ( & format ! ( "user/ban/{}" , & username. value) ) . await
90121 }
91122}
@@ -104,7 +135,30 @@ impl Http {
104135 }
105136 }
106137
107- pub async fn get ( & self , path : & str , params : Query ) -> Response {
138+ pub async fn get ( & self , path : & str , params : Query ) -> TextResponse {
139+ let response = match & self . connection_info . token {
140+ Some ( token) => reqwest:: Client :: builder ( )
141+ . build ( )
142+ . unwrap ( )
143+ . get ( self . base_url ( path) . clone ( ) )
144+ . query ( & ReqwestQuery :: from ( params) )
145+ . bearer_auth ( token)
146+ . send ( )
147+ . await
148+ . unwrap ( ) ,
149+ None => reqwest:: Client :: builder ( )
150+ . build ( )
151+ . unwrap ( )
152+ . get ( self . base_url ( path) . clone ( ) )
153+ . query ( & ReqwestQuery :: from ( params) )
154+ . send ( )
155+ . await
156+ . unwrap ( ) ,
157+ } ;
158+ TextResponse :: from ( response) . await
159+ }
160+
161+ pub async fn get_binary ( & self , path : & str , params : Query ) -> BinaryResponse {
108162 let response = match & self . connection_info . token {
109163 Some ( token) => reqwest:: Client :: builder ( )
110164 . build ( )
@@ -124,10 +178,10 @@ impl Http {
124178 . await
125179 . unwrap ( ) ,
126180 } ;
127- Response :: from ( response) . await
181+ BinaryResponse :: from ( response) . await
128182 }
129183
130- pub async fn post < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> Response {
184+ pub async fn post < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> TextResponse {
131185 let response = match & self . connection_info . token {
132186 Some ( token) => reqwest:: Client :: new ( )
133187 . post ( self . base_url ( path) . clone ( ) )
@@ -143,10 +197,52 @@ impl Http {
143197 . await
144198 . unwrap ( ) ,
145199 } ;
146- Response :: from ( response) . await
200+ TextResponse :: from ( response) . await
201+ }
202+
203+ pub async fn post_multipart ( & self , path : & str , form : multipart:: Form ) -> TextResponse {
204+ let response = match & self . connection_info . token {
205+ Some ( token) => reqwest:: Client :: builder ( )
206+ . build ( )
207+ . unwrap ( )
208+ . post ( self . base_url ( path) . clone ( ) )
209+ . multipart ( form)
210+ . bearer_auth ( token)
211+ . send ( )
212+ . await
213+ . unwrap ( ) ,
214+ None => reqwest:: Client :: builder ( )
215+ . build ( )
216+ . unwrap ( )
217+ . post ( self . base_url ( path) . clone ( ) )
218+ . multipart ( form)
219+ . send ( )
220+ . await
221+ . unwrap ( ) ,
222+ } ;
223+ TextResponse :: from ( response) . await
224+ }
225+
226+ pub async fn put < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> TextResponse {
227+ let response = match & self . connection_info . token {
228+ Some ( token) => reqwest:: Client :: new ( )
229+ . put ( self . base_url ( path) . clone ( ) )
230+ . bearer_auth ( token)
231+ . json ( & form)
232+ . send ( )
233+ . await
234+ . unwrap ( ) ,
235+ None => reqwest:: Client :: new ( )
236+ . put ( self . base_url ( path) . clone ( ) )
237+ . json ( & form)
238+ . send ( )
239+ . await
240+ . unwrap ( ) ,
241+ } ;
242+ TextResponse :: from ( response) . await
147243 }
148244
149- async fn delete ( & self , path : & str ) -> Response {
245+ async fn delete ( & self , path : & str ) -> TextResponse {
150246 let response = match & self . connection_info . token {
151247 Some ( token) => reqwest:: Client :: new ( )
152248 . delete ( self . base_url ( path) . clone ( ) )
@@ -160,10 +256,10 @@ impl Http {
160256 . await
161257 . unwrap ( ) ,
162258 } ;
163- Response :: from ( response) . await
259+ TextResponse :: from ( response) . await
164260 }
165261
166- async fn delete_with_body < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> Response {
262+ async fn delete_with_body < T : Serialize + ?Sized > ( & self , path : & str , form : & T ) -> TextResponse {
167263 let response = match & self . connection_info . token {
168264 Some ( token) => reqwest:: Client :: new ( )
169265 . delete ( self . base_url ( path) . clone ( ) )
@@ -179,7 +275,7 @@ impl Http {
179275 . await
180276 . unwrap ( ) ,
181277 } ;
182- Response :: from ( response) . await
278+ TextResponse :: from ( response) . await
183279 }
184280
185281 fn base_url ( & self , path : & str ) -> String {
0 commit comments