refactor: bring body into request()
This commit is contained in:
parent
ffe00e074b
commit
9fe18ab1b0
64
src/lib.rs
64
src/lib.rs
|
@ -35,9 +35,18 @@ pub struct VehicleId(u64);
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct ExternalVehicleId(u64);
|
pub struct ExternalVehicleId(u64);
|
||||||
|
|
||||||
pub enum MethodWithPayload {
|
pub enum RequestData<'a> {
|
||||||
GET,
|
GET { url: &'a str },
|
||||||
POST(String),
|
POST { url: &'a str, payload: &'a str },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for RequestData<'_> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
RequestData::GET { url } => write!(f, "GET {}", url),
|
||||||
|
RequestData::POST { url, payload } => write!(f, "POST {} {}", url, payload),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Api {
|
pub struct Api {
|
||||||
|
@ -66,15 +75,8 @@ impl Api {
|
||||||
{
|
{
|
||||||
let request_context = || format!("GET {url}");
|
let request_context = || format!("GET {url}");
|
||||||
|
|
||||||
let response = self.request(MethodWithPayload::GET, url).await?;
|
let body = self.request(RequestData::GET { url }).await?;
|
||||||
|
|
||||||
let body = response
|
|
||||||
.text()
|
|
||||||
.await
|
|
||||||
.map_err(|source| TeslatteError::FetchError {
|
|
||||||
source,
|
|
||||||
request: request_context(),
|
|
||||||
})?;
|
|
||||||
trace!(?body);
|
trace!(?body);
|
||||||
|
|
||||||
let data = Self::parse_json::<D, _>(&body, request_context)?;
|
let data = Self::parse_json::<D, _>(&body, request_context)?;
|
||||||
|
@ -88,22 +90,13 @@ impl Api {
|
||||||
where
|
where
|
||||||
S: Serialize + Debug,
|
S: Serialize + Debug,
|
||||||
{
|
{
|
||||||
trace!("Fetching");
|
|
||||||
|
|
||||||
let request_context = || format!("POST {url}");
|
let request_context = || format!("POST {url}");
|
||||||
|
|
||||||
let payload =
|
let payload =
|
||||||
serde_json::to_string(&body).expect("Should not fail creating the request struct.");
|
&serde_json::to_string(&body).expect("Should not fail creating the request struct.");
|
||||||
|
|
||||||
let response = self.request(MethodWithPayload::POST(payload), url).await?;
|
let body = self.request(RequestData::POST { url, payload }).await?;
|
||||||
|
|
||||||
let body = response
|
|
||||||
.text()
|
|
||||||
.await
|
|
||||||
.map_err(|source| TeslatteError::FetchError {
|
|
||||||
source,
|
|
||||||
request: request_context(),
|
|
||||||
})?;
|
|
||||||
let data = Self::parse_json::<PostResponse, _>(&body, request_context)?;
|
let data = Self::parse_json::<PostResponse, _>(&body, request_context)?;
|
||||||
trace!(?data);
|
trace!(?data);
|
||||||
|
|
||||||
|
@ -119,18 +112,16 @@ impl Api {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request(
|
async fn request(&self, request_data: RequestData<'_>) -> Result<String, TeslatteError> {
|
||||||
&self,
|
trace!("{request_data}");
|
||||||
method: MethodWithPayload,
|
|
||||||
url: &str,
|
let request_builder = match request_data {
|
||||||
) -> Result<reqwest::Response, TeslatteError> {
|
RequestData::GET { url } => self.client.get(url),
|
||||||
let request_builder = match &method {
|
RequestData::POST { url, payload } => self
|
||||||
MethodWithPayload::GET => self.client.get(url),
|
|
||||||
MethodWithPayload::POST(payload) => self
|
|
||||||
.client
|
.client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(payload.clone()),
|
.body(payload.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
request_builder
|
request_builder
|
||||||
|
@ -140,10 +131,13 @@ impl Api {
|
||||||
.await
|
.await
|
||||||
.map_err(|source| TeslatteError::FetchError {
|
.map_err(|source| TeslatteError::FetchError {
|
||||||
source,
|
source,
|
||||||
request: match method {
|
request: format!("{request_data}"),
|
||||||
MethodWithPayload::GET => format!("GET {url}"),
|
})?
|
||||||
MethodWithPayload::POST(payload) => format!("POST {url} {payload}"),
|
.text()
|
||||||
},
|
.await
|
||||||
|
.map_err(|source| TeslatteError::FetchError {
|
||||||
|
source,
|
||||||
|
request: format!("{request_data}"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue