-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c3d7f7
commit 57d8c7c
Showing
7 changed files
with
296 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use serde::Deserialize; | ||
|
||
use crate::models::Response; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Link { | ||
#[serde(rename = "self")] | ||
self_link: SelfLink, | ||
next: Option<SelfLink>, | ||
prev: Option<SelfLink>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct SelfLink { | ||
href: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Records { | ||
_links: Link, | ||
id: String, | ||
paging_token: String, | ||
hash: String, | ||
prev_hash: String, | ||
sequence: i32, | ||
successful_transaction_count: i32, | ||
failed_transaction_count: i32, | ||
operation_count: i32, | ||
tx_set_operation_count: i32, | ||
closed_at: String, | ||
total_coins: String, | ||
fee_pool: String, | ||
base_fee_in_stroops: i32, | ||
base_reserve_in_stroops: i32, | ||
max_tx_set_size: i32, | ||
protocol_version: i32, | ||
header_xdr: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Embedded { | ||
records: Vec<Records>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct LedgersResponse { | ||
_links: Link, | ||
_embedded: Embedded, | ||
} | ||
|
||
impl Response for LedgersResponse { | ||
fn from_json(json: String) -> Result<Self, String> { | ||
// serde_json::from_str(&json).map_err(|e| e.to_string()) | ||
|
||
let result = serde_json::from_str(&json).map_err(|e| e.to_string()); | ||
|
||
match result { | ||
Ok(response) => { | ||
println!("\n FROM JSON RESULT: {:?}", response); | ||
Ok(response) | ||
}, | ||
Err(e) => { | ||
println!("\n FROM JSON ERROR: {:?}", e); | ||
Err(e.to_string()) | ||
}, | ||
} | ||
|
||
} | ||
} | ||
|
||
impl LedgersResponse { | ||
pub fn get_id(&self) -> String { | ||
self._embedded.records[0].id.clone() | ||
} | ||
} |
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,7 +1,11 @@ | ||
mod ledgers_request; | ||
mod ledgers_response; | ||
mod single_ledger_request; | ||
mod single_ledger_response; | ||
|
||
pub mod prelude { | ||
pub use super::ledgers_request::*; | ||
pub use super::ledgers_response::*; | ||
pub use super::single_ledger_request::*; | ||
pub use super::single_ledger_response::*; | ||
} |
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,64 @@ | ||
use crate::models::*; | ||
|
||
use super::super::AssetType; | ||
use super::super::Order; | ||
|
||
pub struct SingleLedgerRequest { | ||
/// The sequence of the ledger | ||
sequence: u32, | ||
} | ||
|
||
impl Request for SingleLedgerRequest { | ||
fn new() -> Self { | ||
Self { sequence: 0 } | ||
} | ||
|
||
fn get_path(&self) -> &str { | ||
"/ledgers" | ||
} | ||
|
||
fn get_query_parameters(&self) -> String { | ||
format!("{}", self.sequence) | ||
} | ||
|
||
fn validate(&self) -> Result<(), String> { | ||
if self.sequence < 1 { | ||
return Err("sequence must be greater than or equal to 1".to_string()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn build_url(&self, base_url: &str) -> String { | ||
format!( | ||
"{}{}/{}", | ||
base_url, | ||
self.get_path(), | ||
self.get_query_parameters() | ||
) | ||
} | ||
} | ||
|
||
impl SingleLedgerRequest { | ||
/// Sets the sequence | ||
/// # Arguments | ||
/// * `sequence` - The sequence | ||
/// # Returns | ||
/// The request object | ||
/// [SingleLedgerRequest](struct.SingleLedgerRequest.html) | ||
pub fn set_sequence(&mut self, sequence: u32) { | ||
self.sequence = sequence; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_ledgers_request() { | ||
let request = SingleLedgerRequest::new(); | ||
|
||
assert_eq!(request.get_path(), "/ledgers"); | ||
} | ||
} |
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,66 @@ | ||
use serde::{Deserialize, de}; | ||
use stellar_xdr::{ReadXdr, LedgerHeader}; | ||
|
||
use crate::models::Response; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct SingleLedgerResponse { | ||
_links: Links, | ||
id: String, | ||
paging_token: String, | ||
hash: String, | ||
prev_hash: String, | ||
sequence: i32, | ||
successful_transaction_count: i32, | ||
failed_transaction_count: i32, | ||
operation_count: i32, | ||
tx_set_operation_count: i32, | ||
closed_at: String, | ||
total_coins: String, | ||
fee_pool: String, | ||
base_fee_in_stroops: i32, | ||
base_reserve_in_stroops: i64, | ||
max_tx_set_size: i32, | ||
protocol_version: i32, | ||
header_xdr: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Links { | ||
#[serde(rename = "self")] | ||
self_: Link, | ||
transactions: Link, | ||
operations: Link, | ||
payments: Link, | ||
effects: Link, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Link { | ||
href: String, | ||
templated: Option<bool>, | ||
} | ||
|
||
impl Response for SingleLedgerResponse { | ||
fn from_json(json: String) -> Result<Self, String> { | ||
let result = serde_json::from_str(&json).map_err(|e| e.to_string()); | ||
|
||
match result { | ||
Ok(response) => { | ||
Ok(response) | ||
}, | ||
Err(e) => { | ||
Err(e.to_string()) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl SingleLedgerResponse { | ||
|
||
pub fn get_decoded_header_xdr(&self) -> Result<LedgerHeader, String> { | ||
let encoded = self.header_xdr.as_bytes(); | ||
let decoded = stellar_xdr::LedgerHeader::from_xdr_base64(encoded).unwrap(); | ||
Ok(decoded) | ||
} | ||
} |
Oops, something went wrong.