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-purses.c (3759B)


      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 
     17 
     18 #include <gnunet/gnunet_util_lib.h>
     19 #include <gnunet/gnunet_json_lib.h>
     20 #include <jansson.h>
     21 #include <microhttpd.h>
     22 #include <pthread.h>
     23 #include "taler/taler_json_lib.h"
     24 #include "taler/taler_mhd_lib.h"
     25 #include "taler-auditor-httpd.h"
     26 #include "taler-auditor-httpd_get-monitoring-purses.h"
     27 #define TALER_AUDITORDB_PURSES_RESULT_CLOSURE json_t
     28 #include "auditor-database/iterate_purses.h"
     29 #include "auditor-database/preflight.h"
     30 
     31 /**
     32  * Add purses to the list.
     33  *
     34  * @param[in,out] list a `json_t *` array to extend
     35  * @param serial_id location of the @a dc in the database
     36  * @param dc struct of inconsistencies
     37  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     38  */
     39 static enum GNUNET_GenericReturnValue
     40 process_purses (
     41   json_t *list,
     42   uint64_t serial_id,
     43   const struct TALER_AUDITORDB_Purses *dc)
     44 {
     45   json_t *obj;
     46 
     47   obj = GNUNET_JSON_PACK (
     48 
     49     GNUNET_JSON_pack_int64 ("auditor_purses_rowid", dc->auditor_purses_rowid),
     50     GNUNET_JSON_pack_data_auto ("purse_pub", &dc->purse_pub),
     51     TALER_JSON_pack_amount ("balance", &dc->balance),
     52     TALER_JSON_pack_amount ("target", &dc->target),
     53     GNUNET_JSON_pack_timestamp ("expiration_date",
     54                                 GNUNET_TIME_absolute_to_timestamp (
     55                                   dc->expiration_date))
     56 
     57 
     58     );
     59   GNUNET_break (0 ==
     60                 json_array_append_new (list,
     61                                        obj));
     62 
     63 
     64   return GNUNET_OK;
     65 }
     66 
     67 
     68 enum MHD_Result
     69 TAH_get_monitoring_purses (
     70   struct TAH_RequestHandler *rh,
     71   struct MHD_Connection *connection,
     72   void **connection_cls,
     73   const char *upload_data,
     74   size_t *upload_data_size,
     75   const char *const args[])
     76 {
     77   json_t *ja;
     78   enum GNUNET_DB_QueryStatus qs;
     79   int64_t limit = -20;
     80   uint64_t offset;
     81 
     82   (void) rh;
     83   (void) connection_cls;
     84   (void) upload_data;
     85   (void) upload_data_size;
     86   if (GNUNET_SYSERR ==
     87       TALER_AUDITORDB_preflight (TAH_apg))
     88   {
     89     GNUNET_break (0);
     90     return TALER_MHD_reply_with_error (
     91       connection,
     92       MHD_HTTP_INTERNAL_SERVER_ERROR,
     93       TALER_EC_GENERIC_DB_SETUP_FAILED,
     94       NULL);
     95   }
     96   TAH_PARSE_REQUEST_LIMIT (connection,
     97                            "limit",
     98                            &limit);
     99   if (limit < 0)
    100     offset = TAH_SAFE_UINT64_MAX;
    101   else
    102     offset = 0;
    103   TAH_PARSE_SAFE_REQUEST_NUMBER (connection,
    104                                  "offset",
    105                                  &offset);
    106   ja = json_array ();
    107   GNUNET_break (NULL != ja);
    108   qs = TALER_AUDITORDB_iterate_purses (
    109     TAH_apg,
    110     limit,
    111     offset,
    112     &process_purses,
    113     ja);
    114   if (0 > qs)
    115   {
    116     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    117     json_decref (ja);
    118     TALER_LOG_WARNING (
    119       "Failed to handle GET /purses");
    120     return TALER_MHD_reply_with_error (connection,
    121                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    122                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    123                                        "purses");
    124   }
    125   return TAH_reply_json_records (connection,
    126                                  ja);
    127 }