Line data Source code
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 merchant/backend/taler-merchant-httpd_private-get-reports.c
20 : * @brief implementation of GET /private/reports
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include "taler-merchant-httpd_private-get-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 0 : add_report (void *cls,
45 : uint64_t report_id,
46 : const char *report_description,
47 : struct GNUNET_TIME_Relative frequency)
48 : {
49 0 : json_t *reports = cls;
50 : json_t *entry;
51 :
52 0 : 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 0 : GNUNET_assert (NULL != entry);
60 0 : GNUNET_assert (0 ==
61 : json_array_append_new (reports,
62 : entry));
63 0 : }
64 :
65 :
66 : MHD_RESULT
67 0 : TMH_private_get_reports (const struct TMH_RequestHandler *rh,
68 : struct MHD_Connection *connection,
69 : struct TMH_HandlerContext *hc)
70 : {
71 0 : int64_t limit = -20;
72 : uint64_t offset;
73 : enum GNUNET_DB_QueryStatus qs;
74 : json_t *reports;
75 :
76 : (void) rh;
77 0 : TALER_MHD_parse_request_snumber (connection,
78 : "limit",
79 : &limit);
80 0 : if (limit > 0)
81 0 : offset = 0;
82 : else
83 0 : offset = INT64_MAX;
84 0 : TALER_MHD_parse_request_number (connection,
85 : "offset",
86 : &offset);
87 0 : if ( (-MAX_DELTA > limit) ||
88 0 : (limit > MAX_DELTA) )
89 : {
90 0 : GNUNET_break_op (0);
91 0 : return TALER_MHD_reply_with_error (connection,
92 : MHD_HTTP_BAD_REQUEST,
93 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
94 : "limit");
95 : }
96 :
97 0 : reports = json_array ();
98 0 : GNUNET_assert (NULL != reports);
99 0 : qs = TMH_db->select_reports (TMH_db->cls,
100 0 : hc->instance->settings.id,
101 : limit,
102 : offset,
103 : &add_report,
104 : reports);
105 0 : if (qs < 0)
106 : {
107 0 : json_decref (reports);
108 0 : 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 0 : return TALER_MHD_REPLY_JSON_PACK (
115 : connection,
116 : MHD_HTTP_OK,
117 : GNUNET_JSON_pack_array_steal ("reports",
118 : reports));
119 : }
|