LCOV - code coverage report
Current view: top level - lib - merchant_api_delete_webhook.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 53 67.9 %
Date: 2025-06-23 16:22:09 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2022 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_template.c
      19             :  * @brief Implementation of the DELETE /webhooks/$ID request of the merchant's HTTP API
      20             :  * @author Priscilla HUANG
      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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a DELETE /webhooks/$ID operation.
      36             :  */
      37             : struct TALER_MERCHANT_WebhookDeleteHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_WebhookDeleteCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP GET /webhooks/$ID request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_WebhookDeleteHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param response response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           6 : handle_delete_webhook_finished (void *cls,
      77             :                                 long response_code,
      78             :                                 const void *response)
      79             : {
      80           6 :   struct TALER_MERCHANT_WebhookDeleteHandle *wdh = cls;
      81           6 :   const json_t *json = response;
      82           6 :   struct TALER_MERCHANT_HttpResponse hr = {
      83           6 :     .http_status = (unsigned int) response_code,
      84             :     .reply = json
      85             :   };
      86             : 
      87           6 :   wdh->job = NULL;
      88           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89             :               "Got /webhooks/$ID response with status code %u\n",
      90             :               (unsigned int) response_code);
      91           6 :   switch (response_code)
      92             :   {
      93           4 :   case MHD_HTTP_NO_CONTENT:
      94           4 :     break;
      95           0 :   case MHD_HTTP_UNAUTHORIZED:
      96           0 :     hr.ec = TALER_JSON_get_error_code (json);
      97           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      98             :     /* Nothing really to verify, merchant says we need to authenticate. */
      99           0 :     break;
     100           2 :   case MHD_HTTP_NOT_FOUND:
     101           2 :     hr.ec = TALER_JSON_get_error_code (json);
     102           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     103           2 :     break;
     104           0 :   case MHD_HTTP_CONFLICT:
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107           0 :     break;
     108           0 :   default:
     109             :     /* unexpected response code */
     110           0 :     hr.ec = TALER_JSON_get_error_code (json);
     111           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     112           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     113             :                 "Unexpected response code %u/%d\n",
     114             :                 (unsigned int) response_code,
     115             :                 (int) hr.ec);
     116           0 :     break;
     117             :   }
     118           6 :   wdh->cb (wdh->cb_cls,
     119             :            &hr);
     120           6 :   TALER_MERCHANT_webhook_delete_cancel (wdh);
     121           6 : }
     122             : 
     123             : 
     124             : struct TALER_MERCHANT_WebhookDeleteHandle *
     125           6 : TALER_MERCHANT_webhook_delete (
     126             :   struct GNUNET_CURL_Context *ctx,
     127             :   const char *backend_url,
     128             :   const char *webhook_id,
     129             :   TALER_MERCHANT_WebhookDeleteCallback cb,
     130             :   void *cb_cls)
     131             : {
     132             :   struct TALER_MERCHANT_WebhookDeleteHandle *wdh;
     133             : 
     134           6 :   wdh = GNUNET_new (struct TALER_MERCHANT_WebhookDeleteHandle);
     135           6 :   wdh->ctx = ctx;
     136           6 :   wdh->cb = cb;
     137           6 :   wdh->cb_cls = cb_cls;
     138             :   {
     139             :     char *path;
     140             : 
     141           6 :     GNUNET_asprintf (&path,
     142             :                      "private/webhooks/%s",
     143             :                      webhook_id);
     144           6 :     wdh->url = TALER_url_join (backend_url,
     145             :                                path,
     146             :                                NULL);
     147           6 :     GNUNET_free (path);
     148             :   }
     149           6 :   if (NULL == wdh->url)
     150             :   {
     151           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                 "Could not construct request URL.\n");
     153           0 :     GNUNET_free (wdh);
     154           0 :     return NULL;
     155             :   }
     156           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     157             :               "Requesting URL '%s'\n",
     158             :               wdh->url);
     159             :   {
     160             :     CURL *eh;
     161             : 
     162           6 :     eh = TALER_MERCHANT_curl_easy_get_ (wdh->url);
     163           6 :     GNUNET_assert (CURLE_OK ==
     164             :                    curl_easy_setopt (eh,
     165             :                                      CURLOPT_CUSTOMREQUEST,
     166             :                                      MHD_HTTP_METHOD_DELETE));
     167           6 :     wdh->job = GNUNET_CURL_job_add (ctx,
     168             :                                     eh,
     169             :                                     &handle_delete_webhook_finished,
     170             :                                     wdh);
     171             :   }
     172           6 :   return wdh;
     173             : }
     174             : 
     175             : 
     176             : void
     177           6 : TALER_MERCHANT_webhook_delete_cancel (
     178             :   struct TALER_MERCHANT_WebhookDeleteHandle *wdh)
     179             : {
     180           6 :   if (NULL != wdh->job)
     181           0 :     GNUNET_CURL_job_cancel (wdh->job);
     182           6 :   GNUNET_free (wdh->url);
     183           6 :   GNUNET_free (wdh);
     184           6 : }

Generated by: LCOV version 1.16