Skip to content

Commit

Permalink
feat(api): GET /rates
Browse files Browse the repository at this point in the history
  • Loading branch information
emschwartz committed Sep 12, 2019
1 parent 554467c commit 853d245
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
10 changes: 9 additions & 1 deletion crates/interledger-api/src/routes/settings.rs
@@ -1,6 +1,6 @@
use crate::{NodeStore, BEARER_TOKEN_START};
use futures::{
future::{err, ok},
future::{err, ok, result},
Future,
};
use hyper::Response;
Expand All @@ -25,6 +25,7 @@ struct ServerStatus {
struct Success;

#[derive(Extract, Debug, Response)]
// TODO should this use stringified floats instead of JSON numbers?
struct Rates(HashMap<String, f64>);

#[derive(Extract, Response, Debug)]
Expand Down Expand Up @@ -81,6 +82,13 @@ impl_web! {
})
}

#[get("/rates")]
#[content_type("application/json")]
// TODO should this be admin only?
fn get_rates(&self) -> impl Future<Item = Rates, Error = Response<()>> {
result(self.store.get_all_exchange_rates().map(|rates| Rates(rates)).map_err(|_| Response::builder().status(500).body(()).unwrap()))
}

#[get("/routes")]
#[content_type("application/json")]
fn get_routes(&self) -> impl Future<Item = Routes, Error = Response<()>> {
Expand Down
15 changes: 14 additions & 1 deletion crates/interledger-service-util/src/exchange_rates_service.rs
Expand Up @@ -8,6 +8,7 @@ use log::{debug, error, trace, warn};
use reqwest::{r#async::Client, Url};
use serde::Deserialize;
use std::{
collections::HashMap,
iter::{once, IntoIterator},
marker::PhantomData,
str::FromStr,
Expand All @@ -31,6 +32,14 @@ pub trait ExchangeRateStore: Clone {
) -> Box<dyn Future<Item = (), Error = ()> + Send>;

fn get_exchange_rates(&self, asset_codes: &[&str]) -> Result<Vec<f64>, ()>;

// TODO should this be on the API instead? That's where it's actually used
// TODO should we combine this method with get_exchange_rates?
// The downside of doing that is in this case we want a HashMap with owned values
// (so that we don't accidentally lock up the RwLock on the store's exchange_rates)
// but in the normal case of getting the rate between two assets, we don't want to
// copy all the rate data
fn get_all_exchange_rates(&self) -> Result<HashMap<String, f64>, ()>;
}

/// # Exchange Rates Service
Expand Down Expand Up @@ -436,7 +445,11 @@ mod tests {
&self,
rates: impl IntoIterator<Item = (String, f64)>,
) -> Box<Future<Item = (), Error = ()> + Send> {
Box::new(ok(()))
unimplemented!()
}

fn get_all_exchange_rates(&self) -> Result<HashMap<String, f64>, ()> {
unimplemented!()
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/interledger-store-redis/src/store.rs
Expand Up @@ -780,6 +780,10 @@ impl ExchangeRateStore for RedisStore {
}
}

fn get_all_exchange_rates(&self) -> Result<HashMap<String, f64>, ()> {
Ok((*self.exchange_rates.read()).clone())
}

fn set_exchange_rates(
&self,
rates: impl IntoIterator<Item = (String, f64)>,
Expand Down
21 changes: 18 additions & 3 deletions docs/api.md
Expand Up @@ -134,7 +134,22 @@ Admin only.

Sets the exchange rates for the node.

### Request
#### Request

```json
{
"ABC": 1.0,
"XYZ": 2.517
}
```

### GET /rates

This is currently an open endpoint but it may become admin- and user-only in the future.

Get all of the node's exchange rates.

#### Response

```json
{
Expand All @@ -149,7 +164,7 @@ Admin only.

Configure static routes for the node. These will override routes received by CCP broadcast from other nodes.

### Request
#### Request

```json
{
Expand All @@ -164,7 +179,7 @@ Admin only.

Configure a single route.

### Request
#### Request

```
"4"
Expand Down

0 comments on commit 853d245

Please sign in to comment.