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: 2021-04-12 06:04:58 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 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 response 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             : struct TALER_MERCHANT_ProductDeleteHandle *
     119           0 : TALER_MERCHANT_product_delete (
     120             :   struct GNUNET_CURL_Context *ctx,
     121             :   const char *backend_url,
     122             :   const char *product_id,
     123             :   TALER_MERCHANT_ProductDeleteCallback cb,
     124             :   void *cb_cls)
     125             : {
     126             :   struct TALER_MERCHANT_ProductDeleteHandle *pdh;
     127             : 
     128           0 :   pdh = GNUNET_new (struct TALER_MERCHANT_ProductDeleteHandle);
     129           0 :   pdh->ctx = ctx;
     130           0 :   pdh->cb = cb;
     131           0 :   pdh->cb_cls = cb_cls;
     132             :   {
     133             :     char *path;
     134             : 
     135           0 :     GNUNET_asprintf (&path,
     136             :                      "private/products/%s",
     137             :                      product_id);
     138           0 :     pdh->url = TALER_url_join (backend_url,
     139             :                                path,
     140             :                                NULL);
     141           0 :     GNUNET_free (path);
     142             :   }
     143           0 :   if (NULL == pdh->url)
     144             :   {
     145           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     146             :                 "Could not construct request URL.\n");
     147           0 :     GNUNET_free (pdh);
     148           0 :     return NULL;
     149             :   }
     150           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     151             :               "Requesting URL '%s'\n",
     152             :               pdh->url);
     153             :   {
     154             :     CURL *eh;
     155             : 
     156           0 :     eh = curl_easy_init ();
     157           0 :     GNUNET_assert (CURLE_OK ==
     158             :                    curl_easy_setopt (eh,
     159             :                                      CURLOPT_URL,
     160             :                                      pdh->url));
     161           0 :     GNUNET_assert (CURLE_OK ==
     162             :                    curl_easy_setopt (eh,
     163             :                                      CURLOPT_CUSTOMREQUEST,
     164             :                                      MHD_HTTP_METHOD_DELETE));
     165           0 :     pdh->job = GNUNET_CURL_job_add (ctx,
     166             :                                     eh,
     167             :                                     &handle_delete_product_finished,
     168             :                                     pdh);
     169             :   }
     170           0 :   return pdh;
     171             : }
     172             : 
     173             : 
     174             : void
     175           0 : TALER_MERCHANT_product_delete_cancel (
     176             :   struct TALER_MERCHANT_ProductDeleteHandle *pdh)
     177             : {
     178           0 :   if (NULL != pdh->job)
     179           0 :     GNUNET_CURL_job_cancel (pdh->job);
     180           0 :   GNUNET_free (pdh->url);
     181           0 :   GNUNET_free (pdh);
     182           0 : }

Generated by: LCOV version 1.14