@@ -3,6 +3,7 @@ pub mod responses;
3
3
4
4
use std:: net:: IpAddr ;
5
5
6
+ use anyhow:: { anyhow, Result } ;
6
7
use requests:: announce:: { self , Query } ;
7
8
use requests:: scrape;
8
9
use reqwest:: { Client as ReqwestClient , Response , Url } ;
@@ -25,78 +26,83 @@ pub struct Client {
25
26
/// base url path query
26
27
/// ```
27
28
impl Client {
28
- /// # Panics
29
+ /// # Errors
29
30
///
30
31
/// This method fails if the client builder fails.
31
- # [ must_use ]
32
- pub fn new ( base_url : Url ) -> Self {
33
- Self {
32
+ pub fn new ( base_url : Url ) -> Result < Self > {
33
+ let reqwest = reqwest :: Client :: builder ( ) . build ( ) ? ;
34
+ Ok ( Self {
34
35
base_url,
35
- reqwest : reqwest :: Client :: builder ( ) . build ( ) . unwrap ( ) ,
36
+ reqwest,
36
37
key : None ,
37
- }
38
+ } )
38
39
}
39
40
40
41
/// Creates the new client binding it to an specific local address.
41
42
///
42
- /// # Panics
43
+ /// # Errors
43
44
///
44
45
/// This method fails if the client builder fails.
45
- # [ must_use ]
46
- pub fn bind ( base_url : Url , local_address : IpAddr ) -> Self {
47
- Self {
46
+ pub fn bind ( base_url : Url , local_address : IpAddr ) -> Result < Self > {
47
+ let reqwest = reqwest :: Client :: builder ( ) . local_address ( local_address ) . build ( ) ? ;
48
+ Ok ( Self {
48
49
base_url,
49
- reqwest : reqwest :: Client :: builder ( ) . local_address ( local_address ) . build ( ) . unwrap ( ) ,
50
+ reqwest,
50
51
key : None ,
51
- }
52
+ } )
52
53
}
53
54
54
- /// # Panics
55
+ /// # Errors
55
56
///
56
57
/// This method fails if the client builder fails.
57
- # [ must_use ]
58
- pub fn authenticated ( base_url : Url , key : Key ) -> Self {
59
- Self {
58
+ pub fn authenticated ( base_url : Url , key : Key ) -> Result < Self > {
59
+ let reqwest = reqwest :: Client :: builder ( ) . build ( ) ? ;
60
+ Ok ( Self {
60
61
base_url,
61
- reqwest : reqwest :: Client :: builder ( ) . build ( ) . unwrap ( ) ,
62
+ reqwest,
62
63
key : Some ( key) ,
63
- }
64
+ } )
64
65
}
65
66
66
- pub async fn announce ( & self , query : & announce:: Query ) -> Response {
67
+ /// # Errors
68
+ pub async fn announce ( & self , query : & announce:: Query ) -> Result < Response > {
67
69
self . get ( & self . build_announce_path_and_query ( query) ) . await
68
70
}
69
71
70
- pub async fn scrape ( & self , query : & scrape:: Query ) -> Response {
72
+ /// # Errors
73
+ pub async fn scrape ( & self , query : & scrape:: Query ) -> Result < Response > {
71
74
self . get ( & self . build_scrape_path_and_query ( query) ) . await
72
75
}
73
76
74
- pub async fn announce_with_header ( & self , query : & Query , key : & str , value : & str ) -> Response {
77
+ /// # Errors
78
+ pub async fn announce_with_header ( & self , query : & Query , key : & str , value : & str ) -> Result < Response > {
75
79
self . get_with_header ( & self . build_announce_path_and_query ( query) , key, value)
76
80
. await
77
81
}
78
82
79
- pub async fn health_check ( & self ) -> Response {
83
+ /// # Errors
84
+ pub async fn health_check ( & self ) -> Result < Response > {
80
85
self . get ( & self . build_path ( "health_check" ) ) . await
81
86
}
82
87
83
- /// # Panics
88
+ /// # Errors
84
89
///
85
90
/// This method fails if there was an error while sending request.
86
- pub async fn get ( & self , path : & str ) -> Response {
87
- self . reqwest . get ( self . build_url ( path) ) . send ( ) . await . unwrap ( )
91
+ pub async fn get ( & self , path : & str ) -> Result < Response > {
92
+ match self . reqwest . get ( self . build_url ( path) ) . send ( ) . await {
93
+ Ok ( response) => Ok ( response) ,
94
+ Err ( err) => Err ( anyhow ! ( "{err}" ) ) ,
95
+ }
88
96
}
89
97
90
- /// # Panics
98
+ /// # Errors
91
99
///
92
100
/// This method fails if there was an error while sending request.
93
- pub async fn get_with_header ( & self , path : & str , key : & str , value : & str ) -> Response {
94
- self . reqwest
95
- . get ( self . build_url ( path) )
96
- . header ( key, value)
97
- . send ( )
98
- . await
99
- . unwrap ( )
101
+ pub async fn get_with_header ( & self , path : & str , key : & str , value : & str ) -> Result < Response > {
102
+ match self . reqwest . get ( self . build_url ( path) ) . header ( key, value) . send ( ) . await {
103
+ Ok ( response) => Ok ( response) ,
104
+ Err ( err) => Err ( anyhow ! ( "{err}" ) ) ,
105
+ }
100
106
}
101
107
102
108
fn build_announce_path_and_query ( & self , query : & announce:: Query ) -> String {
0 commit comments