-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
51 additions
and
47 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,56 +1,11 @@ | ||
mod access_token; | ||
mod authorization; | ||
mod error; | ||
mod request; | ||
mod retrieve; | ||
|
||
pub use access_token::*; | ||
pub use authorization::*; | ||
pub use error::*; | ||
use hyper::StatusCode; | ||
use reqwest::Response; | ||
pub(crate) use request::*; | ||
pub use retrieve::*; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
fn check_status_code(response: &Response) -> Option<Error> { | ||
let status = response.status(); | ||
if status == StatusCode::OK { | ||
return None; | ||
} | ||
|
||
let headers = response.headers(); | ||
let x_error_code = headers.get("X-Error-Code"); | ||
let x_error = headers.get("X-Error"); | ||
Some(Error::Status { | ||
status_code: status.as_u16(), | ||
x_error_code: x_error_code | ||
.map(|v| v.to_str()) | ||
.transpose() | ||
.unwrap() | ||
.map(|v| v.to_owned()), | ||
x_error: x_error | ||
.map(|v| v.to_str()) | ||
.transpose() | ||
.unwrap() | ||
.map(|v| v.to_owned()), | ||
}) | ||
} | ||
|
||
async fn post<T, U>(url: &str, body: &T) -> Result<U, Error> | ||
where | ||
T: Serialize + ?Sized, | ||
U: DeserializeOwned, | ||
{ | ||
let client = reqwest::Client::new(); | ||
let response = client | ||
.post(url) | ||
.header("Content-Type", "application/json; charset=UTF8") | ||
.header("X-Accept", "application/json") | ||
.json(&body) | ||
.send() | ||
.await?; | ||
if let Some(error) = check_status_code(&response) { | ||
return Err(error); | ||
} | ||
let response_body = response.json::<U>().await?; | ||
Ok(response_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use hyper::StatusCode; | ||
use reqwest::Response; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
use crate::Error; | ||
|
||
fn check_status_code(response: &Response) -> Option<Error> { | ||
let status = response.status(); | ||
if status == StatusCode::OK { | ||
return None; | ||
} | ||
|
||
let headers = response.headers(); | ||
let x_error_code = headers.get("X-Error-Code"); | ||
let x_error = headers.get("X-Error"); | ||
Some(Error::Status { | ||
status_code: status.as_u16(), | ||
x_error_code: x_error_code | ||
.map(|v| v.to_str()) | ||
.transpose() | ||
.unwrap() | ||
.map(|v| v.to_owned()), | ||
x_error: x_error | ||
.map(|v| v.to_str()) | ||
.transpose() | ||
.unwrap() | ||
.map(|v| v.to_owned()), | ||
}) | ||
} | ||
|
||
pub async fn post<T, U>(url: &str, body: &T) -> Result<U, Error> | ||
where | ||
T: Serialize + ?Sized, | ||
U: DeserializeOwned, | ||
{ | ||
let client = reqwest::Client::new(); | ||
let response = client | ||
.post(url) | ||
.header("Content-Type", "application/json; charset=UTF8") | ||
.header("X-Accept", "application/json") | ||
.json(&body) | ||
.send() | ||
.await?; | ||
if let Some(error) = check_status_code(&response) { | ||
return Err(error); | ||
} | ||
let response_body = response.json::<U>().await?; | ||
Ok(response_body) | ||
} |