generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 14
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
1 parent
c08932a
commit 4d58a72
Showing
7 changed files
with
90 additions
and
11 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
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
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,62 @@ | ||
use crate::{Client, FetchOptions, Method, Response, Result}; | ||
use reqwest::blocking::Client as ReqwestBlockingClient; | ||
use reqwest::header::HeaderMap; | ||
use std::collections::HashMap; | ||
use std::convert::TryFrom; | ||
|
||
pub struct ReqwestClient { | ||
client: ReqwestBlockingClient, | ||
} | ||
|
||
impl ReqwestClient { | ||
pub fn new() -> Self { | ||
ReqwestClient { | ||
client: ReqwestBlockingClient::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Client for ReqwestClient { | ||
fn fetch(&self, url: &str, options: Option<FetchOptions>) -> Result<Response> { | ||
let options = options.unwrap_or_default(); | ||
let method = options.method.unwrap_or(Method::Get).to_string(); | ||
|
||
let mut req = match method.as_str() { | ||
"GET" => self.client.get(url), | ||
"POST" => self.client.post(url), | ||
"PUT" => self.client.put(url), | ||
_ => unreachable!(), | ||
}; | ||
|
||
if let Some(headers) = options.headers { | ||
let mut req_headers = HeaderMap::new(); | ||
for (key, value) in headers { | ||
req_headers.insert( | ||
reqwest::header::HeaderName::try_from(key.as_str()).unwrap(), | ||
value.parse().unwrap(), | ||
); | ||
} | ||
req = req.headers(req_headers); | ||
} | ||
|
||
if let Some(body) = options.body { | ||
req = req.body(body); | ||
} | ||
|
||
let res = req.send().map_err(crate::Error::from)?; | ||
|
||
let status_code = res.status().as_u16(); | ||
let mut headers = HashMap::new(); | ||
for (key, value) in res.headers().iter() { | ||
headers.insert(key.to_string(), value.to_str().unwrap().to_string()); | ||
} | ||
|
||
let body = res.bytes().map_err(crate::Error::from)?.to_vec(); | ||
|
||
Ok(Response { | ||
status_code, | ||
headers, | ||
body, | ||
}) | ||
} | ||
} |
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