use super::{Body, Cursor, Direction, IntoRequest, Limit, Order, Records};
use error::Result;
use http::{Request, Uri};
use resources::{Account, Datum, Effect, Offer, Operation, Trade, Transaction};
use std::str::FromStr;
use uri::{self, TryFromUri, UriWrap};
#[derive(Debug)]
pub struct Details {
account_id: String,
}
impl Details {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
}
}
}
impl IntoRequest for Details {
type Response = Account;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let uri = Uri::from_str(&format!("{}/accounts/{}", host, self.account_id))?;
let request = Request::get(uri).body(Body::None)?;
Ok(request)
}
}
#[derive(Debug)]
pub struct Data {
account_id: String,
key: String,
}
impl Data {
pub fn new(account_id: &str, key: &str) -> Self {
Self {
account_id: account_id.to_string(),
key: key.to_string(),
}
}
}
impl IntoRequest for Data {
type Response = Datum;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let uri = Uri::from_str(&format!(
"{}/accounts/{}/data/{}",
host, self.account_id, self.key
))?;
let request = Request::get(uri).body(Body::None)?;
Ok(request)
}
}
#[cfg(test)]
mod account_tests {
use super::*;
#[test]
fn it_can_make_an_account_uri() {
let details = Details::new("abc123");
let request = details
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123");
}
#[test]
fn it_can_make_an_account_data_uri() {
let data = Data::new("abc123", "key");
let request = data
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/data/key");
}
}
#[derive(Debug, Clone)]
pub struct Trades {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Trades);
impl_limit!(Trades);
impl_order!(Trades);
impl Trades {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Trades {
type Response = Records<Trade>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/trades", host, self.account_id);
if self.has_query() {
uri.push_str("?");
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
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 Trades {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "trades"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod trades_tests {
use super::*;
#[test]
fn it_leaves_off_the_params_if_not_specified() {
let trades = Trades::new("abc123");
let req = trades
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/trades");
assert_eq!(req.uri().query(), None);
}
#[test]
fn it_can_make_a_trades_uri() {
let trades = Trades::new("abc123");
let request = trades
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/trades");
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Trades::new("abc123")
.with_cursor("CURSOR")
.with_order(Direction::Desc)
.with_limit(123);
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/trades");
assert_eq!(
req.uri().query(),
Some("cursor=CURSOR&order=desc&limit=123")
);
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/trades?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Trades::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}
#[derive(Debug, Clone)]
pub struct Transactions {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Transactions);
impl_limit!(Transactions);
impl_order!(Transactions);
impl Transactions {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Transactions {
type Response = Records<Transaction>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/transactions", host, self.account_id);
if self.has_query() {
uri.push_str("?");
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
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 Transactions {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "transactions"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod transactions_tests {
use super::*;
#[test]
fn it_leaves_off_the_params_if_not_specified() {
let transactions = Transactions::new("abc123");
let req = transactions
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/transactions");
assert_eq!(req.uri().query(), None);
}
#[test]
fn it_can_make_a_transactions_uri() {
let transactions = Transactions::new("abc123");
let request = transactions
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/transactions");
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Transactions::new("abc123")
.with_cursor("CURSOR")
.with_order(Direction::Desc)
.with_limit(123);
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/transactions");
assert_eq!(
req.uri().query(),
Some("cursor=CURSOR&order=desc&limit=123")
);
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/transactions?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Transactions::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}
#[derive(Debug, Clone)]
pub struct Effects {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Effects);
impl_limit!(Effects);
impl_order!(Effects);
impl Effects {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Effects {
type Response = Records<Effect>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/effects", host, self.account_id);
if self.has_query() {
uri.push_str("?");
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
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 Effects {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "effects"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod effects_tests {
use super::*;
#[test]
fn it_leaves_off_the_params_if_not_specified() {
let effects = Effects::new("abc123");
let req = effects
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/effects");
assert_eq!(req.uri().query(), None);
}
#[test]
fn it_can_make_a_actions_uri() {
let effects = Effects::new("abc123");
let request = effects
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/effects");
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Effects::new("abc123")
.with_cursor("CURSOR")
.with_order(Direction::Asc)
.with_limit(123);
let req = ep
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/effects");
assert_eq!(req.uri().query(), Some("cursor=CURSOR&order=asc&limit=123"));
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/effects?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Effects::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}
#[derive(Debug, Clone)]
pub struct Operations {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Operations);
impl_limit!(Operations);
impl_order!(Operations);
impl Operations {
pub fn new(account_id: &str) -> Operations {
Operations {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Operations {
type Response = Records<Operation>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/operations", host, self.account_id);
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 Operations {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "operations"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod ledger_operations_tests {
use super::*;
#[test]
fn it_leaves_off_the_params_if_not_specified() {
let ep = Operations::new("abc123");
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/operations");
assert_eq!(req.uri().query(), None);
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Operations::new("abc123")
.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(), "/accounts/abc123/operations");
assert_eq!(
req.uri().query(),
Some("order=desc&cursor=CURSOR&limit=123")
);
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/operations?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Operations::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}
#[derive(Debug, Clone)]
pub struct Payments {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Payments);
impl_limit!(Payments);
impl_order!(Payments);
impl Payments {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Payments {
type Response = Records<Operation>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/payments", host, self.account_id);
if self.has_query() {
uri.push_str("?");
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
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 Payments {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "payments"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod payments_tests {
use super::*;
#[test]
fn it_can_make_a_payments_uri() {
let payments = Payments::new("abc123");
let request = payments
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/payments");
assert_eq!(request.uri().query(), None);
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Payments::new("abc123")
.with_cursor("CURSOR")
.with_order(Direction::Desc)
.with_limit(123);
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/payments");
assert_eq!(
req.uri().query(),
Some("cursor=CURSOR&order=desc&limit=123")
);
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/payments?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Payments::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}
#[derive(Debug, Clone)]
pub struct Offers {
account_id: String,
cursor: Option<String>,
order: Option<Direction>,
limit: Option<u32>,
}
impl_cursor!(Offers);
impl_limit!(Offers);
impl_order!(Offers);
impl Offers {
pub fn new(account_id: &str) -> Self {
Self {
account_id: account_id.to_string(),
cursor: None,
order: None,
limit: None,
}
}
fn has_query(&self) -> bool {
self.order.is_some() || self.cursor.is_some() || self.limit.is_some()
}
}
impl IntoRequest for Offers {
type Response = Records<Offer>;
fn into_request(self, host: &str) -> Result<Request<Body>> {
let mut uri = format!("{}/accounts/{}/offers", host, self.account_id);
if self.has_query() {
uri.push_str("?");
if let Some(cursor) = self.cursor {
uri.push_str(&format!("cursor={}&", cursor));
}
if let Some(order) = self.order {
uri.push_str(&format!("order={}&", order.to_string()));
}
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 Offers {
fn try_from_wrap(wrap: &UriWrap) -> ::std::result::Result<Self, uri::Error> {
match wrap.path() {
["accounts", account_id, "offers"] => {
let params = wrap.params();
Ok(Self {
account_id: account_id.to_string(),
cursor: params.get_parse("cursor").ok(),
order: params.get_parse("order").ok(),
limit: params.get_parse("limit").ok(),
})
}
_ => Err(uri::Error::invalid_path()),
}
}
}
#[cfg(test)]
mod offers_tests {
use super::*;
#[test]
fn it_can_make_an_offers_uri() {
let payments = Offers::new("abc123");
let request = payments
.into_request("https://horizon-testnet.stellar.org")
.unwrap();
assert_eq!(request.uri().host().unwrap(), "horizon-testnet.stellar.org");
assert_eq!(request.uri().path(), "/accounts/abc123/offers");
assert_eq!(request.uri().query(), None);
}
#[test]
fn it_puts_the_query_params_on_the_uri() {
let ep = Offers::new("abc123")
.with_cursor("CURSOR")
.with_order(Direction::Desc)
.with_limit(123);
let req = ep.into_request("https://www.google.com").unwrap();
assert_eq!(req.uri().path(), "/accounts/abc123/offers");
assert_eq!(
req.uri().query(),
Some("cursor=CURSOR&order=desc&limit=123")
);
}
#[test]
fn it_parses_from_a_uri() {
let uri: Uri = "/accounts/abc123/offers?cursor=CURSOR&order=desc&limit=123"
.parse()
.unwrap();
let ep = Offers::try_from(&uri).unwrap();
assert_eq!(ep.account_id, "abc123");
assert_eq!(ep.limit, Some(123));
assert_eq!(ep.cursor, Some("CURSOR".to_string()));
assert_eq!(ep.order, Some(Direction::Desc));
}
}