use super::{Body, Cursor, Direction, IntoRequest, Limit, Order, Records};
use error::Result;
use http::{Request, Uri};
use resources::{Amount, AssetIdentifier, Operation, PaymentPath};
use std::str::FromStr;
use uri::{self, TryFromUri, UriWrap};
pub use super::account::Payments as ForAccount;
pub use super::ledger::Payments as ForLedger;
pub use super::transaction::Payments as ForTransaction;
#[derive(Debug, Default, Clone)]
pub struct All {
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(All);
impl_limit!(All);
impl_order!(All);
impl All {
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for All {
type Response = Records<Operation>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/payments", host);
if self.has_query() {
uri.push_str("?");
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(limit) = self.limit {
uri.push_str(&format!("limit={}", limit));
}
}
let uri = Uri::from_str(&uri)?;
let request = Request::get(uri).body(Body::None)?;
Ok(request)
}
}
impl TryFromUri for All {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<All, uri::Error> {
let params = wrap.params();
Ok(All {
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
}
#[cfg(test)]
mod all_payments_tests {
use super::*;
#[test]
fn it_leaves_off_the_params_if_not_specified() {
let ep = All::default();
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/payments");
assert_eq!(req.uri().query(), None);
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = All::default()
.with_cursor("CURSOR")
.with_limit(123)
.with_order(Direction::Desc);
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/payments");
assert_eq!(
req.uri().query(),
Some("order=desc&cursor=CURSOR&limit=123")
);
}
#[test]
fn it_parses_query_params_from_uri() {
let uri: Uri = "/payments?order=desc&cursor=CURSOR&limit=123"
.parse()
.unwrap();
let all = All::try_from(&uri).unwrap();
assert_eq!(all.order, Some(Direction::Desc));
assert_eq!(all.cursor, Some("CURSOR".to_string()));
assert_eq!(all.limit, Some(123));
}
}
#[derive(Debug, Clone)]
pub struct FindPath {
source_account: String,
destination_account: String,
destination_asset: AssetIdentifier,
destination_amount: Amount,
}
impl FindPath {
pub fn new(
source_account: &str,
destination_account: &str,
destination_asset: AssetIdentifier,
destination_amount: Amount,
) -> Self {
Self {
source_account: source_account.to_string(),
destination_account: destination_account.to_string(),
destination_asset,
destination_amount,
}
}
}
impl IntoRequest for FindPath {
type Response = Records<PaymentPath>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!(
"{}/paths?source_account={}&destination_account={}&\
destination_amount={}&destination_asset_type={}",
host,
self.source_account,
self.destination_account,
self.destination_amount,
self.destination_asset.asset_type()
);
if !self.destination_asset.is_native() {
uri.push_str(&format!(
"&destination_asset_code={}",
self.destination_asset.asset_code().unwrap()
));
uri.push_str(&format!(
"&destination_asset_issuer={}",
self.destination_asset.issuer().to_string()
));
}
let uri = Uri::from_str(&uri)?;
let request = Request::get(uri).body(Body::None)?;
Ok(request)
}
}
impl TryFromUri for FindPath {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<FindPath, uri::Error> {
let params = wrap.params();
Ok(FindPath {
source_account: params.get_parse("from")?,
destination_account: params.get_parse("to")?,
destination_asset: params.get_parse("asset")?,
destination_amount: params.get_parse("amount")?,
})
}
}
#[cfg(test)]
mod find_path_tests {
use super::*;
#[test]
fn it_can_make_a_paths_uri_for_native_assets() {
let paths = FindPath::new(
"account_a",
"account_b",
AssetIdentifier::new("native", None, None).unwrap(),
Amount::new(1000),
);
let request = paths
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/paths");
assert_eq!(
request.uri().query(),
Some(
"source_account=account_a&destination_account=account_b&\
destination_amount=0.0001000&destination_asset_type=native"
)
);
}
#[test]
fn it_can_make_a_paths_uri_for_non_native_assets() {
let paths = FindPath::new(
"account_a",
"account_b",
AssetIdentifier::new(
"credit_alphanum4",
Some("codx".to_string()),
Some("me".to_string()),
).unwrap(),
Amount::new(1000),
);
let request = paths
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/paths");
assert_eq!(
request.uri().query(),
Some(
"source_account=account_a&destination_account=account_b&\
destination_amount=0.0001000&destination_asset_type=credit_alphanum4&\
destination_asset_code=codx&destination_asset_issuer=me"
)
);
}
}