-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
37 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
use crate::commands::run::RequestArgs; | ||
use rede_parser::Request; | ||
use reqwest::{ClientBuilder, Request as Reqwest, RequestBuilder, Url}; | ||
use reqwest::redirect::Policy; | ||
use reqwest::{Client, ClientBuilder, Request as Reqwest, RequestBuilder, Url}; | ||
|
||
use crate::errors::RequestError; | ||
|
||
pub async fn send(req: Request, args: RequestArgs) -> Result<String, RequestError<reqwest::Error>> { | ||
let url = Url::parse(&req.url).map_err(|e| RequestError::invalid_url(&req.url, e))?; | ||
|
||
let mut client = ClientBuilder::new(); | ||
if let Some(timeout) = args.timeout { | ||
client = client.timeout(timeout); | ||
} | ||
|
||
let client = build_client(&args)?; | ||
let reqwest = Reqwest::new(req.method, url); | ||
// todo handle build errors | ||
let builder = RequestBuilder::from_parts(client.build()?, reqwest).version(req.http_version); | ||
let builder = RequestBuilder::from_parts(client, reqwest).version(req.http_version); | ||
// todo handle send errors | ||
// todo handle text errors | ||
Ok(builder.send().await?.text().await?) | ||
} | ||
|
||
fn build_client(args: &RequestArgs) -> Result<Client, reqwest::Error> { | ||
let mut client = ClientBuilder::new(); | ||
if let Some(timeout) = args.timeout { | ||
client = client.timeout(timeout); | ||
} | ||
client = match (args.no_redirect, args.max_redirects) { | ||
(true, _) => client.redirect(Policy::none()), | ||
(false, Some(val)) => client.redirect(Policy::limited(val)), | ||
_ => client, | ||
}; | ||
client.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http.url = "http://localhost:8080/api/redirect_loop" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters