taler-merchant-httpd_get-private-reports.c (3728B)
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 taler-merchant-httpd_get-private-reports.c 20 * @brief implementation of GET /private/reports 21 * @author Christian Grothoff 22 */ 23 #include "taler/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 28 29 /** 30 * Sensible bound on the limit. 31 */ 32 #define MAX_DELTA 1024 33 34 35 /** 36 * Callback for listing reports. 37 * 38 * @param cls closure with a `json_t *` 39 * @param report_id unique identifier of the report 40 * @param report_description human-readable description 41 * @param frequency how often the report is generated 42 */ 43 static void 44 add_report (void *cls, 45 uint64_t report_id, 46 const char *report_description, 47 struct GNUNET_TIME_Relative frequency) 48 { 49 json_t *reports = cls; 50 json_t *entry; 51 52 entry = GNUNET_JSON_PACK ( 53 GNUNET_JSON_pack_uint64 ("report_serial", 54 report_id), 55 GNUNET_JSON_pack_string ("description", 56 report_description), 57 GNUNET_JSON_pack_time_rel ("report_frequency", 58 frequency)); 59 GNUNET_assert (NULL != entry); 60 GNUNET_assert (0 == 61 json_array_append_new (reports, 62 entry)); 63 } 64 65 66 MHD_RESULT 67 TMH_private_get_reports (const struct TMH_RequestHandler *rh, 68 struct MHD_Connection *connection, 69 struct TMH_HandlerContext *hc) 70 { 71 int64_t limit = -20; 72 uint64_t offset; 73 enum GNUNET_DB_QueryStatus qs; 74 json_t *reports; 75 76 (void) rh; 77 TALER_MHD_parse_request_snumber (connection, 78 "limit", 79 &limit); 80 if (limit > 0) 81 offset = 0; 82 else 83 offset = INT64_MAX; 84 TALER_MHD_parse_request_number (connection, 85 "offset", 86 &offset); 87 if ( (-MAX_DELTA > limit) || 88 (limit > MAX_DELTA) ) 89 { 90 GNUNET_break_op (0); 91 return TALER_MHD_reply_with_error (connection, 92 MHD_HTTP_BAD_REQUEST, 93 TALER_EC_GENERIC_PARAMETER_MALFORMED, 94 "limit"); 95 } 96 97 reports = json_array (); 98 GNUNET_assert (NULL != reports); 99 qs = TMH_db->select_reports (TMH_db->cls, 100 hc->instance->settings.id, 101 limit, 102 offset, 103 &add_report, 104 reports); 105 if (qs < 0) 106 { 107 json_decref (reports); 108 return TALER_MHD_reply_with_error (connection, 109 MHD_HTTP_INTERNAL_SERVER_ERROR, 110 TALER_EC_GENERIC_DB_FETCH_FAILED, 111 "select_reports"); 112 } 113 114 return TALER_MHD_REPLY_JSON_PACK ( 115 connection, 116 MHD_HTTP_OK, 117 GNUNET_JSON_pack_array_steal ("reports", 118 reports)); 119 }