taler-rust

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

revenue.rs (2497B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025, 2026 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::sync::Arc;
     18 
     19 use axum::{
     20     Json, Router,
     21     extract::{Query, State},
     22     http::StatusCode,
     23     response::IntoResponse,
     24     routing::get,
     25 };
     26 use taler_common::{
     27     api_params::{History, HistoryParams},
     28     api_revenue::{RevenueConfig, RevenueIncomingHistory},
     29 };
     30 
     31 use super::TalerApi;
     32 use crate::{
     33     api::RouterUtils as _,
     34     auth::AuthMethod,
     35     constants::{MAX_PAGE_SIZE, MAX_TIMEOUT_MS, REVENUE_API_VERSION},
     36     error::ApiResult,
     37 };
     38 
     39 pub trait Revenue: TalerApi {
     40     fn history(
     41         &self,
     42         params: History,
     43     ) -> impl std::future::Future<Output = ApiResult<RevenueIncomingHistory>> + Send;
     44 }
     45 
     46 pub fn router<I: Revenue>(state: Arc<I>, auth: AuthMethod) -> Router {
     47     Router::new()
     48         .route(
     49             "/history",
     50             get(
     51                 async |State(state): State<Arc<I>>, Query(params): Query<HistoryParams>| {
     52                     let params = params.check(MAX_PAGE_SIZE, MAX_TIMEOUT_MS)?;
     53                     let history = state.history(params).await?;
     54                     ApiResult::Ok(if history.incoming_transactions.is_empty() {
     55                         StatusCode::NO_CONTENT.into_response()
     56                     } else {
     57                         Json(history).into_response()
     58                     })
     59                 },
     60             ),
     61         )
     62         .auth(auth, "taler-revenue")
     63         .route(
     64             "/config",
     65             get(async |State(state): State<Arc<I>>| {
     66                 Json(RevenueConfig {
     67                     name: "taler-revenue",
     68                     version: REVENUE_API_VERSION,
     69                     currency: state.currency(),
     70                     implementation: Some(state.implementation()),
     71                 })
     72                 .into_response()
     73             }),
     74         )
     75         .with_state(state)
     76 }