Line data Source code
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 :
17 :
18 : #include "taler/platform.h"
19 : #include <gnunet/gnunet_util_lib.h>
20 : #include <gnunet/gnunet_json_lib.h>
21 : #include <jansson.h>
22 : #include <microhttpd.h>
23 : #include <pthread.h>
24 : #include "taler/taler_json_lib.h"
25 : #include "taler/taler_mhd_lib.h"
26 : #include "taler-auditor-httpd.h"
27 : #include "taler-auditor-httpd_purses-get.h"
28 :
29 : /**
30 : * Add purses to the list.
31 : *
32 : * @param[in,out] cls a `json_t *` array to extend
33 : * @param serial_id location of the @a dc in the database
34 : * @param dc struct of inconsistencies
35 : * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
36 : */
37 : static enum GNUNET_GenericReturnValue
38 0 : process_purses (
39 : void *cls,
40 : uint64_t serial_id,
41 : const struct TALER_AUDITORDB_Purses *dc)
42 : {
43 0 : json_t *list = cls;
44 : json_t *obj;
45 :
46 0 : obj = GNUNET_JSON_PACK (
47 :
48 : GNUNET_JSON_pack_int64 ("auditor_purses_rowid", dc->auditor_purses_rowid),
49 : GNUNET_JSON_pack_data_auto ("purse_pub", &dc->purse_pub),
50 : TALER_JSON_pack_amount ("balance", &dc->balance),
51 : TALER_JSON_pack_amount ("target", &dc->target),
52 : TALER_JSON_pack_time_abs_human ("expiration_date", dc->expiration_date)
53 :
54 :
55 : );
56 0 : GNUNET_break (0 ==
57 : json_array_append_new (list,
58 : obj));
59 :
60 :
61 0 : return GNUNET_OK;
62 : }
63 :
64 :
65 : MHD_RESULT
66 0 : TAH_PURSES_handler_get (
67 : struct TAH_RequestHandler *rh,
68 : struct MHD_Connection *connection,
69 : void **connection_cls,
70 : const char *upload_data,
71 : size_t *upload_data_size,
72 : const char *const args[])
73 : {
74 : json_t *ja;
75 : enum GNUNET_DB_QueryStatus qs;
76 0 : int64_t limit = -20;
77 : uint64_t offset;
78 :
79 : (void) rh;
80 : (void) connection_cls;
81 : (void) upload_data;
82 : (void) upload_data_size;
83 0 : if (GNUNET_SYSERR ==
84 0 : TAH_plugin->preflight (TAH_plugin->cls))
85 : {
86 0 : GNUNET_break (0);
87 0 : return TALER_MHD_reply_with_error (
88 : connection,
89 : MHD_HTTP_INTERNAL_SERVER_ERROR,
90 : TALER_EC_GENERIC_DB_SETUP_FAILED,
91 : NULL);
92 : }
93 0 : TALER_MHD_parse_request_snumber (connection,
94 : "limit",
95 : &limit);
96 0 : if (limit < 0)
97 0 : offset = INT64_MAX;
98 : else
99 0 : offset = 0;
100 0 : TALER_MHD_parse_request_number (connection,
101 : "offset",
102 : &offset);
103 0 : ja = json_array ();
104 0 : GNUNET_break (NULL != ja);
105 0 : qs = TAH_plugin->get_purses (
106 0 : TAH_plugin->cls,
107 : limit,
108 : offset,
109 : &process_purses,
110 : ja);
111 0 : if (0 > qs)
112 : {
113 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
114 0 : json_decref (ja);
115 0 : TALER_LOG_WARNING (
116 : "Failed to handle GET /purses");
117 0 : return TALER_MHD_reply_with_error (connection,
118 : MHD_HTTP_INTERNAL_SERVER_ERROR,
119 : TALER_EC_GENERIC_DB_FETCH_FAILED,
120 : "purses");
121 : }
122 0 : return TALER_MHD_REPLY_JSON_PACK (
123 : connection,
124 : MHD_HTTP_OK,
125 : GNUNET_JSON_pack_array_steal ("purses",
126 : ja));
127 : }
|