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