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
117
118
use crate::client::SharedClient;
use anyhow::Result;
use reqwest::Method;

/// Private API - Withdraws JPY
///
/// 日本円を銀行振込で出金できます。
///
/// <https://coincheck.com/ja/documents/exchange/api#withdraws-jpy>
pub struct WithdrawsJpy {
    client: SharedClient,
}

pub mod model {
    use crate::types::*;
    use chrono::{DateTime, Utc};
    use serde::{Deserialize, Serialize};
    use serde_with::{serde_as, DisplayFromStr};

    /// 銀行口座一覧
    #[derive(Debug, Serialize, Deserialize)]
    pub struct BankAccounts {
        pub success: bool,
        pub data: Vec<BankAccount>,
    }

    /// 銀行口座情報
    #[derive(Debug, Serialize, Deserialize)]
    pub struct BankAccount {
        pub id: IdType,
        pub bank_name: String,
        pub branch_name: String,
        pub bank_account_type: String,
        pub number: String,
        pub name: String,
    }

    /// 出金履歴
    #[derive(Debug, Serialize, Deserialize)]
    pub struct Withdraws {
        pub success: bool,
        pub pagination: Pagination,
        pub data: Vec<Withdraw>,
    }

    /// 出金情報
    #[serde_as]
    #[derive(Debug, Serialize, Deserialize)]
    pub struct Withdraw {
        pub id: IdType,
        pub status: String,
        #[serde_as(as = "DisplayFromStr")]
        pub amount: PriceType,
        pub currency: String,
        pub created_at: DateTime<Utc>,
        pub bank_account_id: IdType,
        #[serde_as(as = "DisplayFromStr")]
        pub fee: PriceType,
        pub is_fast: bool,
    }
}

impl WithdrawsJpy {
    pub fn new(client: SharedClient) -> Self {
        Self { client }
    }

    const USE_AUTH: bool = true;

    /// 銀行口座一覧
    ///
    /// お客様の出金用に登録された銀行口座の一覧を返します。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#bank-accounts>
    pub async fn bank_accounts(&mut self) -> Result<model::BankAccounts> {
        self.client
            .borrow_mut()
            .request_and_get_json(Method::GET, "/api/bank_accounts", None, Self::USE_AUTH)
            .await
    }

    // TODO: implement 銀行口座の登録 POST /api/bank_accounts
    // https://coincheck.com/ja/documents/exchange/api#bank-accounts-create
    // TODO: implement 銀行口座の削除 DELETE /api/bank_accounts/[id]
    // https://coincheck.com/ja/documents/exchange/api#bank-accounts-destroy

    /// 出金履歴
    ///
    /// 日本円出金の申請の履歴を表示します。
    ///
    /// <https://coincheck.com/ja/documents/exchange/api#withdraws>
    pub async fn withdraws(&mut self) -> Result<model::Withdraws> {
        self.client
            .borrow_mut()
            .request_and_get_json(Method::GET, "/api/withdraws", None, Self::USE_AUTH)
            .await
    }

    // TODO: implement 出金申請の作成 POST /api/withdraws
    // https://coincheck.com/ja/documents/exchange/api#withdraws-create
    // TODO: implement 出金申請のキャンセル DELETE /api/withdraws/[id]
    // https://coincheck.com/ja/documents/exchange/api#withdraws-destroy
}

#[cfg(test)]
mod tests {
    use crate::Coincheck;

    #[tokio::test]
    #[serial_test::serial]
    async fn private_withdraw_jpy_api() {
        let mut coincheck = Coincheck::new_with_env_keys();
        let api = &mut coincheck.private.withdraws_jpy;

        assert!(api.bank_accounts().await.is_ok());
        assert!(api.withdraws().await.is_ok());
    }
}