@@ -3,6 +3,7 @@ pub mod responses;
33
44use std:: net:: IpAddr ;
55
6+ use anyhow:: { anyhow, Result } ;
67use requests:: announce:: { self , Query } ;
78use requests:: scrape;
89use reqwest:: { Client as ReqwestClient , Response , Url } ;
@@ -25,78 +26,83 @@ pub struct Client {
2526/// base url path query
2627/// ```
2728impl Client {
28- /// # Panics
29+ /// # Errors
2930 ///
3031 /// 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 {
3435 base_url,
35- reqwest : reqwest :: Client :: builder ( ) . build ( ) . unwrap ( ) ,
36+ reqwest,
3637 key : None ,
37- }
38+ } )
3839 }
3940
4041 /// Creates the new client binding it to an specific local address.
4142 ///
42- /// # Panics
43+ /// # Errors
4344 ///
4445 /// 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 {
4849 base_url,
49- reqwest : reqwest :: Client :: builder ( ) . local_address ( local_address ) . build ( ) . unwrap ( ) ,
50+ reqwest,
5051 key : None ,
51- }
52+ } )
5253 }
5354
54- /// # Panics
55+ /// # Errors
5556 ///
5657 /// 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 {
6061 base_url,
61- reqwest : reqwest :: Client :: builder ( ) . build ( ) . unwrap ( ) ,
62+ reqwest,
6263 key : Some ( key) ,
63- }
64+ } )
6465 }
6566
66- pub async fn announce ( & self , query : & announce:: Query ) -> Response {
67+ /// # Errors
68+ pub async fn announce ( & self , query : & announce:: Query ) -> Result < Response > {
6769 self . get ( & self . build_announce_path_and_query ( query) ) . await
6870 }
6971
70- pub async fn scrape ( & self , query : & scrape:: Query ) -> Response {
72+ /// # Errors
73+ pub async fn scrape ( & self , query : & scrape:: Query ) -> Result < Response > {
7174 self . get ( & self . build_scrape_path_and_query ( query) ) . await
7275 }
7376
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 > {
7579 self . get_with_header ( & self . build_announce_path_and_query ( query) , key, value)
7680 . await
7781 }
7882
79- pub async fn health_check ( & self ) -> Response {
83+ /// # Errors
84+ pub async fn health_check ( & self ) -> Result < Response > {
8085 self . get ( & self . build_path ( "health_check" ) ) . await
8186 }
8287
83- /// # Panics
88+ /// # Errors
8489 ///
8590 /// 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+ }
8896 }
8997
90- /// # Panics
98+ /// # Errors
9199 ///
92100 /// 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+ }
100106 }
101107
102108 fn build_announce_path_and_query ( & self , query : & announce:: Query ) -> String {
0 commit comments