taler-auditor-httpd_get-monitoring-purses.c (3700B)
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 #include "auditor-database/get_purses.h" 28 #include "auditor-database/preflight.h" 29 30 /** 31 * Add purses to the list. 32 * 33 * @param[in,out] cls a `json_t *` array to extend 34 * @param serial_id location of the @a dc in the database 35 * @param dc struct of inconsistencies 36 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 37 */ 38 static enum GNUNET_GenericReturnValue 39 process_purses ( 40 void *cls, 41 uint64_t serial_id, 42 const struct TALER_AUDITORDB_Purses *dc) 43 { 44 json_t *list = cls; 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 TALER_JSON_pack_time_abs_human ("expiration_date", dc->expiration_date) 54 55 56 ); 57 GNUNET_break (0 == 58 json_array_append_new (list, 59 obj)); 60 61 62 return GNUNET_OK; 63 } 64 65 66 MHD_RESULT 67 TAH_get_monitoring_purses ( 68 struct TAH_RequestHandler *rh, 69 struct MHD_Connection *connection, 70 void **connection_cls, 71 const char *upload_data, 72 size_t *upload_data_size, 73 const char *const args[]) 74 { 75 json_t *ja; 76 enum GNUNET_DB_QueryStatus qs; 77 int64_t limit = -20; 78 uint64_t offset; 79 80 (void) rh; 81 (void) connection_cls; 82 (void) upload_data; 83 (void) upload_data_size; 84 if (GNUNET_SYSERR == 85 TALER_AUDITORDB_preflight (TAH_apg)) 86 { 87 GNUNET_break (0); 88 return TALER_MHD_reply_with_error ( 89 connection, 90 MHD_HTTP_INTERNAL_SERVER_ERROR, 91 TALER_EC_GENERIC_DB_SETUP_FAILED, 92 NULL); 93 } 94 TALER_MHD_parse_request_snumber (connection, 95 "limit", 96 &limit); 97 if (limit < 0) 98 offset = INT64_MAX; 99 else 100 offset = 0; 101 TALER_MHD_parse_request_number (connection, 102 "offset", 103 &offset); 104 ja = json_array (); 105 GNUNET_break (NULL != ja); 106 qs = TALER_AUDITORDB_get_purses ( 107 TAH_apg, 108 limit, 109 offset, 110 &process_purses, 111 ja); 112 if (0 > qs) 113 { 114 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 115 json_decref (ja); 116 TALER_LOG_WARNING ( 117 "Failed to handle GET /purses"); 118 return TALER_MHD_reply_with_error (connection, 119 MHD_HTTP_INTERNAL_SERVER_ERROR, 120 TALER_EC_GENERIC_DB_FETCH_FAILED, 121 "purses"); 122 } 123 return TALER_MHD_REPLY_JSON_PACK ( 124 connection, 125 MHD_HTTP_OK, 126 GNUNET_JSON_pack_array_steal ("purses", 127 ja)); 128 }