LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-patch-webhooks-ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.1 % 54 33
Test Date: 2025-11-06 19:31:41 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   (C) 2022 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify
       6              :   it under the terms of the GNU Affero General Public License as
       7              :   published by the Free Software Foundation; either version 3,
       8              :   or (at your option) any later version.
       9              : 
      10              :   TALER is distributed in the hope that it will be useful, but
      11              :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12              :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13              :   GNU General Public License for more details.
      14              : 
      15              :   You should have received a copy of the GNU General Public
      16              :   License along with TALER; see the file COPYING.  If not,
      17              :   see <http://www.gnu.org/licenses/>
      18              : */
      19              : 
      20              : /**
      21              :  * @file taler-merchant-httpd_private-patch-webhooks-ID.c
      22              :  * @brief implementing PATCH /webhooks/$ID request handling
      23              :  * @author Priscilla HUANG
      24              :  */
      25              : #include "platform.h"
      26              : #include "taler-merchant-httpd_private-patch-webhooks-ID.h"
      27              : #include "taler-merchant-httpd_helper.h"
      28              : #include <taler/taler_json_lib.h>
      29              : 
      30              : 
      31              : /**
      32              :  * How often do we retry the simple INSERT database transaction?
      33              :  */
      34              : #define MAX_RETRIES 3
      35              : 
      36              : 
      37              : /**
      38              :  * Determine the cause of the PATCH failure in more detail and report.
      39              :  *
      40              :  * @param connection connection to report on
      41              :  * @param instance_id instance we are processing
      42              :  * @param webhook_id ID of the webhook to patch
      43              :  * @param wb webhook details we failed to set
      44              :  */
      45              : static MHD_RESULT
      46            2 : determine_cause (struct MHD_Connection *connection,
      47              :                  const char *instance_id,
      48              :                  const char *webhook_id,
      49              :                  const struct TALER_MERCHANTDB_WebhookDetails *wb)
      50              : {
      51              :   struct TALER_MERCHANTDB_WebhookDetails wpx;
      52              :   enum GNUNET_DB_QueryStatus qs;
      53              : 
      54            2 :   qs = TMH_db->lookup_webhook (TMH_db->cls,
      55              :                                instance_id,
      56              :                                webhook_id,
      57              :                                &wpx);
      58            2 :   switch (qs)
      59              :   {
      60            0 :   case GNUNET_DB_STATUS_HARD_ERROR:
      61            0 :     GNUNET_break (0);
      62            0 :     return TALER_MHD_reply_with_error (connection,
      63              :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      64              :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      65              :                                        NULL);
      66            0 :   case GNUNET_DB_STATUS_SOFT_ERROR:
      67            0 :     GNUNET_break (0);
      68            0 :     return TALER_MHD_reply_with_error (connection,
      69              :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      70              :                                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
      71              :                                        "unexpected serialization problem");
      72            2 :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
      73            2 :     return TALER_MHD_reply_with_error (connection,
      74              :                                        MHD_HTTP_NOT_FOUND,
      75              :                                        TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN,
      76              :                                        webhook_id);
      77            0 :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
      78            0 :     break; /* do below */
      79              :   }
      80              : 
      81              :   {
      82              :     enum TALER_ErrorCode ec;
      83              : 
      84            0 :     ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
      85            0 :     TALER_MERCHANTDB_webhook_details_free (&wpx);
      86            0 :     GNUNET_break (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE != ec);
      87            0 :     return TALER_MHD_reply_with_error (connection,
      88              :                                        MHD_HTTP_CONFLICT,
      89              :                                        ec,
      90              :                                        NULL);
      91              :   }
      92              : }
      93              : 
      94              : 
      95              : /**
      96              :  * PATCH configuration of an existing instance, given its configuration.
      97              :  *
      98              :  * @param rh context of the handler
      99              :  * @param connection the MHD connection to handle
     100              :  * @param[in,out] hc context with further information about the request
     101              :  * @return MHD result code
     102              :  */
     103              : MHD_RESULT
     104            4 : TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh,
     105              :                                struct MHD_Connection *connection,
     106              :                                struct TMH_HandlerContext *hc)
     107              : {
     108            4 :   struct TMH_MerchantInstance *mi = hc->instance;
     109            4 :   const char *webhook_id = hc->infix;
     110            4 :   struct TALER_MERCHANTDB_WebhookDetails wb = {0};
     111              :   enum GNUNET_DB_QueryStatus qs;
     112              :   struct GNUNET_JSON_Specification spec[] = {
     113            4 :     GNUNET_JSON_spec_string ("event_type",
     114              :                              (const char **) &wb.event_type),
     115            4 :     TALER_JSON_spec_web_url ("url",
     116              :                              (const char **) &wb.url),
     117            4 :     GNUNET_JSON_spec_string ("http_method",
     118              :                              (const char **) &wb.http_method),
     119            4 :     GNUNET_JSON_spec_mark_optional (
     120              :       GNUNET_JSON_spec_string ("header_template",
     121              :                                (const char **) &wb.header_template),
     122              :       NULL),
     123            4 :     GNUNET_JSON_spec_mark_optional (
     124              :       GNUNET_JSON_spec_string ("body_template",
     125              :                                (const char **) &wb.body_template),
     126              :       NULL),
     127            4 :     GNUNET_JSON_spec_end ()
     128              :   };
     129              : 
     130            4 :   GNUNET_assert (NULL != mi);
     131            4 :   GNUNET_assert (NULL != webhook_id);
     132              :   {
     133              :     enum GNUNET_GenericReturnValue res;
     134              : 
     135            4 :     res = TALER_MHD_parse_json_data (connection,
     136            4 :                                      hc->request_body,
     137              :                                      spec);
     138            4 :     if (GNUNET_OK != res)
     139              :       return (GNUNET_NO == res)
     140              :              ? MHD_YES
     141            0 :              : MHD_NO;
     142              :   }
     143              : 
     144              : 
     145            4 :   qs = TMH_db->update_webhook (TMH_db->cls,
     146            4 :                                mi->settings.id,
     147              :                                webhook_id,
     148              :                                &wb);
     149              :   {
     150            4 :     MHD_RESULT ret = MHD_NO;
     151              : 
     152            4 :     switch (qs)
     153              :     {
     154            0 :     case GNUNET_DB_STATUS_HARD_ERROR:
     155            0 :       GNUNET_break (0);
     156            0 :       ret = TALER_MHD_reply_with_error (connection,
     157              :                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     158              :                                         TALER_EC_GENERIC_DB_STORE_FAILED,
     159              :                                         NULL);
     160            0 :       break;
     161            0 :     case GNUNET_DB_STATUS_SOFT_ERROR:
     162            0 :       GNUNET_break (0);
     163            0 :       ret = TALER_MHD_reply_with_error (connection,
     164              :                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     165              :                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     166              :                                         "unexpected serialization problem");
     167            0 :       break;
     168            2 :     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     169            2 :       ret = determine_cause (connection,
     170            2 :                              mi->settings.id,
     171              :                              webhook_id,
     172              :                              &wb);
     173            2 :       break;
     174            2 :     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     175            2 :       ret = TALER_MHD_reply_static (connection,
     176              :                                     MHD_HTTP_NO_CONTENT,
     177              :                                     NULL,
     178              :                                     NULL,
     179              :                                     0);
     180            2 :       break;
     181              :     }
     182            4 :     GNUNET_JSON_parse_free (spec);
     183            4 :     return ret;
     184              :   }
     185              : }
     186              : 
     187              : 
     188              : /* end of taler-merchant-httpd_private-patch-webhooks-ID.c */
        

Generated by: LCOV version 2.0-1