Convert input into a base path, e.g. "http://example:123". Also checks the scheme as it goes.
(
input: impl TryInto<Uri, Error = hyper::http::uri::InvalidUri>,
correct_scheme: Option<&'static str>,
)
| 57 | |
| 58 | /// Convert input into a base path, e.g. "http://example:123". Also checks the scheme as it goes. |
| 59 | fn into_base_path( |
| 60 | input: impl TryInto<Uri, Error = hyper::http::uri::InvalidUri>, |
| 61 | correct_scheme: Option<&'static str>, |
| 62 | ) -> Result<String, ClientInitError> { |
| 63 | // First convert to Uri, since a base path is a subset of Uri. |
| 64 | let uri = input.try_into()?; |
| 65 | |
| 66 | let scheme = uri.scheme_str().ok_or(ClientInitError::InvalidScheme)?; |
| 67 | |
| 68 | // Check the scheme if necessary |
| 69 | if let Some(correct_scheme) = correct_scheme { |
| 70 | if scheme != correct_scheme { |
| 71 | return Err(ClientInitError::InvalidScheme); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | let host = uri.host().ok_or(ClientInitError::MissingHost)?; |
| 76 | let port = uri |
| 77 | .port_u16() |
| 78 | .map(|x| format!(":{}", x)) |
| 79 | .unwrap_or_default(); |
| 80 | Ok(format!( |
| 81 | "{}://{}{}{}", |
| 82 | scheme, |
| 83 | host, |
| 84 | port, |
| 85 | uri.path().trim_end_matches('/') |
| 86 | )) |
| 87 | } |
| 88 | |
| 89 | /// A client that implements the API by making HTTP calls out to a server. |
| 90 | pub struct Client<S, C> |
no test coverage detected