revenue.rs (2496B)
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::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 crate::{ 32 api::RouterUtils as _, 33 auth::AuthMethod, 34 constants::{MAX_PAGE_SIZE, MAX_TIMEOUT_MS, REVENUE_API_VERSION}, 35 error::ApiResult, 36 }; 37 38 use super::TalerApi; 39 40 pub trait Revenue: TalerApi { 41 fn history( 42 &self, 43 params: History, 44 ) -> impl std::future::Future<Output = ApiResult<RevenueIncomingHistory>> + Send; 45 } 46 47 pub fn router<I: Revenue>(state: Arc<I>, auth: AuthMethod) -> Router { 48 Router::new() 49 .route( 50 "/history", 51 get( 52 |State(state): State<Arc<I>>, Query(params): Query<HistoryParams>| async move { 53 let params = params.check(MAX_PAGE_SIZE, MAX_TIMEOUT_MS)?; 54 let history = state.history(params).await?; 55 ApiResult::Ok(if history.incoming_transactions.is_empty() { 56 StatusCode::NO_CONTENT.into_response() 57 } else { 58 Json(history).into_response() 59 }) 60 }, 61 ), 62 ) 63 .auth(auth, "taler-revenue") 64 .route( 65 "/config", 66 get(|State(state): State<Arc<I>>| async move { 67 Json(RevenueConfig { 68 name: "taler-revenue", 69 version: REVENUE_API_VERSION, 70 currency: state.currency(), 71 implementation: state.implementation(), 72 }) 73 .into_response() 74 }), 75 ) 76 .with_state(state) 77 }