rust_api_kit/http/client/
integration.rs1use super::{RequestError, RequestMethod, UnexpectedHttpError};
2
3impl<T> From<serde_json::Error> for UnexpectedHttpError<T> {
4 fn from(_: serde_json::Error) -> Self {
5 UnexpectedHttpError::Request(RequestError::Deserialize)
6 }
7}
8
9impl<T> From<reqwest::Error> for UnexpectedHttpError<T> {
10 fn from(value: reqwest::Error) -> Self {
11 log_error(format!("Request error {:?}", value));
12
13 let request_error = if let Some(status) = value.status() {
14 RequestError::Http(status.as_u16())
15 } else if value.is_timeout() {
16 RequestError::Timeout
17 } else if value.is_connect() {
18 RequestError::Connect
19 } else if value.is_redirect() {
20 RequestError::Redirect
21 } else if value.is_decode() {
22 RequestError::Decode
23 } else if value.is_builder() {
24 RequestError::Builder
25 } else {
26 RequestError::Unknown
27 };
28
29 UnexpectedHttpError::Request(request_error)
30 }
31}
32
33impl From<RequestMethod> for reqwest::Method {
34 fn from(val: RequestMethod) -> Self {
35 match val {
36 RequestMethod::POST => reqwest::Method::POST,
37 RequestMethod::GET => reqwest::Method::GET,
38 RequestMethod::PUT => reqwest::Method::PUT,
39 RequestMethod::DELETE => reqwest::Method::DELETE,
40 }
41 }
42}
43
44#[allow(unused_variables)]
45pub fn log_error(message: String) {
46 #[cfg(feature = "tracing")]
47 tracing::error!(message)
48}