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
use serde::{Deserialize, Serialize};

use crate::api::{Requests, Response};
use crate::client::TradingClient;
use crate::error::Error;
use chrono::prelude::*;

/// Struct for the Withdrawal Request
#[derive(Serialize, Deserialize, Debug)]
pub struct WithdrawalRequest {
    /// Amount to withdraw
    amount: usize,
    /// PIN to use for withdrawal
    pin: i64,
    /// You can set your own unique idempotency key to prevent duplicate operations.
    /// Subsequent requests with the same idempotency key will then not go through and throw an error message.
    /// This means you cannot make the same withdrawal twice.
    idempotency: Option<String>,
}

///
#[derive(Deserialize, Serialize, Debug)]
pub struct Withdrawal {
    /// A unique Identification Number of your withdrawal
    pub id: String,
    /// The amount of the withdrawal
    pub amount: i64,
    /// Timestamp at which you created the withdrawal
    pub created_at: DateTime<Utc>,
    /// Timestamp at which the withdrawal was processed by our partner bank
    pub date: DateTime<Utc>,
    /// Your own unique idempotency key that you specified in your POST request to prevent
    /// duplicate withdrawals.
    pub idempotency: Option<String>,
}

impl TradingClient {
    /// Get account withdrawls
    // TODO: Add support for pagination
    // TODO: Add docs for params
    pub fn get_account_withdrawls(
        &self,
        _limit: Option<i32>,
        _page: Option<i32>,
    ) -> Result<Response, Error> {
        const PATH: &str = "account/withdrawals";
        let resp = self.get::<Response>(PATH);
        match resp {
            Ok(r) => Ok(r),
            Err(e) => Err(e),
        }
    }

    /// Submit a new withdrawal
    /// TODO: Add docs for params
    pub fn post_withdrawal(&self, withdrawal: WithdrawalRequest) -> Result<Response, Error> {
        const PATH: &str = "account/withdrawals/";
        let resp = self.post::<Response, WithdrawalRequest>(PATH, withdrawal);
        match resp {
            Ok(r) => Ok(r),
            Err(e) => Err(e),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::env;

    use super::*;

    #[test]
    fn test_get_account_withdrawls() {
        dotenv::dotenv().unwrap();
        let api_key = env::var("LEMON_MARKET_TRADING_API_KEY").unwrap();
        let client = TradingClient::paper_client(&api_key);
        let resp = client.get_account_withdrawls(None, None).unwrap();
        assert_eq!(resp.status, "ok");
    }

    #[test]
    // TODO: Figure out why this often return 400 error
    fn test_post_withdrawal() {
        dotenv::dotenv().unwrap();
        let api_key = env::var("LEMON_MARKET_TRADING_API_KEY").unwrap();
        let client = TradingClient::paper_client(&api_key);
        let withdrawal = WithdrawalRequest {
            amount: 100, // 0.01 EUR
            pin: 1234,
            idempotency: None,
        };
        let resp = client.post_withdrawal(withdrawal).unwrap();
        assert_eq!(resp.status, "ok");
    }
}