Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept body optionvec or vec #56

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "curl-http-client"
version = "2.2.0"
version = "2.3.0"
edition = "2021"
authors = ["Lorenzo Leonardo <[email protected]>"]
license = "MIT"
Expand Down
30 changes: 26 additions & 4 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,20 @@ impl ExtendedHandler for Collector {
fn get_response_body(&self) -> Option<Vec<u8>> {
match self {
Collector::File(_) => None,
Collector::Ram(container) => Some(container.clone()),
Collector::RamAndHeaders(container, _) => Some(container.clone()),
Collector::Ram(container) => {
if container.is_empty() {
None
} else {
Some(container.clone())
}
}
Collector::RamAndHeaders(container, _) => {
if container.is_empty() {
None
} else {
Some(container.clone())
}
}
Collector::FileAndHeaders(_, _) => None,
}
}
Expand All @@ -338,7 +350,13 @@ impl ExtendedHandler for Collector {
fn get_response_body_and_headers(&self) -> (Option<Vec<u8>>, Option<HeaderMap>) {
match self {
Collector::File(_) => (None, None),
Collector::Ram(container) => (Some(container.clone()), None),
Collector::Ram(container) => {
if container.is_empty() {
(None, None)
} else {
(Some(container.clone()), None)
}
}
Collector::RamAndHeaders(container, headers) => {
let header_str = std::str::from_utf8(headers).unwrap();
let mut header_map = HeaderMap::new();
Expand All @@ -354,7 +372,11 @@ impl ExtendedHandler for Collector {
}
}
}
(Some(container.clone()), Some(header_map))
if container.is_empty() {
(None, Some(header_map))
} else {
(Some(container.clone()), Some(header_map))
}
}
Collector::FileAndHeaders(_, headers) => {
let header_str = std::str::from_utf8(headers).unwrap();
Expand Down
26 changes: 24 additions & 2 deletions src/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
///
/// The HttpRequest can be customized by the caller by setting the Url, Method Type,
/// Headers and the Body.
pub fn request(mut self, request: Request<Option<Vec<u8>>>) -> Result<Self, Error<C>> {
pub fn request<B: CurlBodyRequest>(mut self, request: Request<B>) -> Result<Self, Error<C>> {
self.easy
.url(request.uri().to_string().as_str())
.map_err(|e| {
Expand Down Expand Up @@ -94,7 +94,7 @@ where
Method::POST => {
self.easy.post(true).map_err(Error::Curl)?;

if let Some(body) = request.body() {
if let Some(body) = request.body().get_bytes() {
self.easy.post_field_size(body.len() as u64).map_err(|e| {
trace!("{:?}", e);
Error::Curl(e)
Expand Down Expand Up @@ -1013,3 +1013,25 @@ impl From<usize> for FileSize {
Self(value)
}
}

/// The purpose of this trait is to be able to accept
/// request body with Option<Vec<u8>> or Vec<u8>
pub trait CurlBodyRequest {
fn get_bytes(&self) -> Option<&Vec<u8>>;
}

impl CurlBodyRequest for Vec<u8> {
fn get_bytes(&self) -> Option<&Vec<u8>> {
if self.is_empty() {
None
} else {
Some(self)
}
}
}

impl CurlBodyRequest for Option<Vec<u8>> {
fn get_bytes(&self) -> Option<&Vec<u8>> {
self.as_ref()
}
}
Loading
Loading