-
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.
Merge pull request #91 from cspr-rad/implement-deposit-cli-3
kairos-cli: implement call to deposit endpoint and integration test
- Loading branch information
Showing
11 changed files
with
164 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use kairos_server::routes::PayloadBody; | ||
|
||
use reqwest::{blocking, Url}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt; | ||
|
||
#[derive(PartialOrd, Ord, PartialEq, Eq, Debug, Serialize, Deserialize)] | ||
pub enum KairosClientError { | ||
ResponseError(String), | ||
ResponseErrorWithCode(u16, String), | ||
DecodeError(String), | ||
KairosServerError(String), | ||
} | ||
|
||
impl std::error::Error for KairosClientError {} | ||
|
||
impl fmt::Display for KairosClientError { | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let json_string = serde_json::to_string(self).map_err(|_| fmt::Error)?; | ||
write!(formatter, "{}", json_string) | ||
} | ||
} | ||
impl From<reqwest::Error> for KairosClientError { | ||
fn from(error: reqwest::Error) -> Self { | ||
let error_without_url = error.without_url(); | ||
if error_without_url.is_decode() { | ||
KairosClientError::DecodeError(error_without_url.to_string()) | ||
} else { | ||
match error_without_url.status() { | ||
Option::None => Self::ResponseError(error_without_url.to_string()), | ||
Option::Some(status_code) => { | ||
Self::ResponseErrorWithCode(status_code.as_u16(), error_without_url.to_string()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn submit_transaction_request( | ||
base_url: &Url, | ||
deposit_request: &PayloadBody, | ||
) -> Result<(), KairosClientError> { | ||
let client = blocking::Client::new(); | ||
let url = base_url.join("/api/v1/deposit").unwrap(); | ||
let response = client | ||
.post(url) | ||
.header("Content-Type", "application/json") | ||
.json(deposit_request) | ||
.send() | ||
.map_err(Into::<KairosClientError>::into)?; | ||
let status = response.status(); | ||
if !status.is_success() { | ||
Err(KairosClientError::KairosServerError(status.to_string())) | ||
} else { | ||
Ok(()) | ||
} | ||
} |
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,15 +1,3 @@ | ||
pub mod deposit; | ||
pub mod transfer; | ||
pub mod withdraw; | ||
|
||
use clap::Subcommand; | ||
|
||
#[derive(Subcommand)] | ||
pub enum Command { | ||
#[command(about = "Deposits funds into your account")] | ||
Deposit(deposit::Args), | ||
#[command(about = "Transfers funds to another account")] | ||
Transfer(transfer::Args), | ||
#[command(about = "Withdraws funds from your account")] | ||
Withdraw(withdraw::Args), | ||
} |
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,4 +1,42 @@ | ||
pub mod client; | ||
pub mod commands; | ||
pub mod common; | ||
pub mod error; | ||
pub mod utils; | ||
|
||
use crate::error::CliError; | ||
|
||
use clap::{Parser, Subcommand}; | ||
use reqwest::Url; | ||
|
||
#[derive(Parser)] | ||
#[command(name = "Kairos Client", about = "CLI for interacting with Kairos")] | ||
pub struct Cli { | ||
#[command(subcommand)] | ||
pub command: Command, | ||
#[arg(long, value_name = "URL", default_value = "http://0.0.0.0:9999")] | ||
pub kairos_server_address: Url, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Command { | ||
#[command(about = "Deposits funds into your account")] | ||
Deposit(commands::deposit::Args), | ||
#[command(about = "Transfers funds to another account")] | ||
Transfer(commands::transfer::Args), | ||
#[command(about = "Withdraws funds from your account")] | ||
Withdraw(commands::withdraw::Args), | ||
} | ||
|
||
pub fn run( | ||
Cli { | ||
command, | ||
kairos_server_address, | ||
}: Cli, | ||
) -> Result<String, CliError> { | ||
match command { | ||
Command::Deposit(args) => commands::deposit::run(args, kairos_server_address), | ||
Command::Transfer(args) => commands::transfer::run(args), | ||
Command::Withdraw(args) => commands::withdraw::run(args), | ||
} | ||
} |
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