taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

client.rs (3149B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use std::borrow::Cow;
     18 
     19 use reqwest::Method;
     20 use serde_json::json;
     21 use taler_common::types::amount::Decimal;
     22 
     23 use crate::cyclos_api::{
     24     api::{ApiResult, CyclosAuth, CyclosRequest},
     25     types::{Account, DataForTransaction, HistoryItem, Transaction, Transfer, User},
     26 };
     27 
     28 pub struct Client<'a> {
     29     pub client: &'a reqwest::Client,
     30     pub api_url: &'a reqwest::Url,
     31     pub auth: &'a CyclosAuth,
     32 }
     33 
     34 impl Client<'_> {
     35     fn request(&self, method: Method, path: impl Into<Cow<'static, str>>) -> CyclosRequest<'_> {
     36         CyclosRequest::new(self.client, method, self.api_url, path, self.auth)
     37     }
     38 
     39     pub async fn whoami(&self) -> ApiResult<User> {
     40         self.request(Method::GET, "users/self")
     41             .parse_json()
     42             .await
     43     }
     44 
     45     pub async fn accounts(&self) -> ApiResult<Vec<Account>> {
     46         self.request(Method::GET, "self/accounts")
     47             .parse_json()
     48             .await
     49     }
     50 
     51     pub async fn balance(&self, account_type_id: u64) -> ApiResult<Account> {
     52         self.request(Method::GET, format!("self/accounts/{account_type_id}"))
     53             .parse_json()
     54             .await
     55     }
     56 
     57     pub async fn payment_data(&self) -> ApiResult<DataForTransaction> {
     58         self.request(Method::GET, "self/payments/data-for-perform")
     59             .parse_json()
     60             .await
     61     }
     62 
     63     pub async fn direct_payment(
     64         &self,
     65         account_id: u64,
     66         amount: Decimal,
     67         description: &str,
     68     ) -> ApiResult<Transaction> {
     69         self.request(Method::POST, "self/payments")
     70             .json(&json!({
     71                 "description": description,
     72                 "scheduling": "direct",
     73                 "subject": account_id,
     74                 "amount": amount
     75             }))
     76             .parse_json()
     77             .await
     78     }
     79 
     80     pub async fn chargeback(&self, transfer_id: u64) -> ApiResult<u64> {
     81         self.request(Method::POST, format!("transfers/{transfer_id}/chargeback"))
     82             .parse_json()
     83             .await
     84     }
     85 
     86     pub async fn transfers(&self, account_type_id: u64) -> ApiResult<Vec<HistoryItem>> {
     87         self.request(
     88             Method::GET,
     89             format!("self/accounts/{account_type_id}/history"),
     90         )
     91         .parse_json()
     92         .await
     93     }
     94 
     95     pub async fn transfer(&self, transfer_id: u64) -> ApiResult<Transfer> {
     96         self.request(Method::GET, format!("transfers/{transfer_id}"))
     97             .parse_json()
     98             .await
     99     }
    100 }