LCOV - code coverage report
Current view: top level - lib - merchant_api_get_webhooks.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 74 66.2 %
Date: 2025-06-23 16:22:09 Functions: 4 4 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_webhooks.c
      19             :  * @brief Implementation of the GET /webhooks 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             :  * Maximum number of webhooks we return.
      36             :  */
      37             : #define MAX_WEBHOOKS 1024
      38             : 
      39             : /**
      40             :  * Handle for a GET /webhooks operation.
      41             :  */
      42             : struct TALER_MERCHANT_WebhooksGetHandle
      43             : {
      44             :   /**
      45             :    * The url for this request.
      46             :    */
      47             :   char *url;
      48             : 
      49             :   /**
      50             :    * Handle for the request.
      51             :    */
      52             :   struct GNUNET_CURL_Job *job;
      53             : 
      54             :   /**
      55             :    * Function to call with the result.
      56             :    */
      57             :   TALER_MERCHANT_WebhooksGetCallback cb;
      58             : 
      59             :   /**
      60             :    * Closure for @a cb.
      61             :    */
      62             :   void *cb_cls;
      63             : 
      64             :   /**
      65             :    * Reference to the execution context.
      66             :    */
      67             :   struct GNUNET_CURL_Context *ctx;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Parse webhook information from @a ia.
      74             :  *
      75             :  * @param ia JSON array (or NULL!) with webhook data
      76             :  * @param[in] wgr partially filled webhook response
      77             :  * @param wgh operation handle
      78             :  * @return #GNUNET_OK on success
      79             :  */
      80             : static enum GNUNET_GenericReturnValue
      81           4 : parse_webhooks (const json_t *ia,
      82             :                 struct TALER_MERCHANT_WebhooksGetResponse *wgr,
      83             :                 struct TALER_MERCHANT_WebhooksGetHandle *wgh)
      84             : {
      85           4 :   unsigned int whook_len = (unsigned int) json_array_size (ia);
      86             : 
      87           4 :   if ( (json_array_size (ia) != (size_t)  whook_len) ||
      88             :        (whook_len > MAX_WEBHOOKS) )
      89             :   {
      90           0 :     GNUNET_break (0);
      91           0 :     return GNUNET_SYSERR;
      92             :   }
      93           4 :   {
      94           4 :     struct TALER_MERCHANT_WebhookEntry whook[GNUNET_NZL (whook_len)];
      95             :     size_t index;
      96             :     json_t *value;
      97             : 
      98           6 :     json_array_foreach (ia, index, value) {
      99           2 :       struct TALER_MERCHANT_WebhookEntry *ie = &whook[index];
     100             :       struct GNUNET_JSON_Specification spec[] = {
     101           2 :         GNUNET_JSON_spec_string ("webhook_id",
     102             :                                  &ie->webhook_id),
     103           2 :         GNUNET_JSON_spec_end ()
     104             :       };
     105             : 
     106           2 :       if (GNUNET_OK !=
     107           2 :           GNUNET_JSON_parse (value,
     108             :                              spec,
     109             :                              NULL, NULL))
     110             :       {
     111           0 :         GNUNET_break_op (0);
     112           0 :         return GNUNET_SYSERR;
     113             :       }
     114             :     }
     115           4 :     wgr->details.ok.webhooks_length = whook_len;
     116           4 :     wgr->details.ok.webhooks = whook;
     117           4 :     wgh->cb (wgh->cb_cls,
     118             :              wgr);
     119           4 :     wgh->cb = NULL; /* just to be sure */
     120             :   }
     121           4 :   return GNUNET_OK;
     122             : }
     123             : 
     124             : 
     125             : /**
     126             :  * Function called when we're done processing the
     127             :  * HTTP /webhooks request.
     128             :  *
     129             :  * @param cls the `struct TALER_MERCHANT_WebhooksGetHandle`
     130             :  * @param response_code HTTP response code, 0 on error
     131             :  * @param response response body, NULL if not in JSON
     132             :  */
     133             : static void
     134           4 : handle_get_webhooks_finished (void *cls,
     135             :                               long response_code,
     136             :                               const void *response)
     137             : {
     138           4 :   struct TALER_MERCHANT_WebhooksGetHandle *wgh = cls;
     139           4 :   const json_t *json = response;
     140           4 :   struct TALER_MERCHANT_WebhooksGetResponse wgr = {
     141           4 :     .hr.http_status = (unsigned int) response_code,
     142             :     .hr.reply = json
     143             :   };
     144             : 
     145           4 :   wgh->job = NULL;
     146           4 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     147             :               "Got /webhooks response with status code %u\n",
     148             :               (unsigned int) response_code);
     149           4 :   switch (response_code)
     150             :   {
     151           4 :   case MHD_HTTP_OK:
     152             :     {
     153             :       const json_t *webhooks;
     154             :       struct GNUNET_JSON_Specification spec[] = {
     155           4 :         GNUNET_JSON_spec_array_const ("webhooks",
     156             :                                       &webhooks),
     157           4 :         GNUNET_JSON_spec_end ()
     158             :       };
     159             : 
     160           4 :       if (GNUNET_OK !=
     161           4 :           GNUNET_JSON_parse (json,
     162             :                              spec,
     163             :                              NULL, NULL))
     164             :       {
     165           0 :         wgr.hr.http_status = 0;
     166           0 :         wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     167           0 :         break;
     168             :       }
     169           4 :       if (GNUNET_OK ==
     170           4 :           parse_webhooks (webhooks,
     171             :                           &wgr,
     172             :                           wgh))
     173             :       {
     174           4 :         TALER_MERCHANT_webhooks_get_cancel (wgh);
     175           4 :         return;
     176             :       }
     177           0 :       wgr.hr.http_status = 0;
     178           0 :       wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     179           0 :       break;
     180             :     }
     181           0 :   case MHD_HTTP_UNAUTHORIZED:
     182           0 :     wgr.hr.ec = TALER_JSON_get_error_code (json);
     183           0 :     wgr.hr.hint = TALER_JSON_get_error_hint (json);
     184             :     /* Nothing really to verify, merchant says we need to authenticate. */
     185           0 :     break;
     186           0 :   default:
     187             :     /* unexpected response code */
     188           0 :     wgr.hr.ec = TALER_JSON_get_error_code (json);
     189           0 :     wgr.hr.hint = TALER_JSON_get_error_hint (json);
     190           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     191             :                 "Unexpected response code %u/%d\n",
     192             :                 (unsigned int) response_code,
     193             :                 (int) wgr.hr.ec);
     194           0 :     break;
     195             :   }
     196           0 :   wgh->cb (wgh->cb_cls,
     197             :            &wgr);
     198           0 :   TALER_MERCHANT_webhooks_get_cancel (wgh);
     199             : }
     200             : 
     201             : 
     202             : struct TALER_MERCHANT_WebhooksGetHandle *
     203           4 : TALER_MERCHANT_webhooks_get (
     204             :   struct GNUNET_CURL_Context *ctx,
     205             :   const char *backend_url,
     206             :   TALER_MERCHANT_WebhooksGetCallback cb,
     207             :   void *cb_cls)
     208             : {
     209             :   struct TALER_MERCHANT_WebhooksGetHandle *wgh;
     210             :   CURL *eh;
     211             : 
     212           4 :   wgh = GNUNET_new (struct TALER_MERCHANT_WebhooksGetHandle);
     213           4 :   wgh->ctx = ctx;
     214           4 :   wgh->cb = cb;
     215           4 :   wgh->cb_cls = cb_cls;
     216           4 :   wgh->url = TALER_url_join (backend_url,
     217             :                              "private/webhooks",
     218             :                              NULL);
     219           4 :   if (NULL == wgh->url)
     220             :   {
     221           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     222             :                 "Could not construct request URL.\n");
     223           0 :     GNUNET_free (wgh);
     224           0 :     return NULL;
     225             :   }
     226           4 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     227             :               "Requesting URL '%s'\n",
     228             :               wgh->url);
     229           4 :   eh = TALER_MERCHANT_curl_easy_get_ (wgh->url);
     230           4 :   wgh->job = GNUNET_CURL_job_add (ctx,
     231             :                                   eh,
     232             :                                   &handle_get_webhooks_finished,
     233             :                                   wgh);
     234           4 :   return wgh;
     235             : }
     236             : 
     237             : 
     238             : void
     239           4 : TALER_MERCHANT_webhooks_get_cancel (
     240             :   struct TALER_MERCHANT_WebhooksGetHandle *wgh)
     241             : {
     242           4 :   if (NULL != wgh->job)
     243           0 :     GNUNET_CURL_job_cancel (wgh->job);
     244           4 :   GNUNET_free (wgh->url);
     245           4 :   GNUNET_free (wgh);
     246           4 : }

Generated by: LCOV version 1.16