LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_product.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 81 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14