1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::{Quantity, SettlementAccount};
use futures::{
    future::{err, Either},
    Future,
};
use log::{debug, error, trace};
use reqwest::r#async::Client;
use serde_json::json;
use uuid::Uuid;

#[derive(Clone)]
pub struct SettlementClient {
    http_client: Client,
}

impl SettlementClient {
    pub fn new() -> Self {
        SettlementClient {
            http_client: Client::new(),
        }
    }

    pub fn send_settlement<A: SettlementAccount>(
        &self,
        account: A,
        amount: u64,
    ) -> impl Future<Item = (), Error = ()> {
        if let Some(settlement_engine) = account.settlement_engine_details() {
            let mut settlement_engine_url = settlement_engine.url;
            settlement_engine_url
                .path_segments_mut()
                .expect("Invalid settlement engine URL")
                .push("accounts")
                .push(&account.id().to_string())
                .push("settlements");
            debug!(
                "Sending settlement of amount {} to settlement engine: {}",
                amount, settlement_engine_url
            );
            let settlement_engine_url_clone = settlement_engine_url.clone();
            let idempotency_uuid = Uuid::new_v4().to_hyphenated().to_string();
            return Either::A(self.http_client.post(settlement_engine_url.as_ref())
                .header("Idempotency-Key", idempotency_uuid)
                .json(&json!(Quantity::new(amount, account.asset_scale())))
                .send()
                .map_err(move |err| error!("Error sending settlement command to settlement engine {}: {:?}", settlement_engine_url, err))
                .and_then(move |response| {
                    if response.status().is_success() {
                        trace!("Sent settlement of {} to settlement engine: {}", amount, settlement_engine_url_clone);
                        Ok(())
                    } else {
                        error!("Error sending settlement. Settlement engine responded with HTTP code: {}", response.status());
                        Err(())
                    }
                }));
        }
        error!("Cannot send settlement for account {} because it does not have the settlement_engine_url and scale configured", account.id());
        Either::B(err(()))
    }
}

impl Default for SettlementClient {
    fn default() -> Self {
        SettlementClient::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixtures::TEST_ACCOUNT_0;
    use crate::test_helpers::{block_on, mock_settlement};
    use mockito::Matcher;

    #[test]
    fn settlement_ok() {
        let m = mock_settlement(200)
            .match_header("Idempotency-Key", Matcher::Any)
            .create();
        let client = SettlementClient::new();

        let ret = block_on(client.send_settlement(TEST_ACCOUNT_0.clone(), 100));

        m.assert();
        assert!(ret.is_ok());
    }

    #[test]
    fn engine_rejects() {
        let m = mock_settlement(500)
            .match_header("Idempotency-Key", Matcher::Any)
            .create();
        let client = SettlementClient::new();

        let ret = block_on(client.send_settlement(TEST_ACCOUNT_0.clone(), 100));

        m.assert();
        assert!(ret.is_err());
    }

    #[test]
    fn account_does_not_have_settlement_engine() {
        let m = mock_settlement(200)
            .expect(0)
            .match_header("Idempotency-Key", Matcher::Any)
            .create();
        let client = SettlementClient::new();

        let mut acc = TEST_ACCOUNT_0.clone();
        acc.no_details = true; // Hide the settlement engine data from the account
        let ret = block_on(client.send_settlement(acc, 100));

        m.assert();
        assert!(ret.is_err());
    }
}