1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use super :: TokenProvider ;
15+ use super :: { NetworkError , NetworkResult , TokenProvider } ;
1616use crate :: models:: ConfigurationJson ;
17- use crate :: { ConfigurationId , Error , Result } ;
17+ use crate :: ConfigurationId ;
1818use reqwest:: blocking:: Client ;
1919use std:: cell:: RefCell ;
2020use tungstenite:: stream:: MaybeTlsStream ;
@@ -101,7 +101,7 @@ impl ServerClientImpl {
101101 pub fn new (
102102 service_address : ServiceAddress ,
103103 token_provider : Box < dyn TokenProvider > ,
104- ) -> Result < Self > {
104+ ) -> NetworkResult < Self > {
105105 let access_token = RefCell :: new ( token_provider. get_access_token ( ) ?) ;
106106 Ok ( Self {
107107 service_address,
@@ -110,29 +110,31 @@ impl ServerClientImpl {
110110 } )
111111 }
112112
113- pub fn get_configuration ( & self , collection : & ConfigurationId ) -> Result < ConfigurationJson > {
113+ pub fn get_configuration (
114+ & self ,
115+ configuration_id : & ConfigurationId ,
116+ ) -> NetworkResult < ConfigurationJson > {
114117 let url = format ! (
115118 "{}/feature/v1/instances/{}/config" ,
116119 self . service_address. base_url( ServiceAddressProtocol :: Http ) ,
117- collection . guid
120+ configuration_id . guid
118121 ) ;
122+ let url = Url :: parse ( & url) . map_err ( |_| NetworkError :: UrlParseError ( url) ) ?;
119123 let client = Client :: new ( ) ;
120124 let r = client
121125 . get ( url)
122126 . query ( & [
123127 ( "action" , "sdkConfig" ) ,
124- ( "environment_id" , & collection . environment_id ) ,
125- ( "collection_id" , & collection . collection_id ) ,
128+ ( "environment_id" , & configuration_id . environment_id ) ,
129+ ( "collection_id" , & configuration_id . collection_id ) ,
126130 ] )
127131 . header ( "Accept" , "application/json" )
128132 . header ( "User-Agent" , "appconfiguration-rust-sdk/0.0.1" )
129133 . bearer_auth ( self . access_token . borrow ( ) )
130134 . send ( ) ;
131135
132136 match r {
133- Ok ( response) => response. json ( ) . map_err ( |_| {
134- Error :: ProtocolError ( "Failed to deserialize JSON from server response" . to_string ( ) )
135- } ) ,
137+ Ok ( response) => response. json ( ) . map_err ( |_| NetworkError :: ProtocolError ) ,
136138 Err ( e) => {
137139 // TODO: Identify if token expired, get new one and retry
138140 if false {
@@ -147,13 +149,12 @@ impl ServerClientImpl {
147149 pub fn get_configuration_monitoring_websocket (
148150 & self ,
149151 collection : & ConfigurationId ,
150- ) -> Result < ( WebSocket < MaybeTlsStream < TcpStream > > , Response ) > {
152+ ) -> NetworkResult < ( WebSocket < MaybeTlsStream < TcpStream > > , Response ) > {
151153 let ws_url = format ! (
152154 "{}/wsfeature" ,
153155 self . service_address. base_url( ServiceAddressProtocol :: Ws )
154156 ) ;
155- let mut ws_url = Url :: parse ( & ws_url)
156- . map_err ( |e| Error :: Other ( format ! ( "Cannot parse '{}' as URL: {}" , ws_url, e) ) ) ?;
157+ let mut ws_url = Url :: parse ( & ws_url) . map_err ( |_| NetworkError :: UrlParseError ( ws_url) ) ?;
157158
158159 ws_url
159160 . query_pairs_mut ( )
@@ -164,21 +165,19 @@ impl ServerClientImpl {
164165 let mut request = ws_url
165166 . as_str ( )
166167 . into_client_request ( )
167- . map_err ( Error :: TungsteniteError ) ?;
168+ . map_err ( NetworkError :: TungsteniteError ) ?;
168169 let headers = request. headers_mut ( ) ;
169170 headers. insert (
170171 "User-Agent" ,
171172 "appconfiguration-rust-sdk/0.0.1"
172173 . parse ( )
173- . map_err ( |_| Error :: Other ( "Invalid header value for ' User-Agent' ". to_string ( ) ) ) ?,
174+ . map_err ( |_| NetworkError :: InvalidHeaderValue ( " User-Agent". to_string ( ) ) ) ?,
174175 ) ;
175176 headers. insert (
176177 "Authorization" ,
177178 format ! ( "Bearer {}" , self . access_token. borrow( ) )
178179 . parse ( )
179- . map_err ( |_| {
180- Error :: Other ( "Invalid header value for 'Authorization'" . to_string ( ) )
181- } ) ?,
180+ . map_err ( |_| NetworkError :: InvalidHeaderValue ( "Authorization" . to_string ( ) ) ) ?,
182181 ) ;
183182
184183 Ok ( connect ( request) ?)
0 commit comments