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_reserves-get.c (4557B)


      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 "taler/platform.h"
     17 #include <gnunet/gnunet_util_lib.h>
     18 #include <gnunet/gnunet_json_lib.h>
     19 #include <jansson.h>
     20 #include <microhttpd.h>
     21 #include <pthread.h>
     22 #include "taler/taler_json_lib.h"
     23 #include "taler/taler_mhd_lib.h"
     24 #include "taler-auditor-httpd.h"
     25 #include "taler-auditor-httpd_reserves-get.h"
     26 
     27 
     28 /**
     29  * Add reserves to the list.
     30  *
     31  * @param[in,out] cls a `json_t *` array to extend
     32  * @param serial_id location of the @a dc in the database
     33  * @param dc struct of inconsistencies
     34  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     35  */
     36 static enum GNUNET_GenericReturnValue
     37 process_reserves (
     38   void *cls,
     39   uint64_t serial_id,
     40   const struct TALER_AUDITORDB_Reserves *dc)
     41 {
     42   json_t *list = cls;
     43   json_t *obj;
     44 
     45   obj = GNUNET_JSON_PACK (
     46     GNUNET_JSON_pack_int64 ("auditor_reserves_rowid",
     47                             dc->auditor_reserves_rowid),
     48     GNUNET_JSON_pack_data_auto ("reserve_pub",
     49                                 &dc->reserve_pub),
     50     TALER_JSON_pack_amount ("reserve_balance",
     51                             &dc->reserve_balance),
     52     TALER_JSON_pack_amount ("reserve_loss",
     53                             &dc->reserve_loss),
     54     TALER_JSON_pack_amount ("withdraw_fee_balance",
     55                             &dc->withdraw_fee_balance),
     56     TALER_JSON_pack_amount ("close_fee_balance",
     57                             &dc->close_fee_balance),
     58     TALER_JSON_pack_amount ("purse_fee_balance",
     59                             &dc->purse_fee_balance),
     60     TALER_JSON_pack_amount ("open_fee_balance",
     61                             &dc->open_fee_balance),
     62     TALER_JSON_pack_amount ("history_fee_balance",
     63                             &dc->history_fee_balance),
     64     TALER_JSON_pack_time_abs_human ("expiration_date",
     65                                     dc->expiration_date),
     66     TALER_JSON_pack_full_payto ("origin_account",
     67                                 dc->origin_account)
     68     );
     69   GNUNET_break (0 ==
     70                 json_array_append_new (list,
     71                                        obj));
     72   return GNUNET_OK;
     73 }
     74 
     75 
     76 MHD_RESULT
     77 TAH_RESERVES_handler_get (
     78   struct TAH_RequestHandler *rh,
     79   struct MHD_Connection *connection,
     80   void **connection_cls,
     81   const char *upload_data,
     82   size_t *upload_data_size,
     83   const char *const args[])
     84 {
     85   json_t *ja;
     86   enum GNUNET_DB_QueryStatus qs;
     87   int64_t limit = -20;
     88   uint64_t offset;
     89 
     90   (void) rh;
     91   (void) connection_cls;
     92   (void) upload_data;
     93   (void) upload_data_size;
     94   if (GNUNET_SYSERR ==
     95       TAH_plugin->preflight (TAH_plugin->cls))
     96   {
     97     GNUNET_break (0);
     98     return TALER_MHD_reply_with_error (connection,
     99                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    100                                        TALER_EC_GENERIC_DB_SETUP_FAILED,
    101                                        NULL);
    102   }
    103   TALER_MHD_parse_request_snumber (connection,
    104                                    "limit",
    105                                    &limit);
    106 
    107   if (limit < 0)
    108     offset = INT64_MAX;
    109   else
    110     offset = 0;
    111   TALER_MHD_parse_request_number (connection,
    112                                   "offset",
    113                                   &offset);
    114   ja = json_array ();
    115   GNUNET_break (NULL != ja);
    116   qs = TAH_plugin->get_reserves (
    117     TAH_plugin->cls,
    118     limit,
    119     offset,
    120     &process_reserves,
    121     ja);
    122   if (0 > qs)
    123   {
    124     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    125     json_decref (ja);
    126     TALER_LOG_WARNING (
    127       "Failed to handle GET /reserves");
    128     return TALER_MHD_reply_with_error (connection,
    129                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    130                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    131                                        "reserves");
    132   }
    133   return TALER_MHD_REPLY_JSON_PACK (
    134     connection,
    135     MHD_HTTP_OK,
    136     GNUNET_JSON_pack_array_steal ("reserves",
    137                                   ja));
    138 }