taler-merchant-httpd_get-private-reports.c (3834B)
1 /* 2 This file is part of TALER 3 (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero 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 Affero General Public License for more 12 details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with TALER; see the file COPYING. If not, see 16 <http://www.gnu.org/licenses/> 17 */ 18 /** 19 * @file src/backend/taler-merchant-httpd_get-private-reports.c 20 * @brief implementation of GET /private/reports 21 * @author Christian Grothoff 22 */ 23 #include "platform.h" 24 #include "taler-merchant-httpd_get-private-reports.h" 25 #include <taler/taler_json_lib.h> 26 #include <taler/taler_dbevents.h> 27 #include "merchant-database/select_reports.h" 28 29 30 /** 31 * Sensible bound on the limit. 32 */ 33 #define MAX_DELTA 1024 34 35 36 /** 37 * Callback for listing reports. 38 * 39 * @param cls closure with a `json_t *` 40 * @param report_id unique identifier of the report 41 * @param report_description human-readable description 42 * @param frequency how often the report is generated 43 */ 44 static void 45 add_report (void *cls, 46 uint64_t report_id, 47 const char *report_description, 48 struct GNUNET_TIME_Relative frequency) 49 { 50 json_t *reports = cls; 51 json_t *entry; 52 53 entry = GNUNET_JSON_PACK ( 54 GNUNET_JSON_pack_uint64 ("report_serial", 55 report_id), 56 GNUNET_JSON_pack_string ("description", 57 report_description), 58 GNUNET_JSON_pack_time_rel ("report_frequency", 59 frequency)); 60 GNUNET_assert (NULL != entry); 61 GNUNET_assert (0 == 62 json_array_append_new (reports, 63 entry)); 64 } 65 66 67 enum MHD_Result 68 TMH_private_get_reports (const struct TMH_RequestHandler *rh, 69 struct MHD_Connection *connection, 70 struct TMH_HandlerContext *hc) 71 { 72 int64_t limit = -20; 73 uint64_t offset; 74 enum GNUNET_DB_QueryStatus qs; 75 json_t *reports; 76 77 (void) rh; 78 TALER_MHD_parse_request_snumber (connection, 79 "limit", 80 &limit); 81 if (limit > 0) 82 offset = 0; 83 else 84 offset = INT64_MAX; 85 TALER_MHD_parse_request_number (connection, 86 "offset", 87 &offset); 88 if ( (-MAX_DELTA > limit) || 89 (limit > MAX_DELTA) ) 90 { 91 GNUNET_break_op (0); 92 return TALER_MHD_reply_with_error (connection, 93 MHD_HTTP_BAD_REQUEST, 94 TALER_EC_GENERIC_PARAMETER_MALFORMED, 95 "limit"); 96 } 97 98 reports = json_array (); 99 GNUNET_assert (NULL != reports); 100 qs = TALER_MERCHANTDB_select_reports (TMH_db, 101 hc->instance->settings.id, 102 limit, 103 offset, 104 &add_report, 105 reports); 106 if (qs < 0) 107 { 108 json_decref (reports); 109 return TALER_MHD_reply_with_error (connection, 110 MHD_HTTP_INTERNAL_SERVER_ERROR, 111 TALER_EC_GENERIC_DB_FETCH_FAILED, 112 "select_reports"); 113 } 114 115 return TALER_MHD_REPLY_JSON_PACK ( 116 connection, 117 MHD_HTTP_OK, 118 GNUNET_JSON_pack_array_steal ("reports", 119 reports)); 120 }