LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 81 0.0 %
Date: 2020-08-15 06:12:35 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 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 lib/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 <taler/taler_json_lib.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a PATCH /products/$ID operation.
      37             :  */
      38             : struct TALER_MERCHANT_ProductPatchHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_ProductPatchCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : 
      66             :   /**
      67             :    * Minor context that holds body and headers.
      68             :    */
      69             :   struct TALER_CURL_PostContext post_ctx;
      70             : 
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * HTTP PATCH /products/$ID request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_ProductPatchHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param json response body, NULL if not in JSON
      81             :  */
      82             : static void
      83           0 : handle_patch_product_finished (void *cls,
      84             :                                long response_code,
      85             :                                const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_ProductPatchHandle *pph = cls;
      88           0 :   const json_t *json = response;
      89           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      90           0 :     .http_status = (unsigned int) response_code,
      91             :     .reply = json
      92             :   };
      93             : 
      94           0 :   pph->job = NULL;
      95           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      96             :               "PATCH /products/$ID completed with response code %u\n",
      97             :               (unsigned int) response_code);
      98           0 :   switch (response_code)
      99             :   {
     100           0 :   case 0:
     101           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     102           0 :     break;
     103           0 :   case MHD_HTTP_NO_CONTENT:
     104           0 :     break;
     105           0 :   case MHD_HTTP_BAD_REQUEST:
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108           0 :     GNUNET_break_op (0);
     109             :     /* This should never happen, either us
     110             :      * or the merchant is buggy (or API version conflict);
     111             :      * just pass JSON reply to the application */
     112           0 :     break;
     113           0 :   case MHD_HTTP_FORBIDDEN:
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116             :     /* Nothing really to verify, merchant says we tried to abort the payment
     117             :      * after it was successful. We should pass the JSON reply to the
     118             :      * application */
     119           0 :     break;
     120           0 :   case MHD_HTTP_NOT_FOUND:
     121           0 :     hr.ec = TALER_JSON_get_error_code (json);
     122           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123           0 :     break;
     124           0 :   case MHD_HTTP_CONFLICT:
     125           0 :     hr.ec = TALER_JSON_get_error_code (json);
     126           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     127           0 :     break;
     128           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     129           0 :     hr.ec = TALER_JSON_get_error_code (json);
     130           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     131             :     /* Server had an internal issue; we should retry,
     132             :        but this API leaves this to the application */
     133           0 :     break;
     134           0 :   default:
     135           0 :     TALER_MERCHANT_parse_error_details_ (json,
     136             :                                          response_code,
     137             :                                          &hr);
     138             :     /* unexpected response code */
     139           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                 "Unexpected response code %u/%d\n",
     141             :                 (unsigned int) response_code,
     142             :                 (int) hr.ec);
     143           0 :     GNUNET_break_op (0);
     144           0 :     break;
     145             :   }
     146           0 :   pph->cb (pph->cb_cls,
     147             :            &hr);
     148           0 :   TALER_MERCHANT_product_patch_cancel (pph);
     149           0 : }
     150             : 
     151             : 
     152             : /**
     153             :  * Make a PATCH /products request to update product details in the
     154             :  * inventory.
     155             :  *
     156             :  * @param ctx the context
     157             :  * @param backend_url HTTP base URL for the backend
     158             :  * @param product_id identifier to use for the product; the product must exist,
     159             :  *                    or the transaction will fail with a #MHD_HTTP_NOT_FOUND
     160             :  *                    HTTP status code
     161             :  * @param description description of the product
     162             :  * @param description_i18n Map from IETF BCP 47 language tags to localized descriptions
     163             :  * @param unit unit in which the product is measured (liters, kilograms, packages, etc.)
     164             :  * @param price the price for one @a unit of the product, zero is used to imply that
     165             :  *              this product is not sold separately or that the price is not fixed and
     166             :  *              must be supplied by the front-end.  If non-zero, price must include
     167             :  *              applicable taxes.
     168             :  * @param image base64-encoded product image
     169             :  * @param taxes list of taxes paid by the merchant
     170             :  * @param total_stock in @a units, -1 to indicate "infinite" (i.e. electronic books),
     171             :  *               must be larger than previous values
     172             :  * @param total_lost in @a units, must be larger than previous values, and may
     173             :  *               not exceed total_stock minus total_sold; if it does, the transaction
     174             :  *               will fail with a #MHD_HTTP_CONFLICT HTTP status code
     175             :  * @param location where the product is in stock
     176             :  * @param next_restock when the next restocking is expected to happen
     177             :  * @param cb function to call with the backend's result
     178             :  * @param cb_cls closure for @a cb
     179             :  * @return the request handle; NULL upon error
     180             :  */
     181             : struct TALER_MERCHANT_ProductPatchHandle *
     182           0 : TALER_MERCHANT_product_patch (
     183             :   struct GNUNET_CURL_Context *ctx,
     184             :   const char *backend_url,
     185             :   const char *product_id,
     186             :   const char *description,
     187             :   const json_t *description_i18n,
     188             :   const char *unit,
     189             :   const struct TALER_Amount *price,
     190             :   const json_t *image,
     191             :   const json_t *taxes,
     192             :   int64_t total_stock,
     193             :   uint64_t total_lost,
     194             :   const json_t *address,
     195             :   struct GNUNET_TIME_Absolute next_restock,
     196             :   TALER_MERCHANT_ProductPatchCallback cb,
     197             :   void *cb_cls)
     198             : {
     199             :   struct TALER_MERCHANT_ProductPatchHandle *pph;
     200             :   json_t *req_obj;
     201             : 
     202           0 :   (void) GNUNET_TIME_round_abs (&next_restock);
     203           0 :   req_obj = json_pack ("{s:s, s:O, s:s, s:o, s:O,"
     204             :                        " s:O, s:I: s:I, s:O, s:o}",
     205             :                        "description",
     206             :                        description,
     207             :                        "description_i18n",
     208             :                        description_i18n,
     209             :                        "unit",
     210             :                        unit,
     211             :                        "price",
     212             :                        TALER_JSON_from_amount (price),
     213             :                        "image",
     214             :                        image,
     215             :                        /* End of first group of 5 */
     216             :                        "taxes",
     217             :                        taxes,
     218             :                        "total_stock",
     219             :                        (json_int_t) total_stock,
     220             :                        "total_lost",
     221             :                        (json_int_t) total_lost,
     222             :                        "address",
     223             :                        address,
     224             :                        "next_restock",
     225             :                        GNUNET_JSON_from_time_abs (next_restock));
     226           0 :   if (NULL == req_obj)
     227             :   {
     228           0 :     GNUNET_break (0);
     229           0 :     return NULL;
     230             :   }
     231           0 :   pph = GNUNET_new (struct TALER_MERCHANT_ProductPatchHandle);
     232           0 :   pph->ctx = ctx;
     233           0 :   pph->cb = cb;
     234           0 :   pph->cb_cls = cb_cls;
     235             :   {
     236             :     char *path;
     237             : 
     238           0 :     GNUNET_asprintf (&path,
     239             :                      "private/products/%s",
     240             :                      product_id);
     241           0 :     pph->url = TALER_url_join (backend_url,
     242             :                                path,
     243             :                                NULL);
     244           0 :     GNUNET_free (path);
     245             :   }
     246           0 :   if (NULL == pph->url)
     247             :   {
     248           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     249             :                 "Could not construct request URL.\n");
     250           0 :     json_decref (req_obj);
     251           0 :     GNUNET_free (pph);
     252           0 :     return NULL;
     253             :   }
     254             :   {
     255             :     CURL *eh;
     256             : 
     257           0 :     eh = curl_easy_init ();
     258           0 :     if (GNUNET_OK !=
     259           0 :         TALER_curl_easy_post (&pph->post_ctx,
     260             :                               eh,
     261             :                               req_obj))
     262             :     {
     263           0 :       GNUNET_break (0);
     264           0 :       json_decref (req_obj);
     265           0 :       GNUNET_free (pph);
     266           0 :       return NULL;
     267             :     }
     268             : 
     269           0 :     json_decref (req_obj);
     270           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     271             :                                                  CURLOPT_URL,
     272             :                                                  pph->url));
     273           0 :     GNUNET_assert (CURLE_OK ==
     274             :                    curl_easy_setopt (eh,
     275             :                                      CURLOPT_CUSTOMREQUEST,
     276             :                                      MHD_HTTP_METHOD_PATCH));
     277           0 :     pph->job = GNUNET_CURL_job_add2 (ctx,
     278             :                                      eh,
     279           0 :                                      pph->post_ctx.headers,
     280             :                                      &handle_patch_product_finished,
     281             :                                      pph);
     282             :   }
     283           0 :   return pph;
     284             : }
     285             : 
     286             : 
     287             : /**
     288             :  * Cancel PATCH /products/$ID request.  Must not be called by clients after
     289             :  * the callback was invoked.
     290             :  *
     291             :  * @param pph request to cancel.
     292             :  */
     293             : void
     294           0 : TALER_MERCHANT_product_patch_cancel (
     295             :   struct TALER_MERCHANT_ProductPatchHandle *pph)
     296             : {
     297           0 :   if (NULL != pph->job)
     298             :   {
     299           0 :     GNUNET_CURL_job_cancel (pph->job);
     300           0 :     pph->job = NULL;
     301             :   }
     302           0 :   TALER_curl_easy_post_finished (&pph->post_ctx);
     303           0 :   GNUNET_free (pph->url);
     304           0 :   GNUNET_free (pph);
     305           0 : }
     306             : 
     307             : 
     308             : /* end of merchant_api_patch_product.c */

Generated by: LCOV version 1.14