LCOV - code coverage report
Current view: top level - lib - merchant_api_delete_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 50 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) 2014-2018, 2020 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 lib/merchant_api_delete_product.c
      19             :  * @brief Implementation of the DELETE /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 <taler/taler_json_lib.h>
      30             : #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Handle for a DELETE /products/$ID operation.
      35             :  */
      36             : struct TALER_MERCHANT_ProductDeleteHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_ProductDeleteCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP GET /products/$ID request.
      69             :  *
      70             :  * @param cls the `struct TALER_MERCHANT_ProductDeleteHandle`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param json response body, NULL if not in JSON
      73             :  */
      74             : static void
      75           0 : handle_delete_product_finished (void *cls,
      76             :                                 long response_code,
      77             :                                 const void *response)
      78             : {
      79           0 :   struct TALER_MERCHANT_ProductDeleteHandle *pdh = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   pdh->job = NULL;
      87           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      88             :               "Got /products/$ID response with status code %u\n",
      89             :               (unsigned int) response_code);
      90           0 :   switch (response_code)
      91             :   {
      92           0 :   case MHD_HTTP_NO_CONTENT:
      93           0 :     break;
      94           0 :   case MHD_HTTP_NOT_FOUND:
      95           0 :     hr.ec = TALER_JSON_get_error_code (json);
      96           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      97           0 :     break;
      98           0 :   case MHD_HTTP_CONFLICT:
      99           0 :     hr.ec = TALER_JSON_get_error_code (json);
     100           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     101           0 :     break;
     102           0 :   default:
     103             :     /* unexpected response code */
     104           0 :     hr.ec = TALER_JSON_get_error_code (json);
     105           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     106           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     107             :                 "Unexpected response code %u/%d\n",
     108             :                 (unsigned int) response_code,
     109             :                 (int) hr.ec);
     110           0 :     break;
     111             :   }
     112           0 :   pdh->cb (pdh->cb_cls,
     113             :            &hr);
     114           0 :   TALER_MERCHANT_product_delete_cancel (pdh);
     115           0 : }
     116             : 
     117             : 
     118             : /**
     119             :  * Make a DELETE /products/$ID request to delete a product from our
     120             :  * inventory.
     121             :  *
     122             :  * @param ctx the context
     123             :  * @param backend_url HTTP base URL for the backend
     124             :  * @param product_id identifier of the product
     125             :  * @param cb function to call with the backend's deletion status
     126             :  * @param cb_cls closure for @a cb
     127             :  * @return the request handle; NULL upon error
     128             :  */
     129             : struct TALER_MERCHANT_ProductDeleteHandle *
     130           0 : TALER_MERCHANT_product_delete (
     131             :   struct GNUNET_CURL_Context *ctx,
     132             :   const char *backend_url,
     133             :   const char *product_id,
     134             :   TALER_MERCHANT_ProductDeleteCallback cb,
     135             :   void *cb_cls)
     136             : {
     137             :   struct TALER_MERCHANT_ProductDeleteHandle *pdh;
     138             : 
     139           0 :   pdh = GNUNET_new (struct TALER_MERCHANT_ProductDeleteHandle);
     140           0 :   pdh->ctx = ctx;
     141           0 :   pdh->cb = cb;
     142           0 :   pdh->cb_cls = cb_cls;
     143             :   {
     144             :     char *path;
     145             : 
     146           0 :     GNUNET_asprintf (&path,
     147             :                      "private/products/%s",
     148             :                      product_id);
     149           0 :     pdh->url = TALER_url_join (backend_url,
     150             :                                path,
     151             :                                NULL);
     152           0 :     GNUNET_free (path);
     153             :   }
     154           0 :   if (NULL == pdh->url)
     155             :   {
     156           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     157             :                 "Could not construct request URL.\n");
     158           0 :     GNUNET_free (pdh);
     159           0 :     return NULL;
     160             :   }
     161           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     162             :               "Requesting URL '%s'\n",
     163             :               pdh->url);
     164             :   {
     165             :     CURL *eh;
     166             : 
     167           0 :     eh = curl_easy_init ();
     168           0 :     GNUNET_assert (CURLE_OK ==
     169             :                    curl_easy_setopt (eh,
     170             :                                      CURLOPT_URL,
     171             :                                      pdh->url));
     172           0 :     GNUNET_assert (CURLE_OK ==
     173             :                    curl_easy_setopt (eh,
     174             :                                      CURLOPT_CUSTOMREQUEST,
     175             :                                      MHD_HTTP_METHOD_DELETE));
     176           0 :     pdh->job = GNUNET_CURL_job_add (ctx,
     177             :                                     eh,
     178             :                                     &handle_delete_product_finished,
     179             :                                     pdh);
     180             :   }
     181           0 :   return pdh;
     182             : }
     183             : 
     184             : 
     185             : /**
     186             :  * Cancel DELETE /product/$ID request.  Must not be called by clients after
     187             :  * the callback was invoked.
     188             :  *
     189             :  * @param pdh request to cancel.
     190             :  */
     191             : void
     192           0 : TALER_MERCHANT_product_delete_cancel (
     193             :   struct TALER_MERCHANT_ProductDeleteHandle *pdh)
     194             : {
     195           0 :   if (NULL != pdh->job)
     196           0 :     GNUNET_CURL_job_cancel (pdh->job);
     197           0 :   GNUNET_free (pdh->url);
     198           0 :   GNUNET_free (pdh);
     199           0 : }

Generated by: LCOV version 1.14