Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025-2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as
7 : published by the Free Software Foundation; either version 2.1,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General
16 : Public License along with TALER; see the file COPYING.LGPL.
17 : If not, see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file src/lib/merchant_api_post-private-units.c
21 : * @brief Implementation of the POST /private/units request
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include <curl/curl.h>
26 : #include <jansson.h>
27 : #include <microhttpd.h> /* just for HTTP status codes */
28 : #include <gnunet/gnunet_util_lib.h>
29 : #include <gnunet/gnunet_curl_lib.h>
30 : #include <taler/merchant/post-private-units.h>
31 : #include "merchant_api_curl_defaults.h"
32 : #include "merchant_api_common.h"
33 : #include <taler/taler_json_lib.h>
34 : #include <taler/taler_curl_lib.h>
35 :
36 :
37 : /**
38 : * Handle for a POST /private/units operation.
39 : */
40 : struct TALER_MERCHANT_PostPrivateUnitsHandle
41 : {
42 : /**
43 : * Base URL of the merchant backend.
44 : */
45 : char *base_url;
46 :
47 : /**
48 : * The full URL for this request.
49 : */
50 : char *url;
51 :
52 : /**
53 : * Handle for the request.
54 : */
55 : struct GNUNET_CURL_Job *job;
56 :
57 : /**
58 : * Function to call with the result.
59 : */
60 : TALER_MERCHANT_PostPrivateUnitsCallback cb;
61 :
62 : /**
63 : * Closure for @a cb.
64 : */
65 : TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE *cb_cls;
66 :
67 : /**
68 : * Reference to the execution context.
69 : */
70 : struct GNUNET_CURL_Context *ctx;
71 :
72 : /**
73 : * Minor context that holds body and headers.
74 : */
75 : struct TALER_CURL_PostContext post_ctx;
76 :
77 : /**
78 : * Unit identifier.
79 : */
80 : char *unit_id;
81 :
82 : /**
83 : * Long human-readable name.
84 : */
85 : char *unit_name_long;
86 :
87 : /**
88 : * Short symbol for the unit.
89 : */
90 : char *unit_name_short;
91 :
92 : /**
93 : * Whether fractional quantities are allowed.
94 : */
95 : bool unit_allow_fraction;
96 :
97 : /**
98 : * Precision level for fractional quantities.
99 : */
100 : uint32_t unit_precision_level;
101 :
102 : /**
103 : * Whether the unit is active.
104 : */
105 : bool unit_active;
106 :
107 : /**
108 : * Whether @e unit_allow_fraction was explicitly set via options.
109 : */
110 : bool have_unit_allow_fraction;
111 :
112 : /**
113 : * Whether @e unit_precision_level was explicitly set via options.
114 : */
115 : bool have_unit_precision_level;
116 :
117 : /**
118 : * Whether @e unit_active was explicitly set via options.
119 : */
120 : bool have_unit_active;
121 :
122 : /**
123 : * Optional internationalized long names (JSON).
124 : */
125 : json_t *unit_name_long_i18n;
126 :
127 : /**
128 : * Optional internationalized short names (JSON).
129 : */
130 : json_t *unit_name_short_i18n;
131 : };
132 :
133 :
134 : /**
135 : * Function called when we're done processing the
136 : * HTTP POST /private/units request.
137 : *
138 : * @param cls the `struct TALER_MERCHANT_PostPrivateUnitsHandle`
139 : * @param response_code HTTP response code, 0 on error
140 : * @param response response body, NULL if not in JSON
141 : */
142 : static void
143 8 : handle_post_units_finished (void *cls,
144 : long response_code,
145 : const void *response)
146 : {
147 8 : struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh = cls;
148 8 : const json_t *json = response;
149 8 : struct TALER_MERCHANT_PostPrivateUnitsResponse pur = {
150 8 : .hr.http_status = (unsigned int) response_code,
151 : .hr.reply = json
152 : };
153 :
154 8 : ppuh->job = NULL;
155 8 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
156 : "POST /private/units completed with response code %u\n",
157 : (unsigned int) response_code);
158 8 : switch (response_code)
159 : {
160 0 : case 0:
161 0 : pur.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
162 0 : break;
163 6 : case MHD_HTTP_NO_CONTENT:
164 6 : break;
165 2 : case MHD_HTTP_BAD_REQUEST:
166 : case MHD_HTTP_UNAUTHORIZED:
167 : case MHD_HTTP_FORBIDDEN:
168 : case MHD_HTTP_NOT_FOUND:
169 : case MHD_HTTP_CONFLICT:
170 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
171 2 : pur.hr.ec = TALER_JSON_get_error_code (json);
172 2 : pur.hr.hint = TALER_JSON_get_error_hint (json);
173 2 : break;
174 0 : default:
175 0 : TALER_MERCHANT_parse_error_details_ (json,
176 : response_code,
177 : &pur.hr);
178 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 : "Unexpected response code %u/%d\n",
180 : (unsigned int) response_code,
181 : (int) pur.hr.ec);
182 0 : GNUNET_break_op (0);
183 0 : break;
184 : }
185 8 : ppuh->cb (ppuh->cb_cls,
186 : &pur);
187 8 : TALER_MERCHANT_post_private_units_cancel (ppuh);
188 8 : }
189 :
190 :
191 : struct TALER_MERCHANT_PostPrivateUnitsHandle *
192 8 : TALER_MERCHANT_post_private_units_create (
193 : struct GNUNET_CURL_Context *ctx,
194 : const char *url,
195 : const char *unit_id,
196 : const char *unit_name_long,
197 : const char *unit_name_short)
198 : {
199 : struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh;
200 :
201 8 : ppuh = GNUNET_new (struct TALER_MERCHANT_PostPrivateUnitsHandle);
202 8 : ppuh->ctx = ctx;
203 8 : ppuh->base_url = GNUNET_strdup (url);
204 8 : ppuh->unit_id = GNUNET_strdup (unit_id);
205 8 : ppuh->unit_name_long = GNUNET_strdup (unit_name_long);
206 8 : ppuh->unit_name_short = GNUNET_strdup (unit_name_short);
207 8 : return ppuh;
208 : }
209 :
210 :
211 : enum GNUNET_GenericReturnValue
212 20 : TALER_MERCHANT_post_private_units_set_options_ (
213 : struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh,
214 : unsigned int num_options,
215 : const struct TALER_MERCHANT_PostPrivateUnitsOptionValue *options)
216 : {
217 56 : for (unsigned int i = 0; i < num_options; i++)
218 : {
219 56 : switch (options[i].option)
220 : {
221 20 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_END:
222 20 : return GNUNET_OK;
223 6 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_LONG_I18N:
224 6 : json_decref (ppuh->unit_name_long_i18n);
225 : ppuh->unit_name_long_i18n
226 6 : = json_incref ((json_t *) options[i].details.unit_name_long_i18n);
227 6 : break;
228 6 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_SHORT_I18N:
229 6 : json_decref (ppuh->unit_name_short_i18n);
230 : ppuh->unit_name_short_i18n
231 6 : = json_incref ((json_t *) options[i].details.unit_name_short_i18n);
232 6 : break;
233 8 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ALLOW_FRACTION:
234 8 : ppuh->unit_allow_fraction = options[i].details.unit_allow_fraction;
235 8 : ppuh->have_unit_allow_fraction = true;
236 8 : break;
237 8 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_PRECISION_LEVEL:
238 8 : ppuh->unit_precision_level = options[i].details.unit_precision_level;
239 8 : ppuh->have_unit_precision_level = true;
240 8 : break;
241 8 : case TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ACTIVE:
242 8 : ppuh->unit_active = options[i].details.unit_active;
243 8 : ppuh->have_unit_active = true;
244 8 : break;
245 0 : default:
246 0 : GNUNET_break (0);
247 0 : return GNUNET_SYSERR;
248 : }
249 : }
250 0 : return GNUNET_OK;
251 : }
252 :
253 :
254 : enum TALER_ErrorCode
255 8 : TALER_MERCHANT_post_private_units_start (
256 : struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh,
257 : TALER_MERCHANT_PostPrivateUnitsCallback cb,
258 : TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE *cb_cls)
259 : {
260 : json_t *req_obj;
261 : CURL *eh;
262 :
263 8 : ppuh->cb = cb;
264 8 : ppuh->cb_cls = cb_cls;
265 8 : ppuh->url = TALER_url_join (ppuh->base_url,
266 : "private/units",
267 : NULL);
268 8 : if (NULL == ppuh->url)
269 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
270 8 : req_obj = GNUNET_JSON_PACK (
271 : GNUNET_JSON_pack_string ("unit",
272 : ppuh->unit_id),
273 : GNUNET_JSON_pack_string ("unit_name_long",
274 : ppuh->unit_name_long),
275 : GNUNET_JSON_pack_string ("unit_name_short",
276 : ppuh->unit_name_short),
277 : GNUNET_JSON_pack_bool ("unit_allow_fraction",
278 : ppuh->have_unit_allow_fraction
279 : ? ppuh->unit_allow_fraction
280 : : false),
281 : GNUNET_JSON_pack_uint64 ("unit_precision_level",
282 : ppuh->have_unit_precision_level
283 : ? (uint64_t) ppuh->unit_precision_level
284 : : 0),
285 : GNUNET_JSON_pack_bool ("unit_active",
286 : ppuh->have_unit_active
287 : ? ppuh->unit_active
288 : : true),
289 : GNUNET_JSON_pack_allow_null (
290 : GNUNET_JSON_pack_object_incref ("unit_name_long_i18n",
291 : ppuh->unit_name_long_i18n)),
292 : GNUNET_JSON_pack_allow_null (
293 : GNUNET_JSON_pack_object_incref ("unit_name_short_i18n",
294 : ppuh->unit_name_short_i18n)));
295 8 : eh = TALER_MERCHANT_curl_easy_get_ (ppuh->url);
296 16 : if ( (NULL == eh) ||
297 : (GNUNET_OK !=
298 8 : TALER_curl_easy_post (&ppuh->post_ctx,
299 : eh,
300 : req_obj)) )
301 : {
302 0 : GNUNET_break (0);
303 0 : json_decref (req_obj);
304 0 : if (NULL != eh)
305 0 : curl_easy_cleanup (eh);
306 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
307 : }
308 8 : json_decref (req_obj);
309 16 : ppuh->job = GNUNET_CURL_job_add2 (ppuh->ctx,
310 : eh,
311 8 : ppuh->post_ctx.headers,
312 : &handle_post_units_finished,
313 : ppuh);
314 8 : if (NULL == ppuh->job)
315 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
316 8 : return TALER_EC_NONE;
317 : }
318 :
319 :
320 : void
321 8 : TALER_MERCHANT_post_private_units_cancel (
322 : struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh)
323 : {
324 8 : if (NULL != ppuh->job)
325 : {
326 0 : GNUNET_CURL_job_cancel (ppuh->job);
327 0 : ppuh->job = NULL;
328 : }
329 8 : TALER_curl_easy_post_finished (&ppuh->post_ctx);
330 8 : json_decref (ppuh->unit_name_long_i18n);
331 8 : json_decref (ppuh->unit_name_short_i18n);
332 8 : GNUNET_free (ppuh->unit_id);
333 8 : GNUNET_free (ppuh->unit_name_long);
334 8 : GNUNET_free (ppuh->unit_name_short);
335 8 : GNUNET_free (ppuh->url);
336 8 : GNUNET_free (ppuh->base_url);
337 8 : GNUNET_free (ppuh);
338 8 : }
339 :
340 :
341 : /* end of merchant_api_post-private-units-new.c */
|