Skip to content

Commit

Permalink
Merge pull request #1 from arkus7/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
arkus7 authored May 7, 2022
2 parents a13ca3b + d797637 commit 3ac0cee
Show file tree
Hide file tree
Showing 34 changed files with 2,923 additions and 2,207 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/migrate-bb-to-gh.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 63 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ edition = "2021"

[dependencies]
anyhow = "1.0.56"
async-trait = "0.1.53"
base64 = "0.13.0"
clap = { version = "3.1.8", features = ["derive"] }
dialoguer = { version = "0.10.0", features = ["fuzzy-select"] }
futures = "0.3.21"
indicatif = "0.17.0-rc.10"
lazy_static = "1.4.0"
regex = "1.5.5"
reqwest = { version = "0.11.10", features = ["json"] }
serde = { version = "1.0.136", features = ["derive"] }
Expand Down
91 changes: 91 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use async_trait::async_trait;
use reqwest::header::HeaderMap;
use reqwest::{IntoUrl, Method, RequestBuilder};
use serde::de::DeserializeOwned;
use serde::Serialize;

pub(crate) struct BasicAuth<'a>(&'a str, &'a str);
impl<'a> BasicAuth<'a> {
pub fn new(username: &'a str, password: &'a str) -> Self {
Self(username, password)
}
}

#[async_trait]
pub(crate) trait ApiClient {
fn basic_auth(&self) -> Option<BasicAuth>;
fn headers(&self) -> Option<HeaderMap>;

async fn get<T, U>(&self, url: U) -> reqwest::Result<T>
where
T: DeserializeOwned,
U: IntoUrl + Send,
{
self.request(Method::GET, url, Option::<serde_json::Value>::None)
.await
}

async fn post<T, U, B>(&self, url: U, body: Option<B>) -> reqwest::Result<T>
where
T: DeserializeOwned,
U: IntoUrl + Send,
B: Serialize + Send,
{
self.request(Method::POST, url, body).await
}

async fn put<T, U, B>(&self, url: U, body: Option<B>) -> reqwest::Result<T>
where
T: DeserializeOwned,
U: IntoUrl + Send,
B: Serialize + Send,
{
self.request(Method::PUT, url, body).await
}

async fn patch<T, U, B>(&self, url: U, body: Option<B>) -> reqwest::Result<T>
where
T: DeserializeOwned,
U: IntoUrl + Send,
B: Serialize + Send,
{
self.request(Method::PATCH, url, body).await
}

async fn request<T, U, B>(&self, method: Method, url: U, body: Option<B>) -> reqwest::Result<T>
where
T: DeserializeOwned,
U: IntoUrl + Send,
B: Serialize + Send,
{
let client = reqwest::Client::new().request(method, url);
let mut builder = self.build_common_parts(client);
if let Some(body) = body {
builder = builder.json(&body);
}

let response = builder.send().await?.error_for_status()?;

let mut body = response.text().await?;
if body.is_empty() {
body = "{}".to_string();
}

let response = serde_json::from_str(&body).unwrap();

Ok(response)
}

#[inline]
fn build_common_parts(&self, builder: RequestBuilder) -> RequestBuilder {
let mut builder = builder;
if let Some(headers) = self.headers() {
builder = builder.headers(headers);
}
if let Some(basic) = self.basic_auth() {
builder = builder.basic_auth(basic.0, Some(basic.1));
}

builder
}
}
Loading

0 comments on commit 3ac0cee

Please sign in to comment.