LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_product.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 50.6 % 81 41
Test Date: 2025-10-21 13:10:50 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020, 2021 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 merchant_api_patch_product.c
      21              :  * @brief Implementation of the PATCH /products/$ID request
      22              :  *        of the merchant's HTTP API
      23              :  * @author Christian Grothoff
      24              :  */
      25              : #include "platform.h"
      26              : #include <curl/curl.h>
      27              : #include <jansson.h>
      28              : #include <microhttpd.h> /* just for HTTP status codes */
      29              : #include <gnunet/gnunet_util_lib.h>
      30              : #include "taler_merchant_service.h"
      31              : #include "merchant_api_common.h"
      32              : #include "merchant_api_curl_defaults.h"
      33              : #include <taler/taler_json_lib.h>
      34              : #include <taler/taler_curl_lib.h>
      35              : 
      36              : 
      37              : /**
      38              :  * Handle for a PATCH /products/$ID operation.
      39              :  */
      40              : struct TALER_MERCHANT_ProductPatchHandle
      41              : {
      42              : 
      43              :   /**
      44              :    * The url for this request.
      45              :    */
      46              :   char *url;
      47              : 
      48              :   /**
      49              :    * Handle for the request.
      50              :    */
      51              :   struct GNUNET_CURL_Job *job;
      52              : 
      53              :   /**
      54              :    * Function to call with the result.
      55              :    */
      56              :   TALER_MERCHANT_ProductPatchCallback cb;
      57              : 
      58              :   /**
      59              :    * Closure for @a cb.
      60              :    */
      61              :   void *cb_cls;
      62              : 
      63              :   /**
      64              :    * Reference to the execution context.
      65              :    */
      66              :   struct GNUNET_CURL_Context *ctx;
      67              : 
      68              :   /**
      69              :    * Minor context that holds body and headers.
      70              :    */
      71              :   struct TALER_CURL_PostContext post_ctx;
      72              : 
      73              : };
      74              : 
      75              : 
      76              : /**
      77              :  * Function called when we're done processing the
      78              :  * HTTP PATCH /products/$ID request.
      79              :  *
      80              :  * @param cls the `struct TALER_MERCHANT_ProductPatchHandle`
      81              :  * @param response_code HTTP response code, 0 on error
      82              :  * @param response response body, NULL if not in JSON
      83              :  */
      84              : static void
      85            6 : handle_patch_product_finished (void *cls,
      86              :                                long response_code,
      87              :                                const void *response)
      88              : {
      89            6 :   struct TALER_MERCHANT_ProductPatchHandle *pph = cls;
      90            6 :   const json_t *json = response;
      91            6 :   struct TALER_MERCHANT_HttpResponse hr = {
      92            6 :     .http_status = (unsigned int) response_code,
      93              :     .reply = json
      94              :   };
      95              : 
      96            6 :   pph->job = NULL;
      97            6 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      98              :               "PATCH /products/$ID completed with response code %u\n",
      99              :               (unsigned int) response_code);
     100            6 :   switch (response_code)
     101              :   {
     102            0 :   case 0:
     103            0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     104            0 :     break;
     105            4 :   case MHD_HTTP_NO_CONTENT:
     106            4 :     break;
     107            0 :   case MHD_HTTP_BAD_REQUEST:
     108            0 :     hr.ec = TALER_JSON_get_error_code (json);
     109            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110            0 :     GNUNET_break_op (0);
     111              :     /* This should never happen, either us
     112              :      * or the merchant is buggy (or API version conflict);
     113              :      * just pass JSON reply to the application */
     114            0 :     break;
     115            0 :   case MHD_HTTP_UNAUTHORIZED:
     116            0 :     hr.ec = TALER_JSON_get_error_code (json);
     117            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     118              :     /* Nothing really to verify, merchant says we need to authenticate. */
     119            0 :     break;
     120            0 :   case MHD_HTTP_FORBIDDEN:
     121            0 :     hr.ec = TALER_JSON_get_error_code (json);
     122            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123              :     /* Nothing really to verify, merchant says we tried to abort the payment
     124              :      * after it was successful. We should pass the JSON reply to the
     125              :      * application */
     126            0 :     break;
     127            2 :   case MHD_HTTP_NOT_FOUND:
     128            2 :     hr.ec = TALER_JSON_get_error_code (json);
     129            2 :     hr.hint = TALER_JSON_get_error_hint (json);
     130            2 :     break;
     131            0 :   case MHD_HTTP_CONFLICT:
     132            0 :     hr.ec = TALER_JSON_get_error_code (json);
     133            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     134            0 :     break;
     135            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     136            0 :     hr.ec = TALER_JSON_get_error_code (json);
     137            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     138              :     /* Server had an internal issue; we should retry,
     139              :        but this API leaves this to the application */
     140            0 :     break;
     141            0 :   default:
     142            0 :     TALER_MERCHANT_parse_error_details_ (json,
     143              :                                          response_code,
     144              :                                          &hr);
     145              :     /* unexpected response code */
     146            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147              :                 "Unexpected response code %u/%d\n",
     148              :                 (unsigned int) response_code,
     149              :                 (int) hr.ec);
     150            0 :     GNUNET_break_op (0);
     151            0 :     break;
     152              :   }
     153            6 :   pph->cb (pph->cb_cls,
     154              :            &hr);
     155            6 :   TALER_MERCHANT_product_patch_cancel (pph);
     156            6 : }
     157              : 
     158              : 
     159              : struct TALER_MERCHANT_ProductPatchHandle *
     160            6 : TALER_MERCHANT_product_patch (
     161              :   struct GNUNET_CURL_Context *ctx,
     162              :   const char *backend_url,
     163              :   const char *product_id,
     164              :   const char *description,
     165              :   const json_t *description_i18n,
     166              :   const char *unit,
     167              :   const struct TALER_Amount *price,
     168              :   const char *image,
     169              :   const json_t *taxes,
     170              :   int64_t total_stock,
     171              :   uint64_t total_lost,
     172              :   const json_t *address,
     173              :   struct GNUNET_TIME_Timestamp next_restock,
     174              :   TALER_MERCHANT_ProductPatchCallback cb,
     175              :   void *cb_cls)
     176              : {
     177              :   struct TALER_MERCHANT_ProductPatchHandle *pph;
     178              :   json_t *req_obj;
     179              : 
     180            6 :   req_obj = GNUNET_JSON_PACK (
     181              :     /* FIXME: once we move to the new-style API,
     182              :        allow applications to set the product name properly! */
     183              :     GNUNET_JSON_pack_string ("product_name",
     184              :                              description),
     185              :     GNUNET_JSON_pack_string ("description",
     186              :                              description),
     187              :     GNUNET_JSON_pack_object_incref ("description_i18n",
     188              :                                     (json_t *) description_i18n),
     189              :     GNUNET_JSON_pack_string ("unit",
     190              :                              unit),
     191              :     TALER_JSON_pack_amount ("price",
     192              :                             price),
     193              :     GNUNET_JSON_pack_string ("image",
     194              :                              image),
     195              :     GNUNET_JSON_pack_array_incref ("taxes",
     196              :                                    (json_t *) taxes),
     197              :     GNUNET_JSON_pack_uint64 ("total_stock",
     198              :                              total_stock),
     199              :     GNUNET_JSON_pack_uint64 ("total_lost",
     200              :                              total_lost),
     201              :     GNUNET_JSON_pack_object_incref ("address",
     202              :                                     (json_t *) address),
     203              :     GNUNET_JSON_pack_timestamp ("next_restock",
     204              :                                 next_restock));
     205            6 :   pph = GNUNET_new (struct TALER_MERCHANT_ProductPatchHandle);
     206            6 :   pph->ctx = ctx;
     207            6 :   pph->cb = cb;
     208            6 :   pph->cb_cls = cb_cls;
     209              :   {
     210              :     char *path;
     211              : 
     212            6 :     GNUNET_asprintf (&path,
     213              :                      "private/products/%s",
     214              :                      product_id);
     215            6 :     pph->url = TALER_url_join (backend_url,
     216              :                                path,
     217              :                                NULL);
     218            6 :     GNUNET_free (path);
     219              :   }
     220            6 :   if (NULL == pph->url)
     221              :   {
     222            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     223              :                 "Could not construct request URL.\n");
     224            0 :     json_decref (req_obj);
     225            0 :     GNUNET_free (pph);
     226            0 :     return NULL;
     227              :   }
     228              :   {
     229              :     CURL *eh;
     230              : 
     231            6 :     eh = TALER_MERCHANT_curl_easy_get_ (pph->url);
     232            6 :     if (GNUNET_OK !=
     233            6 :         TALER_curl_easy_post (&pph->post_ctx,
     234              :                               eh,
     235              :                               req_obj))
     236              :     {
     237            0 :       GNUNET_break (0);
     238            0 :       curl_easy_cleanup (eh);
     239            0 :       json_decref (req_obj);
     240            0 :       GNUNET_free (pph);
     241            0 :       return NULL;
     242              :     }
     243            6 :     json_decref (req_obj);
     244            6 :     GNUNET_assert (CURLE_OK ==
     245              :                    curl_easy_setopt (eh,
     246              :                                      CURLOPT_CUSTOMREQUEST,
     247              :                                      MHD_HTTP_METHOD_PATCH));
     248           12 :     pph->job = GNUNET_CURL_job_add2 (ctx,
     249              :                                      eh,
     250            6 :                                      pph->post_ctx.headers,
     251              :                                      &handle_patch_product_finished,
     252              :                                      pph);
     253              :   }
     254            6 :   return pph;
     255              : }
     256              : 
     257              : 
     258              : void
     259            6 : TALER_MERCHANT_product_patch_cancel (
     260              :   struct TALER_MERCHANT_ProductPatchHandle *pph)
     261              : {
     262            6 :   if (NULL != pph->job)
     263              :   {
     264            0 :     GNUNET_CURL_job_cancel (pph->job);
     265            0 :     pph->job = NULL;
     266              :   }
     267            6 :   TALER_curl_easy_post_finished (&pph->post_ctx);
     268            6 :   GNUNET_free (pph->url);
     269            6 :   GNUNET_free (pph);
     270            6 : }
     271              : 
     272              : 
     273              : /* end of merchant_api_patch_product.c */
        

Generated by: LCOV version 2.0-1