Skip to content

Commit

Permalink
add body support for NadeoRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
TgZ39 committed May 25, 2024
1 parent d54ca8f commit 04d59f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,19 @@ impl AuthInfo {
HttpMethod::Head => client.head(request.url),
};

let res = api_request
let mut res = api_request
.header("Authorization", token.parse::<HeaderValue>().unwrap())
.header(
"User-Agent",
meta_data.user_agent.parse::<HeaderValue>().unwrap(),
)
.headers(request.headers)
.send()
.await?
.error_for_status()?;
.headers(request.headers);
if let Some(json) = request.body {
res = res.body(json);
}

let res = res.send().await?.error_for_status()?;

Ok(res)
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/auth/o_auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,19 @@ impl OAuthInfo {
HttpMethod::Head => client.head(request.url),
};

let res = api_request
let mut res = api_request
.header("Authorization", token.parse::<HeaderValue>().unwrap())
.header(
"User-Agent",
meta_data.user_agent.parse::<HeaderValue>().unwrap(),
)
.headers(request.headers)
.send()
.await?
.error_for_status()?;
.headers(request.headers);
if let Some(json) = request.body {
res = res.body(json);
}

let res = res.send().await?.error_for_status()?;

Ok(res)
}
}
1 change: 1 addition & 0 deletions src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct NadeoRequest {
pub(crate) url: String,
pub(crate) method: HttpMethod,
pub(crate) headers: HeaderMap,
pub(crate) body: Option<String>,
}

impl NadeoRequest {
Expand Down
20 changes: 9 additions & 11 deletions src/request/request_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@ use serde::{Deserialize, Serialize};
/// [`NadeoRequest`]: NadeoRequest
/// [`HttpMethod`]: HttpMethod
/// [`AuthType`]: AuthType
#[derive(Default)]
pub struct NadeoRequestBuilder {
auth_type: Option<AuthType>,
url: Option<String>,
method: Option<HttpMethod>,
headers: HeaderMap,
}

impl Default for NadeoRequestBuilder {
fn default() -> Self {
NadeoRequestBuilder {
auth_type: None,
method: None,
headers: HeaderMap::new(),
url: None,
}
}
body: Option<String>,
}

/// Error when the Request is invalid. For example if a required field is missing.
Expand All @@ -40,6 +31,12 @@ pub enum RequestBuilderError {
}

impl NadeoRequestBuilder {
pub fn body(mut self, json: &str) -> Self {
self.body = Some(json.to_string());

self
}

pub fn url(mut self, url: &str) -> Self {
self.url = Some(url.to_string());

Expand Down Expand Up @@ -93,6 +90,7 @@ impl NadeoRequestBuilder {
method: self.method.unwrap(),
url: self.url.unwrap(),
headers: self.headers,
body: self.body,
})
}
}

0 comments on commit 04d59f3

Please sign in to comment.