exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-auditor-httpd_get-monitoring-balances.c (3652B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 Taler Systems SA
      4 
      5    TALER is free software; you can redistribute it and/or modify it under the
      6    terms of the GNU 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 General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 #include <gnunet/gnunet_util_lib.h>
     17 #include <gnunet/gnunet_json_lib.h>
     18 #include <jansson.h>
     19 #include <microhttpd.h>
     20 #include <pthread.h>
     21 #include "taler/taler_json_lib.h"
     22 #include "taler/taler_mhd_lib.h"
     23 #include "taler-auditor-httpd.h"
     24 #include "taler-auditor-httpd_get-monitoring-balances.h"
     25 #define TALER_AUDITORDB_BALANCES_RESULT_CLOSURE json_t
     26 #include "auditor-database/iterate_balances.h"
     27 #include "auditor-database/preflight.h"
     28 
     29 /**
     30  * Add balance to the list.
     31  *
     32  * @param[in,out] list a `json_t *` array to extend
     33  * @param dc struct of with balance data
     34  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     35  */
     36 static enum GNUNET_GenericReturnValue
     37 process_balances (
     38   json_t *list,
     39   const struct TALER_AUDITORDB_Balances *dc)
     40 {
     41   json_t *obj;
     42 
     43   obj = GNUNET_JSON_PACK (
     44     GNUNET_JSON_pack_string ("balance_key",
     45                              dc->balance_key),
     46     GNUNET_JSON_pack_allow_null (
     47       TALER_JSON_pack_amount ("balance_value",
     48                               (GNUNET_OK ==
     49                                TALER_amount_is_valid (&dc->balance_value))
     50                               ? &dc->balance_value
     51                               : NULL))
     52     );
     53   GNUNET_break (0 ==
     54                 json_array_append_new (list,
     55                                        obj));
     56   return GNUNET_OK;
     57 }
     58 
     59 
     60 enum MHD_Result
     61 TAH_get_monitoring_balances (
     62   struct TAH_RequestHandler *rh,
     63   struct MHD_Connection *connection,
     64   void **connection_cls,
     65   const char *upload_data,
     66   size_t *upload_data_size,
     67   const char *const args[])
     68 {
     69   json_t *ja;
     70   enum GNUNET_DB_QueryStatus qs;
     71   const char *balance_key;
     72 
     73   (void) rh;
     74   (void) connection_cls;
     75   (void) upload_data;
     76   (void) upload_data_size;
     77   if (GNUNET_SYSERR ==
     78       TALER_AUDITORDB_preflight (TAH_apg))
     79   {
     80     GNUNET_break (0);
     81     return TALER_MHD_reply_with_error (connection,
     82                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     83                                        TALER_EC_GENERIC_DB_SETUP_FAILED,
     84                                        NULL);
     85   }
     86   balance_key
     87     = MHD_lookup_connection_value (connection,
     88                                    MHD_GET_ARGUMENT_KIND,
     89                                    "balance_key");
     90   ja = json_array ();
     91   GNUNET_break (NULL != ja);
     92   qs = TALER_AUDITORDB_iterate_balances (
     93     TAH_apg,
     94     balance_key,
     95     &process_balances,
     96     ja);
     97   if (0 > qs)
     98   {
     99     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    100     json_decref (ja);
    101     TALER_LOG_WARNING (
    102       "Failed to handle GET /balances");
    103     return TALER_MHD_reply_with_error (connection,
    104                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    105                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    106                                        "balances");
    107   }
    108   return TALER_MHD_REPLY_JSON_PACK (
    109     connection,
    110     MHD_HTTP_OK,
    111     GNUNET_JSON_pack_array_steal ("balances",
    112                                   ja));
    113 }