LCOV - code coverage report
Current view: top level - lib - merchant_api_get_products.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.4 % 79 58
Test Date: 2025-11-13 17:46:01 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-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 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 merchant_api_get_products.c
      19              :  * @brief Implementation of the GET /products request of the merchant's HTTP API
      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_service.h"
      29              : #include "merchant_api_curl_defaults.h"
      30              : #include <taler/taler_json_lib.h>
      31              : #include <taler/taler_signatures.h>
      32              : 
      33              : 
      34              : /**
      35              :  * Maximum number of products we return.
      36              :  */
      37              : #define MAX_PRODUCTS 1024
      38              : 
      39              : 
      40              : /**
      41              :  * Handle for a GET /products operation.
      42              :  */
      43              : struct TALER_MERCHANT_ProductsGetHandle
      44              : {
      45              :   /**
      46              :    * The url for this request.
      47              :    */
      48              :   char *url;
      49              : 
      50              :   /**
      51              :    * Handle for the request.
      52              :    */
      53              :   struct GNUNET_CURL_Job *job;
      54              : 
      55              :   /**
      56              :    * Function to call with the result.
      57              :    */
      58              :   TALER_MERCHANT_ProductsGetCallback cb;
      59              : 
      60              :   /**
      61              :    * Closure for @a cb.
      62              :    */
      63              :   void *cb_cls;
      64              : 
      65              :   /**
      66              :    * Reference to the execution context.
      67              :    */
      68              :   struct GNUNET_CURL_Context *ctx;
      69              : 
      70              : };
      71              : 
      72              : 
      73              : /**
      74              :  * Parse product information from @a ia.
      75              :  *
      76              :  * @param json overall JSON reply
      77              :  * @param ia JSON array (or NULL!) with product data
      78              :  * @param pgh operation handle
      79              :  * @return #GNUNET_OK on success
      80              :  */
      81              : static enum GNUNET_GenericReturnValue
      82           10 : parse_products (const json_t *json,
      83              :                 const json_t *ia,
      84              :                 struct TALER_MERCHANT_ProductsGetHandle *pgh)
      85              : {
      86           10 :   unsigned int ies_len = json_array_size (ia);
      87              : 
      88           10 :   if ( (json_array_size (ia) != (size_t)  ies_len) ||
      89              :        (ies_len > MAX_PRODUCTS) )
      90              :   {
      91            0 :     GNUNET_break (0);
      92            0 :     return GNUNET_SYSERR;
      93              :   }
      94           10 :   {
      95           10 :     struct TALER_MERCHANT_InventoryEntry ies[GNUNET_NZL (ies_len)];
      96              :     size_t index;
      97              :     json_t *value;
      98              :     enum GNUNET_GenericReturnValue ret;
      99              : 
     100           10 :     ret = GNUNET_OK;
     101           12 :     json_array_foreach (ia, index, value) {
     102            2 :       struct TALER_MERCHANT_InventoryEntry *ie = &ies[index];
     103              :       struct GNUNET_JSON_Specification spec[] = {
     104            2 :         GNUNET_JSON_spec_string ("product_id",
     105              :                                  &ie->product_id),
     106            2 :         GNUNET_JSON_spec_uint64 ("product_serial",
     107              :                                  &ie->product_serial),
     108            2 :         GNUNET_JSON_spec_end ()
     109              :       };
     110              : 
     111            2 :       if (GNUNET_OK !=
     112            2 :           GNUNET_JSON_parse (value,
     113              :                              spec,
     114              :                              NULL, NULL))
     115              :       {
     116            0 :         GNUNET_break_op (0);
     117            0 :         ret = GNUNET_SYSERR;
     118            0 :         continue;
     119              :       }
     120            2 :       if (GNUNET_SYSERR == ret)
     121            0 :         break;
     122              :     }
     123           10 :     if (GNUNET_OK == ret)
     124              :     {
     125           10 :       struct TALER_MERCHANT_GetProductsResponse gpr = {
     126              :         .hr.http_status = MHD_HTTP_OK,
     127              :         .hr.reply = json,
     128              :         .details.ok.products_length = ies_len,
     129              :         .details.ok.products = ies
     130              :       };
     131              : 
     132           10 :       pgh->cb (pgh->cb_cls,
     133              :                &gpr);
     134           10 :       pgh->cb = NULL; /* just to be sure */
     135              :     }
     136           10 :     return ret;
     137              :   }
     138              : }
     139              : 
     140              : 
     141              : /**
     142              :  * Function called when we're done processing the
     143              :  * HTTP /products request.
     144              :  *
     145              :  * @param cls the `struct TALER_MERCHANT_ProductsGetHandle`
     146              :  * @param response_code HTTP response code, 0 on error
     147              :  * @param response response body, NULL if not in JSON
     148              :  */
     149              : static void
     150           14 : handle_get_products_finished (void *cls,
     151              :                               long response_code,
     152              :                               const void *response)
     153              : {
     154           14 :   struct TALER_MERCHANT_ProductsGetHandle *pgh = cls;
     155           14 :   const json_t *json = response;
     156           14 :   struct TALER_MERCHANT_GetProductsResponse gpr = {
     157           14 :     .hr.http_status = (unsigned int) response_code,
     158              :     .hr.reply = json
     159              :   };
     160              : 
     161           14 :   pgh->job = NULL;
     162           14 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     163              :               "Got /products response with status code %u\n",
     164              :               (unsigned int) response_code);
     165           14 :   switch (response_code)
     166              :   {
     167           10 :   case MHD_HTTP_OK:
     168              :     {
     169              :       const json_t *products;
     170              :       struct GNUNET_JSON_Specification spec[] = {
     171           10 :         GNUNET_JSON_spec_array_const ("products",
     172              :                                       &products),
     173           10 :         GNUNET_JSON_spec_end ()
     174              :       };
     175              : 
     176           10 :       if (GNUNET_OK !=
     177           10 :           GNUNET_JSON_parse (json,
     178              :                              spec,
     179              :                              NULL, NULL))
     180              :       {
     181            0 :         gpr.hr.http_status = 0;
     182            0 :         gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     183            0 :         break;
     184              :       }
     185           10 :       if (GNUNET_OK ==
     186           10 :           parse_products (json,
     187              :                           products,
     188              :                           pgh))
     189              :       {
     190           10 :         TALER_MERCHANT_products_get_cancel (pgh);
     191           10 :         return;
     192              :       }
     193            0 :       gpr.hr.http_status = 0;
     194            0 :       gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     195            0 :       break;
     196              :     }
     197            4 :   case MHD_HTTP_UNAUTHORIZED:
     198            4 :     gpr.hr.ec = TALER_JSON_get_error_code (json);
     199            4 :     gpr.hr.hint = TALER_JSON_get_error_hint (json);
     200              :     /* Nothing really to verify, merchant says we need to authenticate. */
     201            4 :     break;
     202            0 :   default:
     203              :     /* unexpected response code */
     204            0 :     gpr.hr.ec = TALER_JSON_get_error_code (json);
     205            0 :     gpr.hr.hint = TALER_JSON_get_error_hint (json);
     206            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     207              :                 "Unexpected response code %u/%d\n",
     208              :                 (unsigned int) response_code,
     209              :                 (int) gpr.hr.ec);
     210            0 :     break;
     211              :   }
     212            4 :   pgh->cb (pgh->cb_cls,
     213              :            &gpr);
     214            4 :   TALER_MERCHANT_products_get_cancel (pgh);
     215              : }
     216              : 
     217              : 
     218              : struct TALER_MERCHANT_ProductsGetHandle *
     219           14 : TALER_MERCHANT_products_get (
     220              :   struct GNUNET_CURL_Context *ctx,
     221              :   const char *backend_url,
     222              :   TALER_MERCHANT_ProductsGetCallback cb,
     223              :   void *cb_cls)
     224              : {
     225              :   struct TALER_MERCHANT_ProductsGetHandle *pgh;
     226              :   CURL *eh;
     227              : 
     228           14 :   pgh = GNUNET_new (struct TALER_MERCHANT_ProductsGetHandle);
     229           14 :   pgh->ctx = ctx;
     230           14 :   pgh->cb = cb;
     231           14 :   pgh->cb_cls = cb_cls;
     232           14 :   pgh->url = TALER_url_join (backend_url,
     233              :                              "private/products",
     234              :                              NULL);
     235           14 :   if (NULL == pgh->url)
     236              :   {
     237            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     238              :                 "Could not construct request URL.\n");
     239            0 :     GNUNET_free (pgh);
     240            0 :     return NULL;
     241              :   }
     242           14 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     243              :               "Requesting URL '%s'\n",
     244              :               pgh->url);
     245           14 :   eh = TALER_MERCHANT_curl_easy_get_ (pgh->url);
     246           14 :   pgh->job = GNUNET_CURL_job_add (ctx,
     247              :                                   eh,
     248              :                                   &handle_get_products_finished,
     249              :                                   pgh);
     250           14 :   return pgh;
     251              : }
     252              : 
     253              : 
     254              : void
     255           14 : TALER_MERCHANT_products_get_cancel (
     256              :   struct TALER_MERCHANT_ProductsGetHandle *pgh)
     257              : {
     258           14 :   if (NULL != pgh->job)
     259            0 :     GNUNET_CURL_job_cancel (pgh->job);
     260           14 :   GNUNET_free (pgh->url);
     261           14 :   GNUNET_free (pgh);
     262           14 : }
        

Generated by: LCOV version 2.0-1