Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, 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 Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file src/lib/merchant_api_get-private-products.c
19 : * @brief Implementation of the GET /private/products request
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include <taler/merchant/get-private-products.h>
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 :
32 :
33 : /**
34 : * Maximum number of products we return.
35 : */
36 : #define MAX_PRODUCTS 1024
37 :
38 :
39 : /**
40 : * Handle for a GET /private/products operation.
41 : */
42 : struct TALER_MERCHANT_GetPrivateProductsHandle
43 : {
44 : /**
45 : * Base URL of the merchant backend.
46 : */
47 : char *base_url;
48 :
49 : /**
50 : * The full URL for this request.
51 : */
52 : char *url;
53 :
54 : /**
55 : * Handle for the request.
56 : */
57 : struct GNUNET_CURL_Job *job;
58 :
59 : /**
60 : * Function to call with the result.
61 : */
62 : TALER_MERCHANT_GetPrivateProductsCallback cb;
63 :
64 : /**
65 : * Closure for @a cb.
66 : */
67 : TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE *cb_cls;
68 :
69 : /**
70 : * Reference to the execution context.
71 : */
72 : struct GNUNET_CURL_Context *ctx;
73 :
74 : /**
75 : * Starting row for pagination.
76 : */
77 : uint64_t offset;
78 :
79 : /**
80 : * Limit on number of results.
81 : */
82 : int64_t limit;
83 :
84 : /**
85 : * Category name filter, or NULL.
86 : */
87 : char *category_filter;
88 :
89 : /**
90 : * Product name filter, or NULL.
91 : */
92 : char *name_filter;
93 :
94 : /**
95 : * Product description filter, or NULL.
96 : */
97 : char *description_filter;
98 :
99 : /**
100 : * Product group serial filter.
101 : */
102 : uint64_t product_group_serial;
103 :
104 : /**
105 : * True if offset was explicitly set.
106 : */
107 : bool have_offset;
108 :
109 : /**
110 : * True if product_group_serial was explicitly set.
111 : */
112 : bool have_product_group_serial;
113 : };
114 :
115 :
116 : /**
117 : * Parse product information from @a ia.
118 : *
119 : * @param ia JSON array (or NULL!) with product data
120 : * @param[in] pgr partially filled response
121 : * @param gpph operation handle
122 : * @return #GNUNET_OK on success
123 : */
124 : static enum GNUNET_GenericReturnValue
125 10 : parse_products (const json_t *ia,
126 : struct TALER_MERCHANT_GetPrivateProductsResponse *pgr,
127 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph)
128 : {
129 10 : unsigned int ies_len = (unsigned int) json_array_size (ia);
130 :
131 10 : if ( (json_array_size (ia) != (size_t) ies_len) ||
132 : (ies_len > MAX_PRODUCTS) )
133 : {
134 0 : GNUNET_break (0);
135 0 : return GNUNET_SYSERR;
136 : }
137 10 : {
138 10 : struct TALER_MERCHANT_GetPrivateProductsInventoryEntry ies[
139 10 : GNUNET_NZL (ies_len)];
140 : size_t index;
141 : json_t *value;
142 :
143 12 : json_array_foreach (ia, index, value) {
144 2 : struct TALER_MERCHANT_GetPrivateProductsInventoryEntry *ie =
145 : &ies[index];
146 : struct GNUNET_JSON_Specification spec[] = {
147 2 : TALER_JSON_spec_slug ("product_id",
148 : &ie->product_id),
149 2 : GNUNET_JSON_spec_uint64 ("product_serial",
150 : &ie->product_serial),
151 2 : GNUNET_JSON_spec_end ()
152 : };
153 :
154 2 : if (GNUNET_OK !=
155 2 : GNUNET_JSON_parse (value,
156 : spec,
157 : NULL, NULL))
158 : {
159 0 : GNUNET_break_op (0);
160 0 : return GNUNET_SYSERR;
161 : }
162 : }
163 10 : pgr->details.ok.products_length = ies_len;
164 10 : pgr->details.ok.products = ies;
165 10 : gpph->cb (gpph->cb_cls,
166 : pgr);
167 10 : gpph->cb = NULL;
168 : }
169 10 : return GNUNET_OK;
170 : }
171 :
172 :
173 : /**
174 : * Function called when we're done processing the
175 : * HTTP GET /private/products request.
176 : *
177 : * @param cls the `struct TALER_MERCHANT_GetPrivateProductsHandle`
178 : * @param response_code HTTP response code, 0 on error
179 : * @param response response body, NULL if not in JSON
180 : */
181 : static void
182 14 : handle_get_products_finished (void *cls,
183 : long response_code,
184 : const void *response)
185 : {
186 14 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph = cls;
187 14 : const json_t *json = response;
188 14 : struct TALER_MERCHANT_GetPrivateProductsResponse pgr = {
189 14 : .hr.http_status = (unsigned int) response_code,
190 : .hr.reply = json
191 : };
192 :
193 14 : gpph->job = NULL;
194 14 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 : "Got /private/products response with status code %u\n",
196 : (unsigned int) response_code);
197 14 : switch (response_code)
198 : {
199 10 : case MHD_HTTP_OK:
200 : {
201 : const json_t *products;
202 : struct GNUNET_JSON_Specification spec[] = {
203 10 : GNUNET_JSON_spec_array_const ("products",
204 : &products),
205 10 : GNUNET_JSON_spec_end ()
206 : };
207 :
208 10 : if (GNUNET_OK !=
209 10 : GNUNET_JSON_parse (json,
210 : spec,
211 : NULL, NULL))
212 : {
213 0 : pgr.hr.http_status = 0;
214 0 : pgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
215 0 : break;
216 : }
217 10 : if (GNUNET_OK ==
218 10 : parse_products (products,
219 : &pgr,
220 : gpph))
221 : {
222 10 : TALER_MERCHANT_get_private_products_cancel (gpph);
223 10 : return;
224 : }
225 0 : pgr.hr.http_status = 0;
226 0 : pgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
227 0 : break;
228 : }
229 4 : case MHD_HTTP_UNAUTHORIZED:
230 4 : pgr.hr.ec = TALER_JSON_get_error_code (json);
231 4 : pgr.hr.hint = TALER_JSON_get_error_hint (json);
232 4 : break;
233 0 : default:
234 0 : pgr.hr.ec = TALER_JSON_get_error_code (json);
235 0 : pgr.hr.hint = TALER_JSON_get_error_hint (json);
236 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 : "Unexpected response code %u/%d\n",
238 : (unsigned int) response_code,
239 : (int) pgr.hr.ec);
240 0 : break;
241 : }
242 4 : gpph->cb (gpph->cb_cls,
243 : &pgr);
244 4 : TALER_MERCHANT_get_private_products_cancel (gpph);
245 : }
246 :
247 :
248 : struct TALER_MERCHANT_GetPrivateProductsHandle *
249 14 : TALER_MERCHANT_get_private_products_create (
250 : struct GNUNET_CURL_Context *ctx,
251 : const char *url)
252 : {
253 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph;
254 :
255 14 : gpph = GNUNET_new (struct TALER_MERCHANT_GetPrivateProductsHandle);
256 14 : gpph->ctx = ctx;
257 14 : gpph->base_url = GNUNET_strdup (url);
258 14 : gpph->limit = 20;
259 14 : gpph->offset = INT64_MAX;
260 14 : return gpph;
261 : }
262 :
263 :
264 : enum GNUNET_GenericReturnValue
265 0 : TALER_MERCHANT_get_private_products_set_options_ (
266 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph,
267 : unsigned int num_options,
268 : const struct TALER_MERCHANT_GetPrivateProductsOptionValue *options)
269 : {
270 0 : for (unsigned int i = 0; i < num_options; i++)
271 : {
272 0 : const struct TALER_MERCHANT_GetPrivateProductsOptionValue *opt =
273 0 : &options[i];
274 :
275 0 : switch (opt->option)
276 : {
277 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_END:
278 0 : return GNUNET_OK;
279 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_LIMIT:
280 0 : gpph->limit = opt->details.limit;
281 0 : break;
282 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_OFFSET:
283 0 : if (opt->details.offset > INT64_MAX)
284 : {
285 0 : GNUNET_break (0);
286 0 : return GNUNET_NO;
287 : }
288 0 : gpph->offset = opt->details.offset;
289 0 : gpph->have_offset = true;
290 0 : break;
291 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_CATEGORY_FILTER:
292 0 : GNUNET_free (gpph->category_filter);
293 0 : if (NULL != opt->details.category_filter)
294 0 : gpph->category_filter = GNUNET_strdup (opt->details.category_filter);
295 0 : break;
296 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_NAME_FILTER:
297 0 : GNUNET_free (gpph->name_filter);
298 0 : if (NULL != opt->details.name_filter)
299 0 : gpph->name_filter = GNUNET_strdup (opt->details.name_filter);
300 0 : break;
301 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_DESCRIPTION_FILTER:
302 0 : GNUNET_free (gpph->description_filter);
303 0 : if (NULL != opt->details.description_filter)
304 0 : gpph->description_filter =
305 0 : GNUNET_strdup (opt->details.description_filter);
306 0 : break;
307 0 : case TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_PRODUCT_GROUP_SERIAL:
308 0 : gpph->product_group_serial = opt->details.product_group_serial;
309 0 : gpph->have_product_group_serial = true;
310 0 : break;
311 0 : default:
312 0 : GNUNET_break (0);
313 0 : return GNUNET_NO;
314 : }
315 : }
316 0 : return GNUNET_OK;
317 : }
318 :
319 :
320 : enum TALER_ErrorCode
321 14 : TALER_MERCHANT_get_private_products_start (
322 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph,
323 : TALER_MERCHANT_GetPrivateProductsCallback cb,
324 : TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE *cb_cls)
325 : {
326 : CURL *eh;
327 :
328 14 : gpph->cb = cb;
329 14 : gpph->cb_cls = cb_cls;
330 : {
331 : char lbuf[30];
332 : char obuf[30];
333 : char pgsbuf[30];
334 : bool have_offset;
335 :
336 14 : GNUNET_snprintf (lbuf,
337 : sizeof (lbuf),
338 : "%lld",
339 14 : (long long) gpph->limit);
340 14 : GNUNET_snprintf (obuf,
341 : sizeof (obuf),
342 : "%llu",
343 14 : (unsigned long long) gpph->offset);
344 14 : if (gpph->have_product_group_serial)
345 0 : GNUNET_snprintf (pgsbuf,
346 : sizeof (pgsbuf),
347 : "%llu",
348 0 : (unsigned long long) gpph->product_group_serial);
349 14 : if (gpph->limit > 0)
350 : {
351 14 : have_offset = gpph->have_offset
352 14 : && (0 != gpph->offset);
353 : }
354 : else
355 : {
356 0 : have_offset = gpph->have_offset
357 0 : && (INT64_MAX != gpph->offset);
358 : }
359 28 : gpph->url = TALER_url_join (gpph->base_url,
360 : "private/products",
361 : "limit",
362 14 : (20 != gpph->limit)
363 : ? lbuf
364 : : NULL,
365 : "offset",
366 : have_offset
367 : ? obuf
368 : : NULL,
369 : "category_filter",
370 : gpph->category_filter,
371 : "name_filter",
372 : gpph->name_filter,
373 : "description_filter",
374 : gpph->description_filter,
375 : "product_group_serial",
376 14 : gpph->have_product_group_serial
377 : ? pgsbuf
378 : : NULL,
379 : NULL);
380 : }
381 14 : if (NULL == gpph->url)
382 : {
383 0 : GNUNET_break (0);
384 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
385 : }
386 14 : eh = TALER_MERCHANT_curl_easy_get_ (gpph->url);
387 14 : if (NULL == eh)
388 : {
389 0 : GNUNET_break (0);
390 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
391 : }
392 14 : gpph->job = GNUNET_CURL_job_add (gpph->ctx,
393 : eh,
394 : &handle_get_products_finished,
395 : gpph);
396 14 : if (NULL == gpph->job)
397 : {
398 0 : GNUNET_break (0);
399 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
400 : }
401 14 : return TALER_EC_NONE;
402 : }
403 :
404 :
405 : void
406 14 : TALER_MERCHANT_get_private_products_cancel (
407 : struct TALER_MERCHANT_GetPrivateProductsHandle *gpph)
408 : {
409 14 : if (NULL != gpph->job)
410 : {
411 0 : GNUNET_CURL_job_cancel (gpph->job);
412 0 : gpph->job = NULL;
413 : }
414 14 : GNUNET_free (gpph->url);
415 14 : GNUNET_free (gpph->category_filter);
416 14 : GNUNET_free (gpph->name_filter);
417 14 : GNUNET_free (gpph->description_filter);
418 14 : GNUNET_free (gpph->base_url);
419 14 : GNUNET_free (gpph);
420 14 : }
421 :
422 :
423 : /* end of merchant_api_get-private-products.c */
|