Skip to content

Commit

Permalink
clearancce
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardTibben committed Oct 6, 2023
1 parent 19242c9 commit e2c35bb
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}
8 changes: 1 addition & 7 deletions src/accounts/accounts_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ pub struct AccountsResponse {

impl Response for AccountsResponse {
fn from_json(json: String) -> Result<Self, String> {
let response = serde_json::from_str(&json);

// println!("\n\nRESPONSE {:?}", response);
match response {
Ok(response) => Ok(response),
Err(e) => Err(e.to_string()),
}
serde_json::from_str(&json).map_err(|e| e.to_string())
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/accounts/single_account_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ pub struct SingleAccountsResponse {

impl Response for SingleAccountsResponse {
fn from_json(json: String) -> Result<Self, String> {
match serde_json::from_str(&json) {
Ok(response) => {
println!("FROM JSON: {:?}", response);
Ok(response)
},
Err(e) => {
println!("FROM JSON ERROR {:?}", e);
Err(e.to_string())
}
}

serde_json::from_str(&json).map_err(|e| e.to_string())
}
}
11 changes: 1 addition & 10 deletions src/assets/all_assets_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ pub struct AllAssetsResponse {

impl Response for AllAssetsResponse {
fn from_json(json: String) -> Result<Self, String> {
match serde_json::from_str(&json) {
Ok(response) => {
println!("FROM JSON: {:?}", response);
Ok(response)
},
Err(e) => {
println!("FROM JSON ERROR {:?}", e);
Err(e.to_string())
}
}
serde_json::from_str(&json).map_err(|e| e.to_string())
}
}
2 changes: 1 addition & 1 deletion src/horizon_client/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod tests {
.set_limit(10);

// call the get_account_list method to retrieve the account list response
let _accounts_response = horizon_client.get_account_list(&accounts_request).await;
let _accounts_response: Result<AccountsResponse, String> = horizon_client.get_account_list(&accounts_request).await;

assert!(_accounts_response.is_ok());
}
Expand Down
103 changes: 103 additions & 0 deletions src/ledgers/ledgers_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use crate::models::*;

use super::super::Order;
use super::super::AssetType;

Check warning on line 4 in src/ledgers/ledgers_request.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

unused import: `super::super::AssetType`

pub struct LedgersRequest {

/// The cursor for the page
cursor: Option<u32>,

/// The maximum number of records to return
limit: Option<u32>,

/// The order of the records
order: Option<Order>,
}

impl Request for LedgersRequest {
fn new() -> Self {
Self {
cursor: None,
limit: None,
order: None,
}
}

fn get_path(&self) -> &str {
"/ledgers"
}

fn get_query_parameters(&self) -> String {
let mut query_parameters = vec![];

if let Some(cursor) = &self.cursor {
query_parameters.push(format!("cursor={}", cursor));
}

if let Some(limit) = &self.limit {
query_parameters.push(format!("limit={}", limit));
}

if let Some(order) = &self.order {
query_parameters.push(format!("order={}", order));
}

query_parameters.join("&")
}

fn validate(&self) -> Result<(), String> {
if let Some(cursor) = &self.cursor {
if *cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}
}

if let Some(limit) = &self.limit {
if *limit < 1 {
return Err("limit must be greater than or equal to 1".to_string());
}
if *limit > 200 {
return Err("limit must be less than or equal to 200".to_string());
}
}

Ok(())
}

fn build_url(&self, base_url: &str) -> String {

Check warning on line 68 in src/ledgers/ledgers_request.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

unused variable: `base_url`
todo!()
}
}

impl LedgersRequest {
/// Sets the cursor for the page
pub fn cursor(mut self, cursor: u32) -> Self {
self.cursor = Some(cursor);
self
}

/// Sets the maximum number of records to return
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}

/// Sets the order of the records
pub fn order(mut self, order: Order) -> Self {
self.order = Some(order);
self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ledgers_request() {
let request = LedgersRequest::new();

assert_eq!(request.get_path(), "/ledgers");
}
}
Empty file added src/ledgers/ledgers_response.rs
Empty file.
7 changes: 7 additions & 0 deletions src/ledgers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod ledgers_request;
mod ledgers_response;

pub mod prelude {
pub use super::ledgers_request::*;
pub use super::ledgers_response::*;
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod assets;
mod accounts;
mod ledgers;
mod horizon_client;
mod models;
mod xdr;
Expand Down

0 comments on commit e2c35bb

Please sign in to comment.