taler-auditor-httpd_get-monitoring-balances.c (3405B)
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 #include "auditor-database/get_balances.h" 26 #include "auditor-database/preflight.h" 27 28 /** 29 * Add balance to the list. 30 * 31 * @param[in,out] cls a `json_t *` array to extend 32 * @param dc struct of with balance data 33 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 34 */ 35 static enum GNUNET_GenericReturnValue 36 process_balances ( 37 void *cls, 38 const struct TALER_AUDITORDB_Balances *dc) 39 { 40 json_t *list = cls; 41 json_t *obj; 42 43 obj = GNUNET_JSON_PACK ( 44 GNUNET_JSON_pack_string ("balance_key", 45 dc->balance_key), 46 TALER_JSON_pack_amount ("balance_value", 47 &dc->balance_value) 48 ); 49 GNUNET_break (0 == 50 json_array_append_new (list, 51 obj)); 52 return GNUNET_OK; 53 } 54 55 56 MHD_RESULT 57 TAH_get_monitoring_balances ( 58 struct TAH_RequestHandler *rh, 59 struct MHD_Connection *connection, 60 void **connection_cls, 61 const char *upload_data, 62 size_t *upload_data_size, 63 const char *const args[]) 64 { 65 json_t *ja; 66 enum GNUNET_DB_QueryStatus qs; 67 const char *balance_key; 68 69 (void) rh; 70 (void) connection_cls; 71 (void) upload_data; 72 (void) upload_data_size; 73 if (GNUNET_SYSERR == 74 TALER_AUDITORDB_preflight (TAH_apg)) 75 { 76 GNUNET_break (0); 77 return TALER_MHD_reply_with_error (connection, 78 MHD_HTTP_INTERNAL_SERVER_ERROR, 79 TALER_EC_GENERIC_DB_SETUP_FAILED, 80 NULL); 81 } 82 balance_key 83 = MHD_lookup_connection_value (connection, 84 MHD_GET_ARGUMENT_KIND, 85 "balance_key"); 86 ja = json_array (); 87 GNUNET_break (NULL != ja); 88 qs = TALER_AUDITORDB_get_balances ( 89 TAH_apg, 90 balance_key, 91 &process_balances, 92 ja); 93 if (0 > qs) 94 { 95 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 96 json_decref (ja); 97 TALER_LOG_WARNING ( 98 "Failed to handle GET /balances"); 99 return TALER_MHD_reply_with_error (connection, 100 MHD_HTTP_INTERNAL_SERVER_ERROR, 101 TALER_EC_GENERIC_DB_FETCH_FAILED, 102 "balances"); 103 } 104 return TALER_MHD_REPLY_JSON_PACK ( 105 connection, 106 MHD_HTTP_OK, 107 GNUNET_JSON_pack_array_steal ("balances", 108 ja)); 109 }