LCOV - code coverage report
Current view: top level - lib - merchant_api_delete_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 42 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 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_order.c
      19             :  * @brief Implementation of the DELETE /orders/$ORDER_ID request of the merchant's HTTP API
      20             :  * @author Jonathan Buchanan
      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             :  * Handle for a DELETE /orders/$ID operation.
      34             :  */
      35             : struct TALER_MERCHANT_OrderDeleteHandle
      36             : {
      37             :   /**
      38             :    * The url for this request.
      39             :    */
      40             :   char *url;
      41             : 
      42             :   /**
      43             :    * Handle for the request.
      44             :    */
      45             :   struct GNUNET_CURL_Job *job;
      46             : 
      47             :   /**
      48             :    * Function to call with the result.
      49             :    */
      50             :   TALER_MERCHANT_OrderDeleteCallback cb;
      51             : 
      52             :   /**
      53             :    * Closure for @a cb.
      54             :    */
      55             :   void *cb_cls;
      56             : 
      57             :   /**
      58             :    * Reference to the execution context.
      59             :    */
      60             :   struct GNUNET_CURL_Context *ctx;
      61             : };
      62             : 
      63             : 
      64             : /**
      65             :  * Function called when we're done processing the
      66             :  * HTTP DELETE /orders/$ORDER_ID request.
      67             :  *
      68             :  * @param cls the `struct TALER_MERCHANT_OrderDeleteHandle`
      69             :  * @param response_code HTTP response code, 0 on error
      70             :  * @param json response body, NULL if not in JSON
      71             :  */
      72             : static void
      73           0 : handle_delete_order_finished (void *cls,
      74             :                               long response_code,
      75             :                               const void *response)
      76             : {
      77           0 :   struct TALER_MERCHANT_OrderDeleteHandle *odh = cls;
      78           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      79           0 :     .http_status = (unsigned int) response_code,
      80             :     .reply = NULL,
      81             :   };
      82             : 
      83           0 :   odh->job = NULL;
      84           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      85             :               "Got /orders/$ID response with status code %u\n",
      86             :               (unsigned int) response_code);
      87           0 :   switch (response_code)
      88             :   {
      89           0 :   case MHD_HTTP_NO_CONTENT:
      90           0 :     break;
      91           0 :   case MHD_HTTP_NOT_FOUND:
      92           0 :     break;
      93           0 :   case MHD_HTTP_CONFLICT:
      94           0 :     break;
      95           0 :   default:
      96             :     /* unexpected response code */
      97           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      98             :                 "Unexpected response code %u\n",
      99             :                 (unsigned int) response_code);
     100           0 :     break;
     101             :   }
     102           0 :   odh->cb (odh->cb_cls,
     103             :            &hr);
     104           0 :   TALER_MERCHANT_order_delete_cancel (odh);
     105           0 : }
     106             : 
     107             : 
     108             : /**
     109             :  * Make a DELETE /orders/$ID request to delete a order from our
     110             :  * inventory.
     111             :  *
     112             :  * @param ctx the context
     113             :  * @param backend_url HTTP base URL for the backend
     114             :  * @param order_id identifier of the order
     115             :  * @param cb function to call with the backend's deletion status
     116             :  * @param cb_cls closure for @a cb
     117             :  * @return the request handle; NULL upon error
     118             :  */
     119             : struct TALER_MERCHANT_OrderDeleteHandle *
     120           0 : TALER_MERCHANT_order_delete (
     121             :   struct GNUNET_CURL_Context *ctx,
     122             :   const char *backend_url,
     123             :   const char *order_id,
     124             :   TALER_MERCHANT_OrderDeleteCallback cb,
     125             :   void *cb_cls)
     126             : {
     127             :   struct TALER_MERCHANT_OrderDeleteHandle *odh;
     128             : 
     129           0 :   odh = GNUNET_new (struct TALER_MERCHANT_OrderDeleteHandle);
     130           0 :   odh->ctx = ctx;
     131           0 :   odh->cb = cb;
     132           0 :   odh->cb_cls = cb_cls;
     133             :   {
     134             :     char *path;
     135             : 
     136           0 :     GNUNET_asprintf (&path,
     137             :                      "private/orders/%s",
     138             :                      order_id);
     139             : 
     140           0 :     odh->url = TALER_url_join (backend_url,
     141             :                                path,
     142             :                                NULL);
     143           0 :     GNUNET_free (path);
     144             :   }
     145           0 :   if (NULL == odh->url)
     146             :   {
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                 "Could not construct request url.\n");
     149           0 :     GNUNET_free (odh);
     150           0 :     return NULL;
     151             :   }
     152             : 
     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             :                                      odh->url));
     161           0 :     GNUNET_assert (CURLE_OK ==
     162             :                    curl_easy_setopt (eh,
     163             :                                      CURLOPT_CUSTOMREQUEST,
     164             :                                      MHD_HTTP_METHOD_DELETE));
     165           0 :     odh->job = GNUNET_CURL_job_add (ctx,
     166             :                                     eh,
     167             :                                     &handle_delete_order_finished,
     168             :                                     odh);
     169             :   }
     170           0 :   return odh;
     171             : }
     172             : 
     173             : 
     174             : /**
     175             :  * Cancel DELETE /orders/$ID operation.
     176             :  *
     177             :  * @param odh operation to cancel
     178             :  */
     179             : void
     180           0 : TALER_MERCHANT_order_delete_cancel (
     181             :   struct TALER_MERCHANT_OrderDeleteHandle *odh)
     182             : {
     183           0 :   if (NULL != odh->job)
     184           0 :     GNUNET_CURL_job_cancel (odh->job);
     185           0 :   GNUNET_free (odh->url);
     186           0 :   GNUNET_free (odh);
     187           0 : }

Generated by: LCOV version 1.14