1
+ use std:: time:: Duration ;
2
+
1
3
use reqwest:: { Error , Response } ;
2
4
pub struct ConnectionInfo {
3
5
/// The URL of the tracker. Eg: <https://tracker:7070> or <udp://tracker:6969>
@@ -15,6 +17,7 @@ impl ConnectionInfo {
15
17
16
18
pub struct Client {
17
19
pub connection_info : ConnectionInfo ,
20
+ timeout : Duration ,
18
21
base_url : String ,
19
22
}
20
23
@@ -24,6 +27,7 @@ impl Client {
24
27
let base_url = format ! ( "{}/api/v1" , connection_info. url) ;
25
28
Self {
26
29
connection_info,
30
+ timeout : Duration :: from_secs ( 5 ) ,
27
31
base_url,
28
32
}
29
33
}
@@ -36,7 +40,7 @@ impl Client {
36
40
pub async fn whitelist_torrent ( & self , info_hash : & str ) -> Result < Response , Error > {
37
41
let request_url = format ! ( "{}/whitelist/{}" , self . base_url, info_hash) ;
38
42
39
- let client = reqwest:: Client :: new ( ) ;
43
+ let client = reqwest:: Client :: builder ( ) . timeout ( self . timeout ) . build ( ) ? ;
40
44
41
45
let params = [ ( "token" , & self . connection_info . token ) ] ;
42
46
@@ -51,7 +55,7 @@ impl Client {
51
55
pub async fn remove_torrent_from_whitelist ( & self , info_hash : & str ) -> Result < Response , Error > {
52
56
let request_url = format ! ( "{}/whitelist/{}" , self . base_url, info_hash) ;
53
57
54
- let client = reqwest:: Client :: new ( ) ;
58
+ let client = reqwest:: Client :: builder ( ) . timeout ( self . timeout ) . build ( ) ? ;
55
59
56
60
let params = [ ( "token" , & self . connection_info . token ) ] ;
57
61
@@ -66,7 +70,7 @@ impl Client {
66
70
pub async fn retrieve_new_tracker_key ( & self , token_valid_seconds : u64 ) -> Result < Response , Error > {
67
71
let request_url = format ! ( "{}/key/{}" , self . base_url, token_valid_seconds) ;
68
72
69
- let client = reqwest:: Client :: new ( ) ;
73
+ let client = reqwest:: Client :: builder ( ) . timeout ( self . timeout ) . build ( ) ? ;
70
74
71
75
let params = [ ( "token" , & self . connection_info . token ) ] ;
72
76
@@ -81,7 +85,7 @@ impl Client {
81
85
pub async fn get_torrent_info ( & self , info_hash : & str ) -> Result < Response , Error > {
82
86
let request_url = format ! ( "{}/torrent/{}" , self . base_url, info_hash) ;
83
87
84
- let client = reqwest:: Client :: new ( ) ;
88
+ let client = reqwest:: Client :: builder ( ) . timeout ( self . timeout ) . build ( ) ? ;
85
89
86
90
let params = [ ( "token" , & self . connection_info . token ) ] ;
87
91
0 commit comments