LCOV - code coverage report
Current view: top level - lib - merchant_api_get_product.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 68 82.4 %
Date: 2025-07-10 12:24:40 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014--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 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_product.c
      19             :  * @brief Implementation of the GET /product/$ID 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             :  * Handle for a GET /products/$ID operation.
      36             :  */
      37             : struct TALER_MERCHANT_ProductGetHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_ProductGetCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP GET /products/$ID request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_ProductGetHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param response response body, NULL if not in JSON
      74             :  */
      75             : static void
      76          16 : handle_get_product_finished (void *cls,
      77             :                              long response_code,
      78             :                              const void *response)
      79             : {
      80          16 :   struct TALER_MERCHANT_ProductGetHandle *pgh = cls;
      81          16 :   const json_t *json = response;
      82          16 :   struct TALER_MERCHANT_ProductGetResponse pgr = {
      83          16 :     .hr.http_status = (unsigned int) response_code,
      84             :     .hr.reply = json
      85             :   };
      86             : 
      87          16 :   pgh->job = NULL;
      88          16 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89             :               "Got /products/$ID response with status code %u\n",
      90             :               (unsigned int) response_code);
      91          16 :   switch (response_code)
      92             :   {
      93           4 :   case MHD_HTTP_OK:
      94             :     {
      95             :       struct GNUNET_JSON_Specification spec[] = {
      96           4 :         GNUNET_JSON_spec_string (
      97             :           "product_name",
      98             :           &pgr.details.ok.product_name),
      99           4 :         GNUNET_JSON_spec_string (
     100             :           "description",
     101             :           &pgr.details.ok.description),
     102           4 :         GNUNET_JSON_spec_object_const (
     103             :           "description_i18n",
     104             :           &pgr.details.ok.description_i18n),
     105           4 :         GNUNET_JSON_spec_string (
     106             :           "unit",
     107             :           &pgr.details.ok.unit),
     108           4 :         TALER_JSON_spec_amount_any (
     109             :           "price",
     110             :           &pgr.details.ok.price),
     111           4 :         GNUNET_JSON_spec_mark_optional (
     112             :           GNUNET_JSON_spec_string (
     113             :             "image",
     114             :             &pgr.details.ok.image),
     115             :           NULL),
     116           4 :         GNUNET_JSON_spec_mark_optional (
     117             :           GNUNET_JSON_spec_array_const (
     118             :             "taxes",
     119             :             &pgr.details.ok.taxes),
     120             :           NULL),
     121           4 :         GNUNET_JSON_spec_int64 (
     122             :           "total_stock",
     123             :           &pgr.details.ok.total_stock),
     124           4 :         GNUNET_JSON_spec_uint64 (
     125             :           "total_sold",
     126             :           &pgr.details.ok.total_sold),
     127           4 :         GNUNET_JSON_spec_uint64 (
     128             :           "total_lost",
     129             :           &pgr.details.ok.total_lost),
     130           4 :         GNUNET_JSON_spec_mark_optional (
     131             :           GNUNET_JSON_spec_object_const (
     132             :             "address",
     133             :             &pgr.details.ok.location),
     134             :           NULL),
     135           4 :         GNUNET_JSON_spec_mark_optional (
     136             :           GNUNET_JSON_spec_timestamp (
     137             :             "next_restock",
     138             :             &pgr.details.ok.next_restock),
     139             :           NULL),
     140           4 :         GNUNET_JSON_spec_end ()
     141             :       };
     142             : 
     143           4 :       if (GNUNET_OK ==
     144           4 :           GNUNET_JSON_parse (json,
     145             :                              spec,
     146             :                              NULL, NULL))
     147             :       {
     148           4 :         pgh->cb (pgh->cb_cls,
     149             :                  &pgr);
     150           4 :         GNUNET_JSON_parse_free (spec);
     151           4 :         TALER_MERCHANT_product_get_cancel (pgh);
     152           4 :         return;
     153             :       }
     154           0 :       pgr.hr.http_status = 0;
     155           0 :       pgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     156           0 :       break;
     157             :     }
     158           4 :   case MHD_HTTP_UNAUTHORIZED:
     159           4 :     pgr.hr.ec = TALER_JSON_get_error_code (json);
     160           4 :     pgr.hr.hint = TALER_JSON_get_error_hint (json);
     161             :     /* Nothing really to verify, merchant says we need to authenticate. */
     162           4 :     break;
     163           8 :   case MHD_HTTP_NOT_FOUND:
     164           8 :     pgr.hr.ec = TALER_JSON_get_error_code (json);
     165           8 :     pgr.hr.hint = TALER_JSON_get_error_hint (json);
     166           8 :     break;
     167           0 :   default:
     168             :     /* unexpected response code */
     169           0 :     pgr.hr.ec = TALER_JSON_get_error_code (json);
     170           0 :     pgr.hr.hint = TALER_JSON_get_error_hint (json);
     171           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     172             :                 "Unexpected response code %u/%d\n",
     173             :                 (unsigned int) response_code,
     174             :                 (int) pgr.hr.ec);
     175           0 :     break;
     176             :   }
     177          12 :   pgh->cb (pgh->cb_cls,
     178             :            &pgr);
     179          12 :   TALER_MERCHANT_product_get_cancel (pgh);
     180             : }
     181             : 
     182             : 
     183             : struct TALER_MERCHANT_ProductGetHandle *
     184          16 : TALER_MERCHANT_product_get (
     185             :   struct GNUNET_CURL_Context *ctx,
     186             :   const char *backend_url,
     187             :   const char *product_id,
     188             :   TALER_MERCHANT_ProductGetCallback cb,
     189             :   void *cb_cls)
     190             : {
     191             :   struct TALER_MERCHANT_ProductGetHandle *pgh;
     192             :   CURL *eh;
     193             : 
     194          16 :   pgh = GNUNET_new (struct TALER_MERCHANT_ProductGetHandle);
     195          16 :   pgh->ctx = ctx;
     196          16 :   pgh->cb = cb;
     197          16 :   pgh->cb_cls = cb_cls;
     198             :   {
     199             :     char *path;
     200             : 
     201          16 :     GNUNET_asprintf (&path,
     202             :                      "private/products/%s",
     203             :                      product_id);
     204          16 :     pgh->url = TALER_url_join (backend_url,
     205             :                                path,
     206             :                                NULL);
     207          16 :     GNUNET_free (path);
     208             :   }
     209          16 :   if (NULL == pgh->url)
     210             :   {
     211           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     212             :                 "Could not construct request URL.\n");
     213           0 :     GNUNET_free (pgh);
     214           0 :     return NULL;
     215             :   }
     216          16 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     217             :               "Requesting URL '%s'\n",
     218             :               pgh->url);
     219          16 :   eh = TALER_MERCHANT_curl_easy_get_ (pgh->url);
     220          16 :   pgh->job = GNUNET_CURL_job_add (ctx,
     221             :                                   eh,
     222             :                                   &handle_get_product_finished,
     223             :                                   pgh);
     224          16 :   return pgh;
     225             : }
     226             : 
     227             : 
     228             : void
     229          16 : TALER_MERCHANT_product_get_cancel (
     230             :   struct TALER_MERCHANT_ProductGetHandle *pgh)
     231             : {
     232          16 :   if (NULL != pgh->job)
     233           0 :     GNUNET_CURL_job_cancel (pgh->job);
     234          16 :   GNUNET_free (pgh->url);
     235          16 :   GNUNET_free (pgh);
     236          16 : }

Generated by: LCOV version 1.16