Skip to content

Commit

Permalink
[Gateway Client]: Add a gateway client.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jan 25, 2024
1 parent 1e0c101 commit 89b00b0
Show file tree
Hide file tree
Showing 215 changed files with 7,954 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"packages/caviarnine-adapter-v1",
# Libraries
"libraries/package-loader",
"libraries/gateway-client",
"libraries/scrypto-interface",
"libraries/adapters-interface",
# Tools
Expand Down
3 changes: 3 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ install_crate = "rustfmt"
command = "cargo"
args = ["fmt"]

[tasks.fmt]
alias = "format"

[tasks.publish-stokenet]
command = "cargo"
args = [
Expand Down
13 changes: 13 additions & 0 deletions libraries/gateway-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "gateway-client"
description = "An auto-generated gateway client used by the various Ignition tools."
version.workspace = true
edition.workspace = true

[dependencies]
serde = { version = "1.0.195", features = ["derive"] }
serde_with = { version = "3.5.1" }
serde_json = { version = "1.0.111" }
url = { version = "2.5.0" }
uuid = { version = "1.7.0", features = ["serde", "v4"] }
reqwest = { version = "0.11.23", features = ["json", "blocking", "multipart"] }
38 changes: 38 additions & 0 deletions libraries/gateway-client/src/apis/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[derive(Debug, Clone)]
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::blocking::Client,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub bearer_access_token: Option<String>,
pub api_key: Option<ApiKey>,
}

pub type BasicAuth = (String, Option<String>);

#[derive(Debug, Clone)]
pub struct ApiKey {
pub prefix: Option<String>,
pub key: String,
}

impl Configuration {
pub fn new() -> Configuration {
Configuration::default()
}
}

impl Default for Configuration {
fn default() -> Self {
Configuration {
base_path: "http://localhost".to_owned(),
user_agent: Some("OpenAPI-Generator/v1.2.2/rust".to_owned()),
client: reqwest::blocking::Client::new(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,
}
}
}
109 changes: 109 additions & 0 deletions libraries/gateway-client/src/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::error;
use std::fmt;

#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub status: reqwest::StatusCode,
pub content: String,
pub entity: Option<T>,
}

#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
}

impl<T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => {
("response", format!("status code {}", e.status))
}
};
write!(f, "error in {}: {}", module, e)
}
}

impl<T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
})
}
}

impl<T> From<reqwest::Error> for Error<T> {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}

impl<T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
}
}

impl<T> From<std::io::Error> for Error<T> {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}

pub fn urlencode<T: AsRef<str>>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}

pub fn parse_deep_object(
prefix: &str,
value: &serde_json::Value,
) -> Vec<(String, String)> {
if let serde_json::Value::Object(object) = value {
let mut params = vec![];

for (key, value) in object {
match value {
serde_json::Value::Object(_) => {
params.append(&mut parse_deep_object(
&format!("{}[{}]", prefix, key),
value,
))
}
serde_json::Value::Array(array) => {
for (i, value) in array.iter().enumerate() {
params.append(&mut parse_deep_object(
&format!("{}[{}][{}]", prefix, key, i),
value,
));
}
}
serde_json::Value::String(s) => {
params.push((format!("{}[{}]", prefix, key), s.clone()))
}
_ => params
.push((format!("{}[{}]", prefix, key), value.to_string())),
}
}

return params;
}

unimplemented!("Only objects are supported with style=deepObject")
}

pub mod state_api;
pub mod statistics_api;
pub mod status_api;
pub mod stream_api;
pub mod transaction_api;

pub mod configuration;
Loading

0 comments on commit 89b00b0

Please sign in to comment.