LCOV - code coverage report
Current view: top level - lib - merchant_api_lock_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 79 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_lock_product.c
      21             :  * @brief Implementation of the POST /products/$ID/lock 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 POST /products/$ID/lock operation.
      37             :  */
      38             : struct TALER_MERCHANT_ProductLockHandle
      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_ProductLockCallback 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 POST /products/$ID/lock request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_ProductLockHandle`
      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_lock_product_finished (void *cls,
      84             :                               long response_code,
      85             :                               const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_ProductLockHandle *plh = 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 :   plh->job = NULL;
      95           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      96             :               "LOCK /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_GONE:
     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 :   plh->cb (plh->cb_cls,
     147             :            &hr);
     148           0 :   TALER_MERCHANT_product_lock_cancel (plh);
     149           0 : }
     150             : 
     151             : 
     152             : /**
     153             :  * Make a POST /products/$ID/lock request to reserve a certain
     154             :  * amount of product in inventory to a reservation UUID.
     155             :  *
     156             :  * @param ctx the context
     157             :  * @param backend_url HTTP base URL for the backend
     158             :  * @param product_id identifier of the product
     159             :  * @param uuid UUID that identifies the client holding the lock
     160             :  * @param duration how long should the lock be held
     161             :  * @param quantity how much product should be locked
     162             :  * @param cb function to call with the backend's lock status
     163             :  * @param cb_cls closure for @a cb
     164             :  * @return the request handle; NULL upon error
     165             :  */
     166             : struct TALER_MERCHANT_ProductLockHandle *
     167           0 : TALER_MERCHANT_product_lock (
     168             :   struct GNUNET_CURL_Context *ctx,
     169             :   const char *backend_url,
     170             :   const char *product_id,
     171             :   const struct GNUNET_Uuid *uuid,
     172             :   struct GNUNET_TIME_Relative duration,
     173             :   uint32_t quantity,
     174             :   TALER_MERCHANT_ProductLockCallback cb,
     175             :   void *cb_cls)
     176             : {
     177             :   struct TALER_MERCHANT_ProductLockHandle *plh;
     178             :   json_t *req_obj;
     179             : 
     180           0 :   req_obj = json_pack ("{s:o, s:o, s:I}",
     181             :                        "lock_uuid",
     182             :                        GNUNET_JSON_from_data_auto (uuid),
     183             :                        "duration",
     184             :                        GNUNET_JSON_from_time_rel (duration),
     185             :                        "quantity",
     186             :                        (json_int_t) quantity);
     187           0 :   if (NULL == req_obj)
     188             :   {
     189           0 :     GNUNET_break (0);
     190           0 :     return NULL;
     191             :   }
     192           0 :   plh = GNUNET_new (struct TALER_MERCHANT_ProductLockHandle);
     193           0 :   plh->ctx = ctx;
     194           0 :   plh->cb = cb;
     195           0 :   plh->cb_cls = cb_cls;
     196             :   {
     197             :     char *path;
     198             : 
     199           0 :     GNUNET_asprintf (&path,
     200             :                      "private/products/%s/lock",
     201             :                      product_id);
     202           0 :     plh->url = TALER_url_join (backend_url,
     203             :                                path,
     204             :                                NULL);
     205           0 :     GNUNET_free (path);
     206             :   }
     207           0 :   if (NULL == plh->url)
     208             :   {
     209           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     210             :                 "Could not construct request URL.\n");
     211           0 :     json_decref (req_obj);
     212           0 :     GNUNET_free (plh);
     213           0 :     return NULL;
     214             :   }
     215             :   {
     216             :     CURL *eh;
     217             : 
     218           0 :     eh = curl_easy_init ();
     219           0 :     if (GNUNET_OK !=
     220           0 :         TALER_curl_easy_post (&plh->post_ctx,
     221             :                               eh,
     222             :                               req_obj))
     223             :     {
     224           0 :       GNUNET_break (0);
     225           0 :       json_decref (req_obj);
     226           0 :       GNUNET_free (plh);
     227           0 :       return NULL;
     228             :     }
     229             : 
     230           0 :     json_decref (req_obj);
     231           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     232             :                                                  CURLOPT_URL,
     233             :                                                  plh->url));
     234           0 :     plh->job = GNUNET_CURL_job_add2 (ctx,
     235             :                                      eh,
     236           0 :                                      plh->post_ctx.headers,
     237             :                                      &handle_lock_product_finished,
     238             :                                      plh);
     239             :   }
     240           0 :   return plh;
     241             : }
     242             : 
     243             : 
     244             : /**
     245             :  * Cancel POST /products/$ID/lock request.  Must not be called by clients after
     246             :  * the callback was invoked.
     247             :  *
     248             :  * @param plh request to cancel.
     249             :  */
     250             : void
     251           0 : TALER_MERCHANT_product_lock_cancel (
     252             :   struct TALER_MERCHANT_ProductLockHandle *plh)
     253             : {
     254           0 :   if (NULL != plh->job)
     255             :   {
     256           0 :     GNUNET_CURL_job_cancel (plh->job);
     257           0 :     plh->job = NULL;
     258             :   }
     259           0 :   TALER_curl_easy_post_finished (&plh->post_ctx);
     260           0 :   GNUNET_free (plh->url);
     261           0 :   GNUNET_free (plh);
     262           0 : }
     263             : 
     264             : 
     265             : /* end of merchant_api_lock_product.c */

Generated by: LCOV version 1.14