Skip to content

Commit

Permalink
Added Header Json when post Json content
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Feb 22, 2024
1 parent 23af541 commit 55002ff
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 19 deletions.
19 changes: 11 additions & 8 deletions src/fl_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::DropConnectionScenario;

use crate::FlUrlError;

use crate::FlUrlHeaders;
use crate::HttpClient;
use crate::UrlBuilder;

Expand All @@ -20,7 +21,7 @@ lazy_static::lazy_static! {

pub struct FlUrl {
pub url: UrlBuilder,
pub headers: Vec<(StrOrString<'static>, String)>,
pub headers: FlUrlHeaders,
pub client_cert: Option<my_tls::ClientCertificate>,
pub accept_invalid_certificate: bool,
pub execute_timeout: Duration,
Expand All @@ -33,12 +34,9 @@ impl FlUrl {
pub fn new<'s>(url: impl Into<StrOrString<'s>>) -> Self {
let url: StrOrString<'s> = url.into();
let url = UrlBuilder::new(ShortString::from_str(url.as_str()).unwrap());
let mut headers = Vec::new();

headers.shrink_to(16);

Self {
headers,
headers: FlUrlHeaders::new(),
execute_timeout: Duration::from_secs(30),
client_cert: None,
url,
Expand Down Expand Up @@ -113,7 +111,7 @@ impl FlUrl {
let name: StrOrString<'static> = name.into();
let value: StrOrString<'v> = value.into();

self.headers.push((name, value.to_string()));
self.headers.add(name, value.to_string());
self
}

Expand Down Expand Up @@ -194,7 +192,10 @@ impl FlUrl {

pub async fn post_json(self, json: impl serde::Serialize) -> Result<FlUrlResponse, FlUrlError> {
let body = serde_json::to_vec(&json).unwrap();
self.execute(Method::POST, Some(body)).await

self.with_header("Content-Type", "application/json")
.execute(Method::POST, Some(body))
.await
}

pub async fn put(self, body: Option<Vec<u8>>) -> Result<FlUrlResponse, FlUrlError> {
Expand All @@ -203,7 +204,9 @@ impl FlUrl {

pub async fn put_json(self, json: impl serde::Serialize) -> Result<FlUrlResponse, FlUrlError> {
let body = serde_json::to_vec(&json).unwrap();
self.execute(Method::PUT, Some(body)).await
self.with_header("Content-Type", "application/json")
.execute(Method::PUT, Some(body))
.await
}

pub async fn delete(self) -> Result<FlUrlResponse, FlUrlError> {
Expand Down
41 changes: 41 additions & 0 deletions src/fl_url_headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use rust_extensions::StrOrString;

pub struct FlUrlHeader {
pub name: StrOrString<'static>,
pub value: String,
}

pub struct FlUrlHeaders {
headers: Vec<FlUrlHeader>,
}

impl FlUrlHeaders {
pub fn new() -> Self {
Self {
headers: Vec::new(),
}
}

fn find_index(&self, name: &str) -> Option<usize> {
self.headers.iter().position(|header| {
rust_extensions::str_utils::compare_strings_case_insensitive(header.name.as_str(), name)
})
}

pub fn add(&mut self, name: StrOrString<'static>, value: String) {
match self.find_index(name.as_str()) {
Some(index) => self.headers[index].value = value,
None => {
self.headers.push(FlUrlHeader { name, value });
}
}
}

pub fn len(&self) -> usize {
self.headers.len()
}

pub fn iter(&self) -> std::slice::Iter<FlUrlHeader> {
self.headers.iter()
}
}
46 changes: 35 additions & 11 deletions src/http_client/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hyper::{client::conn::http1::SendRequest, Method, Request, Uri};
use rust_extensions::{date_time::DateTimeAsMicroseconds, StrOrString};
use tokio::sync::Mutex;

use crate::{FlUrlError, FlUrlResponse, UrlBuilder, UrlBuilderOwned};
use crate::{FlUrlError, FlUrlHeaders, FlUrlResponse, UrlBuilder, UrlBuilderOwned};

use my_tls::ClientCertificate;

Expand Down Expand Up @@ -79,7 +79,7 @@ impl HttpClient {
&self,
url_builder: &UrlBuilder,
method: Method,
headers: &Vec<(StrOrString<'static>, String)>,
headers: &FlUrlHeaders,
body: Option<Vec<u8>>,
request_timeout: Duration,
) -> Result<FlUrlResponse, FlUrlError> {
Expand All @@ -90,7 +90,7 @@ impl HttpClient {
.execute_int(
&url_builder_owned,
&method,
headers,
&headers,
body.clone(),
request_timeout,
)
Expand Down Expand Up @@ -128,7 +128,7 @@ impl HttpClient {
&self,
url_builder: &UrlBuilderOwned,
method: &Method,
headers: &Vec<(StrOrString<'static>, String)>,
headers: &FlUrlHeaders,
body: Option<Vec<u8>>,
request_timeout: Duration,
) -> Result<FlUrlResponse, FlUrlError> {
Expand All @@ -151,8 +151,8 @@ impl HttpClient {
);

if headers.len() > 0 {
for (key, value) in headers {
request = request.header(key.as_str(), value);
for header in headers.iter() {
request = request.header(header.name.as_str(), header.value.to_string());
}
};
}
Expand Down Expand Up @@ -208,7 +208,7 @@ mod tests {
use rust_extensions::StopWatch;

use super::HttpClient;
use crate::UrlBuilder;
use crate::{FlUrlHeaders, UrlBuilder};

static REQUEST_TIMEOUT: Duration = Duration::from_secs(5);

Expand All @@ -225,7 +225,13 @@ mod tests {
sw.start();

let mut response = fl_url_client
.execute_request(&url_builder, Method::GET, &vec![], None, REQUEST_TIMEOUT)
.execute_request(
&url_builder,
Method::GET,
&FlUrlHeaders::new(),
None,
REQUEST_TIMEOUT,
)
.await
.unwrap();
println!("StatusCode: {}", response.get_status_code());
Expand All @@ -238,7 +244,13 @@ mod tests {
sw.start();

let mut response = fl_url_client
.execute_request(&url_builder, Method::GET, &vec![], None, REQUEST_TIMEOUT)
.execute_request(
&url_builder,
Method::GET,
&FlUrlHeaders::new(),
None,
REQUEST_TIMEOUT,
)
.await
.unwrap();
println!("StatusCode: {}", response.get_status_code());
Expand All @@ -261,7 +273,13 @@ mod tests {
sw.start();

let mut response = fl_url_client
.execute_request(&url_builder, Method::GET, &vec![], None, REQUEST_TIMEOUT)
.execute_request(
&url_builder,
Method::GET,
&FlUrlHeaders::new(),
None,
REQUEST_TIMEOUT,
)
.await
.unwrap();
println!("StatusCode: {}", response.get_status_code());
Expand All @@ -274,7 +292,13 @@ mod tests {
sw.start();

let mut response = fl_url_client
.execute_request(&url_builder, Method::GET, &vec![], None, REQUEST_TIMEOUT)
.execute_request(
&url_builder,
Method::GET,
&FlUrlHeaders::new(),
None,
REQUEST_TIMEOUT,
)
.await
.unwrap();
println!("StatusCode: {}", response.get_status_code());
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ mod errors;
pub use errors::*;

pub extern crate my_tls;
mod fl_url_headers;
pub use fl_url_headers::*;

0 comments on commit 55002ff

Please sign in to comment.