Skip to content

Commit

Permalink
updated http request builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Jan 2, 2025
1 parent 539e5f9 commit f2b2ab6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ pub enum FetcherError {
}

#[cfg(feature = "proto-http")]
pub use crate::fetcher::http::{request::HttpRequest, response::HttpResponse, CompleteHttpFetcher, HttpBody, HttpMethod};
pub use crate::fetcher::http::{response::HttpResponse, CompleteHttpFetcher, HttpBody, HttpMethod};

#[cfg(feature = "proto-ftp")]
pub use crate::fetcher::ftp::{fetcher::FtpFetcher, fetcher::FtpRequest, fetcher::FtpResponse};

#[cfg(feature = "proto-gopher")]
pub use crate::fetcher::gopher::{fetcher::GopherFetcher, fetcher::GopherRequest, fetcher::GopherResponse};
use crate::fetcher::http::request::HttpRequestBuilder;

enum Response {
#[cfg(feature = "proto-http")]
Expand Down Expand Up @@ -98,7 +99,7 @@ impl Fetcher {
match scheme {
#[cfg(feature = "proto-http")]
"https" | "http" => {
let request = HttpRequest::new(HttpMethod::Get, url);
let request = HttpRequestBuilder::new(HttpMethod::Get, url).build();
match self.http_fetcher.fetch_with_request(request).await {
Ok(response) => Ok(Response::Http(response)),
Err(e) => Err(FetcherError::Http(e)),
Expand Down
4 changes: 2 additions & 2 deletions src/fetcher/http/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fetcher::http::agents::HttpRequestAgent;
use crate::fetcher::http::request::HttpRequest;
use crate::fetcher::http::request::{HttpRequest, HttpRequestBuilder};
use crate::fetcher::http::response::HttpResponse;
use crate::fetcher::http::HttpError;
use crate::fetcher::http::HttpMethod;
Expand Down Expand Up @@ -30,7 +30,7 @@ impl<R: HttpRequestAgent> HttpFetcher<R> {
/// Simple fetch with just method and URL.
pub async fn fetch(&self, method: HttpMethod, url: Url) -> Result<HttpResponse, HttpError> {
info!(target: "fetcher", "HTTP fetching: {:?}", url);
let req = HttpRequest::new(method, url);
let req = HttpRequestBuilder::new(method, url).build();
self.agent.execute(req).await
}

Expand Down
54 changes: 29 additions & 25 deletions src/fetcher/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ pub struct HttpRequest {
pub cookies: HashMap<String, String>,
}

impl HttpRequest {
/// Create a new http request with the given method and URL
/// Builder for HttpRequest
#[derive(Clone, Debug)]
pub struct HttpRequestBuilder {
method: HttpMethod,
url: Url,
query_params: HashMap<String, String>,
headers: HashMap<String, String>,
body: HttpBody,
cookies: HashMap<String, String>,
}

impl HttpRequestBuilder {
pub fn new(method: HttpMethod, url: Url) -> Self {
Self {
method,
Expand All @@ -31,42 +41,36 @@ impl HttpRequest {
cookies: HashMap::new(),
}
}
}

/// Builder for HttpRequest
struct HttpRequestBuilder {
request: HttpRequest,
}

impl HttpRequestBuilder {
pub fn new(method: HttpMethod, url: Url) -> Self {
Self {
request: HttpRequest::new(method, url),
}
}

pub fn query_param(&mut self, key: &str, value: &str) -> &mut Self {
self.request.query_params.insert(key.to_string(), value.to_string());
pub fn query_param(mut self, key: &str, value: &str) -> Self {
self.query_params.insert(key.to_string(), value.to_string());
self
}

pub fn header(&mut self, key: &str, value: &str) -> &mut Self {
self.request.headers.insert(key.to_string(), value.to_string());
pub fn header(mut self, key: &str, value: &str) -> Self {
self.headers.insert(key.to_string(), value.to_string());
self
}

pub fn cookie(&mut self, key: &str, value: &str) -> &mut Self {
self.request.cookies.insert(key.to_string(), value.to_string());
pub fn cookie(mut self, key: &str, value: &str) -> Self {
self.cookies.insert(key.to_string(), value.to_string());
self
}

pub fn body(&mut self, body: HttpBody) -> &mut Self {
self.request.body = body;
pub fn body(mut self, body: HttpBody) -> Self {
self.body = body;
self
}

pub fn build(&self) -> HttpRequest {
self.request.clone()
pub fn build(self) -> HttpRequest {
HttpRequest {
method: self.method,
url: self.url,
query_params: self.query_params,
headers: self.headers,
body: self.body,
cookies: self.cookies,
}
}
}

Expand Down

0 comments on commit f2b2ab6

Please sign in to comment.