LCOV - code coverage report
Current view: top level - lib - merchant_api_get_webhook.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 63 73.0 %
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_get_webhook.c
      19             :  * @brief Implementation of the GET /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 GET /webhooks/$ID operation.
      36             :  */
      37             : struct TALER_MERCHANT_WebhookGetHandle
      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_WebhookGetCallback 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_WebhookGetHandle`
      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_get_webhook_finished (void *cls,
      77             :                              long response_code,
      78             :                              const void *response)
      79             : {
      80           6 :   struct TALER_MERCHANT_WebhookGetHandle *wgh = 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 :   wgh->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_OK:
      94             :     {
      95             :       const char *event_type;
      96             :       const char *url;
      97             :       const char *http_method;
      98             :       const char *header_template;
      99             :       const char *body_template;
     100           4 :       bool rst_ok = true;
     101             :       struct GNUNET_JSON_Specification spec[] = {
     102           4 :         GNUNET_JSON_spec_string ("event_type",
     103             :                                  &event_type),
     104           4 :         TALER_JSON_spec_web_url ("url",
     105             :                                  &url),
     106           4 :         GNUNET_JSON_spec_string ("http_method",
     107             :                                  &http_method),
     108           4 :         GNUNET_JSON_spec_string ("header_template",
     109             :                                  &header_template),
     110           4 :         GNUNET_JSON_spec_string ("body_template",
     111             :                                  &body_template),
     112           4 :         GNUNET_JSON_spec_end ()
     113             :       };
     114             : 
     115             : 
     116           8 :       if ( (rst_ok) &&
     117             :            (GNUNET_OK ==
     118           4 :             GNUNET_JSON_parse (json,
     119             :                                spec,
     120             :                                NULL, NULL)) )
     121             :       {
     122           4 :         wgh->cb (wgh->cb_cls,
     123             :                  &hr,
     124             :                  event_type,
     125             :                  url,
     126             :                  http_method,
     127             :                  header_template,
     128             :                  body_template);
     129           4 :         GNUNET_JSON_parse_free (spec);
     130           4 :         TALER_MERCHANT_webhook_get_cancel (wgh);
     131           4 :         return;
     132             :       }
     133           0 :       hr.http_status = 0;
     134           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     135           0 :       GNUNET_JSON_parse_free (spec);
     136           0 :       break;
     137             :     }
     138           0 :   case MHD_HTTP_UNAUTHORIZED:
     139           0 :     hr.ec = TALER_JSON_get_error_code (json);
     140           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     141             :     /* Nothing really to verify, merchant says we need to authenticate. */
     142           0 :     break;
     143           2 :   case MHD_HTTP_NOT_FOUND:
     144           2 :     hr.ec = TALER_JSON_get_error_code (json);
     145           2 :     hr.hint = TALER_JSON_get_error_hint (json);
     146           2 :     break;
     147           0 :   default:
     148             :     /* unexpected response code */
     149           0 :     hr.ec = TALER_JSON_get_error_code (json);
     150           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     151           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     152             :                 "Unexpected response code %u/%d\n",
     153             :                 (unsigned int) response_code,
     154             :                 (int) hr.ec);
     155           0 :     break;
     156             :   }
     157           2 :   wgh->cb (wgh->cb_cls,
     158             :            &hr,
     159             :            NULL,
     160             :            NULL,
     161             :            NULL,
     162             :            NULL,
     163             :            NULL);
     164           2 :   TALER_MERCHANT_webhook_get_cancel (wgh);
     165             : }
     166             : 
     167             : 
     168             : struct TALER_MERCHANT_WebhookGetHandle *
     169           6 : TALER_MERCHANT_webhook_get (
     170             :   struct GNUNET_CURL_Context *ctx,
     171             :   const char *backend_url,
     172             :   const char *webhook_id,
     173             :   TALER_MERCHANT_WebhookGetCallback cb,
     174             :   void *cb_cls)
     175             : {
     176             :   struct TALER_MERCHANT_WebhookGetHandle *wgh;
     177             :   CURL *eh;
     178             : 
     179           6 :   wgh = GNUNET_new (struct TALER_MERCHANT_WebhookGetHandle);
     180           6 :   wgh->ctx = ctx;
     181           6 :   wgh->cb = cb;
     182           6 :   wgh->cb_cls = cb_cls;
     183             :   {
     184             :     char *path;
     185             : 
     186           6 :     GNUNET_asprintf (&path,
     187             :                      "private/webhooks/%s",
     188             :                      webhook_id);
     189           6 :     wgh->url = TALER_url_join (backend_url,
     190             :                                path,
     191             :                                NULL);
     192           6 :     GNUNET_free (path);
     193             :   }
     194           6 :   if (NULL == wgh->url)
     195             :   {
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     197             :                 "Could not construct request URL.\n");
     198           0 :     GNUNET_free (wgh);
     199           0 :     return NULL;
     200             :   }
     201           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     202             :               "Requesting URL '%s'\n",
     203             :               wgh->url);
     204           6 :   eh = TALER_MERCHANT_curl_easy_get_ (wgh->url);
     205           6 :   wgh->job = GNUNET_CURL_job_add (ctx,
     206             :                                   eh,
     207             :                                   &handle_get_webhook_finished,
     208             :                                   wgh);
     209           6 :   return wgh;
     210             : }
     211             : 
     212             : 
     213             : void
     214           6 : TALER_MERCHANT_webhook_get_cancel (
     215             :   struct TALER_MERCHANT_WebhookGetHandle *wgh)
     216             : {
     217           6 :   if (NULL != wgh->job)
     218           0 :     GNUNET_CURL_job_cancel (wgh->job);
     219           6 :   GNUNET_free (wgh->url);
     220           6 :   GNUNET_free (wgh);
     221           6 : }

Generated by: LCOV version 1.16