Skip to content

Commit

Permalink
Refactor: replaced client with http
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jul 27, 2023
1 parent b2f76bb commit 75b37ff
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "amp-common"
description = "Rust libraries shared across Amphitheatre components and libraries"
version = "0.3.7"
version = "0.3.8"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository contains Rust libraries that are shared across Amphitheatre
components and libraries. They are considered internal to Amphitheatre, without
any stability guarantees for external usage.

- [**client**](src/client/): Represents the Rust client for the API.
- [**http**](src/http/): Represents the HTTP client for the API.
- [**config**](src/config/): Common configuration structures.
- [**docker**](src/docker/): Docker container & Registry wrappers.
- [**filesystem**](src/filesystem/): File system related structures and helpers.
Expand Down
44 changes: 22 additions & 22 deletions src/client/client.rs → src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde::de::DeserializeOwned;
use serde_json::{from_value, json, Value};
use ureq::Request;

use super::ClientError;
use super::HTTPError;

const VERSION: &str = "0.1.0";
const DEFAULT_USER_AGENT: &str = "amp";
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Client {
&self,
path: &str,
options: Option<HashMap<String, String>>,
) -> Result<Response<E::Output>, ClientError> {
) -> Result<Response<E::Output>, HTTPError> {
self.call::<E>(self.build_get_request(&path, options))
}

Expand All @@ -110,7 +110,7 @@ impl Client {
&self,
path: &str,
data: Value,
) -> Result<Response<<E as Endpoint>::Output>, ClientError> {
) -> Result<Response<<E as Endpoint>::Output>, HTTPError> {
self.call_with_payload::<E>(self.build_post_request(&path), data)
}

Expand All @@ -119,7 +119,7 @@ impl Client {
/// # Arguments
///
/// `path`: the path to the endpoint
pub fn empty_post(&self, path: &str) -> Result<Response<()>, ClientError> {
pub fn empty_post(&self, path: &str) -> Result<Response<()>, HTTPError> {
self.call_empty(self.build_post_request(&path))
}

Expand All @@ -133,7 +133,7 @@ impl Client {
&self,
path: &str,
data: Value,
) -> Result<Response<<E as Endpoint>::Output>, ClientError> {
) -> Result<Response<<E as Endpoint>::Output>, HTTPError> {
self.call_with_payload::<E>(self.build_put_request(&path), data)
}

Expand All @@ -142,7 +142,7 @@ impl Client {
/// # Arguments
///
/// `path`: the path to the endpoint
pub fn empty_put(&self, path: &str) -> Result<Response<()>, ClientError> {
pub fn empty_put(&self, path: &str) -> Result<Response<()>, HTTPError> {
self.call_empty(self.build_put_request(&path))
}

Expand All @@ -156,7 +156,7 @@ impl Client {
&self,
path: &str,
data: Value,
) -> Result<Response<<E as Endpoint>::Output>, ClientError> {
) -> Result<Response<<E as Endpoint>::Output>, HTTPError> {
self.call_with_payload::<E>(self.build_patch_request(&path), data)
}

Expand All @@ -165,7 +165,7 @@ impl Client {
/// # Arguments
///
/// `path`: the path to the endpoint
pub fn delete(&self, path: &str) -> Result<Response<()>, ClientError> {
pub fn delete(&self, path: &str) -> Result<Response<()>, HTTPError> {
self.call_empty(self.build_delete_request(&path))
}

Expand All @@ -174,54 +174,54 @@ impl Client {
/// # Arguments
///
/// `path`: the path to the endpoint
pub fn delete_with_response<E: Endpoint>(&self, path: &str) -> Result<Response<E::Output>, ClientError> {
pub fn delete_with_response<E: Endpoint>(&self, path: &str) -> Result<Response<E::Output>, HTTPError> {
self.call::<E>(self.build_delete_request(&path))
}

fn call_with_payload<E: Endpoint>(
&self,
request: Request,
data: Value,
) -> Result<Response<E::Output>, ClientError> {
) -> Result<Response<E::Output>, HTTPError> {
self.process_response::<E>(request.send_json(data))
}

fn call<E: Endpoint>(&self, request: Request) -> Result<Response<E::Output>, ClientError> {
fn call<E: Endpoint>(&self, request: Request) -> Result<Response<E::Output>, HTTPError> {
self.process_response::<E>(request.call())
}

fn process_response<E: Endpoint>(
&self,
result: Result<ureq::Response, ureq::Error>,
) -> Result<Response<E::Output>, ClientError> {
) -> Result<Response<E::Output>, HTTPError> {
match result {
Ok(response) => Self::build_response::<E>(response),
Err(ureq::Error::Status(code, response)) => Err(ClientError::parse_response(code, response)),
Err(ureq::Error::Transport(transport)) => Err(ClientError::parse_transport(transport)),
Err(ureq::Error::Status(code, response)) => Err(HTTPError::parse_response(code, response)),
Err(ureq::Error::Transport(transport)) => Err(HTTPError::parse_transport(transport)),
}
}

fn call_empty(&self, request: Request) -> Result<Response<()>, ClientError> {
fn call_empty(&self, request: Request) -> Result<Response<()>, HTTPError> {
match request.call() {
Ok(response) => Self::build_empty_response(response),
Err(ureq::Error::Status(code, response)) => Err(ClientError::parse_response(code, response)),
Err(ureq::Error::Transport(transport)) => Err(ClientError::parse_transport(transport)),
Err(ureq::Error::Status(code, response)) => Err(HTTPError::parse_response(code, response)),
Err(ureq::Error::Transport(transport)) => Err(HTTPError::parse_transport(transport)),
}
}

fn build_response<E: Endpoint>(resp: ureq::Response) -> Result<Response<E::Output>, ClientError> {
fn build_response<E: Endpoint>(resp: ureq::Response) -> Result<Response<E::Output>, HTTPError> {
let status = resp.status();

let json = resp
.into_json::<Value>()
.map_err(|e| ClientError::Deserialization(e.to_string()))?;
let data = from_value(json!(json)).map_err(|e| ClientError::Deserialization(e.to_string()))?;
let body = from_value(json!(json)).map_err(|e| ClientError::Deserialization(e.to_string()))?;
.map_err(|e| HTTPError::Deserialization(e.to_string()))?;
let data = from_value(json!(json)).map_err(|e| HTTPError::Deserialization(e.to_string()))?;
let body = from_value(json!(json)).map_err(|e| HTTPError::Deserialization(e.to_string()))?;

Ok(Response { status, data, body })
}

fn build_empty_response(res: ureq::Response) -> Result<Response<()>, ClientError> {
fn build_empty_response(res: ureq::Response) -> Result<Response<()>, HTTPError> {
Ok(Response {
status: res.status(),
data: None,
Expand Down
20 changes: 10 additions & 10 deletions src/client/errors.rs → src/http/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ureq::{Response, Transport};

/// Represents the possible errors thrown while interacting with the Amphitheatre API
#[derive(Error, Deserialize, Serialize, Debug, PartialEq, Eq)]
pub enum ClientError {
pub enum HTTPError {
#[error("Authentication failed")]
Unauthorized,

Expand Down Expand Up @@ -60,8 +60,8 @@ pub enum ClientError {
Deserialization(String),
}

impl ClientError {
pub fn parse_response(code: u16, response: Response) -> ClientError {
impl HTTPError {
pub fn parse_response(code: u16, response: Response) -> HTTPError {
match code {
400 => Self::bad_request(response),
401 => Self::Unauthorized,
Expand All @@ -77,11 +77,11 @@ impl ClientError {
}
}

pub fn parse_transport(transport: Transport) -> ClientError {
pub fn parse_transport(transport: Transport) -> HTTPError {
Self::Transport(transport.to_string(), transport.kind().to_string())
}

fn bad_request(response: Response) -> ClientError {
fn bad_request(response: Response) -> HTTPError {
match Self::response_to_json(response) {
Ok(json) => Self::BadRequest {
message: Self::message_in(&json),
Expand All @@ -91,21 +91,21 @@ impl ClientError {
}
}

fn gateway_timeout(response: Response) -> ClientError {
fn gateway_timeout(response: Response) -> HTTPError {
match Self::response_to_json(response) {
Ok(json) => Self::GatewayTimeout(Self::message_in(&json)),
Err(error) => error,
}
}

fn not_found(response: Response) -> ClientError {
fn not_found(response: Response) -> HTTPError {
match Self::response_to_json(response) {
Ok(json) => Self::NotFound(Self::message_in(&json)),
Err(error) => error,
}
}

fn precondition_required(response: Response) -> ClientError {
fn precondition_required(response: Response) -> HTTPError {
match Self::response_to_json(response) {
Ok(json) => Self::PreconditionRequired(Self::message_in(&json)),
Err(error) => error,
Expand All @@ -119,10 +119,10 @@ impl ClientError {
}
}

fn response_to_json(response: Response) -> Result<Value, ClientError> {
fn response_to_json(response: Response) -> Result<Value, HTTPError> {
match response.into_json::<Value>() {
Ok(value) => Ok(value),
Err(error) => Err(ClientError::Deserialization(error.to_string())),
Err(error) => Err(HTTPError::Deserialization(error.to_string())),
}
}
}
1 change: 0 additions & 1 deletion src/client/mod.rs → src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[allow(clippy::module_inception)]
mod client;
mod errors;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod client;
pub mod config;
pub mod docker;
pub mod filesystem;
pub mod http;
pub mod schema;
pub mod scm;
pub mod sync;
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/github/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::collections::HashMap;
use data_encoding::BASE64_MIME as BASE64;
use serde::{Deserialize, Serialize};

use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::content::{Content, ContentService};
pub struct GithubContentService {
pub client: Client,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Endpoint for GithubContentEndpoint {

#[cfg(test)]
mod test {
use crate::client::Client;
use crate::http::Client;
use crate::scm::content::ContentService;
use crate::scm::driver::github::content::GithubContentService;

Expand Down
2 changes: 1 addition & 1 deletion src/scm/driver/github/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use super::content::GithubContentService;
use super::git::GithubGitService;
use super::repo::GithubRepoService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::content::ContentService;
use crate::scm::driver::DriverTrait;
use crate::scm::git::GitService;
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/github/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};

use super::utils::convert_list_options;
use super::GithubFile;
use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::client::ListOptions;
use crate::scm::git::{Commit, GitService, Reference, Signature};
use crate::scm::utils;
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Endpoint for GithubCommitEndpoint {
#[cfg(test)]
mod test {
use super::GithubGitService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::client::ListOptions;
use crate::scm::git::GitService;

Expand Down
2 changes: 1 addition & 1 deletion src/scm/driver/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod utils;
use self::driver::GithubDriver;
use self::pr::GithubFile;
use super::Driver;
use crate::client::Client;
use crate::http::Client;

/// Returnes a new GitHub API client.
pub fn new(url: &str, token: Option<String>) -> Driver {
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/github/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use serde::{Deserialize, Serialize};

use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::repo::{Repository, RepositoryService};

pub struct GithubRepoService {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Endpoint for GithubRepoEndpoint {
#[cfg(test)]
mod test {
use super::GithubRepoService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::repo::RepositoryService;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/gitlab/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use data_encoding::BASE64_MIME as BASE64;
use serde::{Deserialize, Serialize};

use super::utils::{encode, encode_path};
use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::content::{Content, ContentService};

pub struct GitlabContentService {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Endpoint for GitlabContentEndpoint {
#[cfg(test)]
mod test {
use super::GitlabContentService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::content::ContentService;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/scm/driver/gitlab/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use super::content::GitlabContentService;
use super::git::GitlabGitService;
use super::repo::GitlabRepoService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::content::ContentService;
use crate::scm::driver::DriverTrait;
use crate::scm::git::GitService;
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/gitlab/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use serde::{Deserialize, Serialize};

use super::utils::{convert_list_options, encode};
use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::client::ListOptions;
use crate::scm::git::{Commit, GitService, Reference, Signature};
use crate::scm::utils;
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Endpoint for GitlabCommitEndpoint {

#[cfg(test)]
mod test {
use crate::client::Client;
use crate::http::Client;
use crate::scm::client::ListOptions;
use crate::scm::driver::gitlab::git::GitlabGitService;
use crate::scm::git::GitService;
Expand Down
2 changes: 1 addition & 1 deletion src/scm/driver/gitlab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use self::driver::GitlabDriver;
use super::Driver;
use crate::client::Client;
use crate::http::Client;

pub mod content;
pub mod driver;
Expand Down
4 changes: 2 additions & 2 deletions src/scm/driver/gitlab/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use serde::{Deserialize, Serialize};

use super::utils::encode;
use crate::client::{Client, Endpoint};
use crate::http::{Client, Endpoint};
use crate::scm::constants::Visibility;
use crate::scm::repo::{Repository, RepositoryService};

Expand Down Expand Up @@ -100,7 +100,7 @@ impl Endpoint for GitlabRepoEndpoint {
#[cfg(test)]
mod test {
use super::GitlabRepoService;
use crate::client::Client;
use crate::http::Client;
use crate::scm::repo::RepositoryService;

#[test]
Expand Down
Loading

0 comments on commit 75b37ff

Please sign in to comment.