-
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.
Merge pull request #37 from animo/feat/apikey_header
feat: apikey from cli arg or config
- Loading branch information
Showing
5 changed files
with
99 additions
and
60 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 |
---|---|---|
@@ -1,49 +1,71 @@ | ||
use reqwest::{Client, RequestBuilder, Url}; | ||
use serde::de::DeserializeOwned; | ||
use serde_json::Value; | ||
|
||
use async_trait::async_trait; | ||
use crate::agent::http_agent::HttpAgent; | ||
use crate::error::{throw, Error}; | ||
|
||
/// Builds a get request and calls the sender | ||
pub async fn get<T: DeserializeOwned>(url: Url, query: Option<Vec<(&str, String)>>) -> T { | ||
let client = match query { | ||
Some(q) => Client::new().get(url).query(&q), | ||
None => Client::new().get(url), | ||
}; | ||
|
||
send::<T>(client).await | ||
/// Interface providing HTTP call methods | ||
#[async_trait] | ||
pub trait HttpCalls { | ||
/// GET method | ||
async fn get<T: DeserializeOwned>(&self, url: Url, query: Option<Vec<(&str, String)>>) -> T; | ||
/// POST method | ||
async fn post<T: DeserializeOwned>(&self, url: Url, query: Option<Vec<(&str, String)>>, body: Option<Value>) -> T; | ||
/// SEND - general method for GET and POST | ||
async fn send<T: DeserializeOwned>(&self, client: RequestBuilder) -> T; | ||
} | ||
|
||
/// Builds a post request and calls the sender | ||
pub async fn post<T: DeserializeOwned>( | ||
url: Url, | ||
query: Option<Vec<(&str, String)>>, | ||
body: Option<Value>, | ||
) -> T { | ||
let client = Client::new().post(url).query(&query); | ||
/// Call logic for http calls | ||
#[async_trait] | ||
impl HttpCalls for HttpAgent { | ||
/// Builds a get request and calls the sender | ||
async fn get<T: DeserializeOwned>(&self, url: Url, query: Option<Vec<(&str, String)>>) -> T { | ||
let client = match query { | ||
Some(q) => Client::new().get(url).query(&q), | ||
None => Client::new().get(url), | ||
}; | ||
|
||
self.send::<T>(client).await | ||
} | ||
|
||
let client = match body { | ||
Some(b) => client.json(&b), | ||
None => client, | ||
}; | ||
/// Builds a post request and calls the sender | ||
async fn post<T: DeserializeOwned>( | ||
&self, | ||
url: Url, | ||
query: Option<Vec<(&str, String)>>, | ||
body: Option<Value>, | ||
) -> T { | ||
let client = Client::new().post(url).query(&query); | ||
|
||
send::<T>(client).await | ||
} | ||
let client = match body { | ||
Some(b) => client.json(&b), | ||
None => client, | ||
}; | ||
|
||
self.send::<T>(client).await | ||
} | ||
|
||
/// Sends any request | ||
async fn send<T: DeserializeOwned>(&self, client: RequestBuilder) -> T { | ||
let client = match &self.api_key { | ||
Some(a) => client.header("X-API-KEY", a), | ||
None => client, | ||
}; | ||
|
||
let response = client.send().await; | ||
|
||
/// Sends any request | ||
async fn send<T: DeserializeOwned>(client: RequestBuilder) -> T { | ||
let response = client.send().await; | ||
|
||
match response { | ||
Ok(res) => { | ||
if res.status().is_success() { | ||
return match res.json().await { | ||
Ok(parsed) => parsed, | ||
Err(_) => throw(Error::ServerResponseParseError), | ||
}; | ||
match response { | ||
Ok(res) => { | ||
if res.status().is_success() { | ||
return match res.json().await { | ||
Ok(parsed) => parsed, | ||
Err(_) => throw(Error::ServerResponseParseError), | ||
}; | ||
} | ||
throw(Error::InternalServerError) | ||
} | ||
throw(Error::InternalServerError) | ||
Err(_) => throw(Error::InternalServerError), | ||
} | ||
Err(_) => throw(Error::InternalServerError), | ||
} | ||
} |