taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c (6764B)
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 src/backend/taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c 22 * @brief implementing PATCH /webhooks/$ID request handling 23 * @author Priscilla HUANG 24 */ 25 #include "platform.h" 26 #include "taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.h" 27 #include "taler-merchant-httpd_helper.h" 28 #include <taler/taler_json_lib.h> 29 #include "merchant-database/lookup_webhook.h" 30 #include "merchant-database/update_webhook.h" 31 32 33 /** 34 * How often do we retry the simple INSERT database transaction? 35 */ 36 #define MAX_RETRIES 3 37 38 39 /** 40 * Determine the cause of the PATCH failure in more detail and report. 41 * 42 * @param connection connection to report on 43 * @param instance_id instance we are processing 44 * @param webhook_id ID of the webhook to patch 45 * @param wb webhook details we failed to set 46 */ 47 static enum MHD_Result 48 determine_cause (struct MHD_Connection *connection, 49 const char *instance_id, 50 const char *webhook_id, 51 const struct TALER_MERCHANTDB_WebhookDetails *wb) 52 { 53 struct TALER_MERCHANTDB_WebhookDetails wpx; 54 enum GNUNET_DB_QueryStatus qs; 55 56 qs = TALER_MERCHANTDB_lookup_webhook (TMH_db, 57 instance_id, 58 webhook_id, 59 &wpx); 60 switch (qs) 61 { 62 case GNUNET_DB_STATUS_HARD_ERROR: 63 GNUNET_break (0); 64 return TALER_MHD_reply_with_error (connection, 65 MHD_HTTP_INTERNAL_SERVER_ERROR, 66 TALER_EC_GENERIC_DB_FETCH_FAILED, 67 NULL); 68 case GNUNET_DB_STATUS_SOFT_ERROR: 69 GNUNET_break (0); 70 return TALER_MHD_reply_with_error (connection, 71 MHD_HTTP_INTERNAL_SERVER_ERROR, 72 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 73 "unexpected serialization problem"); 74 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 75 return TALER_MHD_reply_with_error (connection, 76 MHD_HTTP_NOT_FOUND, 77 TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN, 78 webhook_id); 79 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 80 break; /* do below */ 81 } 82 83 { 84 enum TALER_ErrorCode ec; 85 86 ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 87 TALER_MERCHANTDB_webhook_details_free (&wpx); 88 GNUNET_break (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE != ec); 89 return TALER_MHD_reply_with_error (connection, 90 MHD_HTTP_CONFLICT, 91 ec, 92 NULL); 93 } 94 } 95 96 97 /** 98 * PATCH configuration of an existing instance, given its configuration. 99 * 100 * @param rh context of the handler 101 * @param connection the MHD connection to handle 102 * @param[in,out] hc context with further information about the request 103 * @return MHD result code 104 */ 105 enum MHD_Result 106 TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh, 107 struct MHD_Connection *connection, 108 struct TMH_HandlerContext *hc) 109 { 110 struct TMH_MerchantInstance *mi = hc->instance; 111 const char *webhook_id = hc->infix; 112 struct TALER_MERCHANTDB_WebhookDetails wb = {0}; 113 enum GNUNET_DB_QueryStatus qs; 114 struct GNUNET_JSON_Specification spec[] = { 115 GNUNET_JSON_spec_string ("event_type", 116 (const char **) &wb.event_type), 117 TALER_JSON_spec_web_url ("url", 118 (const char **) &wb.url), 119 GNUNET_JSON_spec_string ("http_method", 120 (const char **) &wb.http_method), 121 GNUNET_JSON_spec_mark_optional ( 122 GNUNET_JSON_spec_string ("header_template", 123 (const char **) &wb.header_template), 124 NULL), 125 GNUNET_JSON_spec_mark_optional ( 126 GNUNET_JSON_spec_string ("body_template", 127 (const char **) &wb.body_template), 128 NULL), 129 GNUNET_JSON_spec_end () 130 }; 131 132 GNUNET_assert (NULL != mi); 133 GNUNET_assert (NULL != webhook_id); 134 { 135 enum GNUNET_GenericReturnValue res; 136 137 res = TALER_MHD_parse_json_data (connection, 138 hc->request_body, 139 spec); 140 if (GNUNET_OK != res) 141 return (GNUNET_NO == res) 142 ? MHD_YES 143 : MHD_NO; 144 } 145 146 147 qs = TALER_MERCHANTDB_update_webhook (TMH_db, 148 mi->settings.id, 149 webhook_id, 150 &wb); 151 { 152 enum MHD_Result ret = MHD_NO; 153 154 switch (qs) 155 { 156 case GNUNET_DB_STATUS_HARD_ERROR: 157 GNUNET_break (0); 158 ret = TALER_MHD_reply_with_error (connection, 159 MHD_HTTP_INTERNAL_SERVER_ERROR, 160 TALER_EC_GENERIC_DB_STORE_FAILED, 161 NULL); 162 break; 163 case GNUNET_DB_STATUS_SOFT_ERROR: 164 GNUNET_break (0); 165 ret = TALER_MHD_reply_with_error (connection, 166 MHD_HTTP_INTERNAL_SERVER_ERROR, 167 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 168 "unexpected serialization problem"); 169 break; 170 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 171 ret = determine_cause (connection, 172 mi->settings.id, 173 webhook_id, 174 &wb); 175 break; 176 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 177 ret = TALER_MHD_reply_static (connection, 178 MHD_HTTP_NO_CONTENT, 179 NULL, 180 NULL, 181 0); 182 break; 183 } 184 GNUNET_JSON_parse_free (spec); 185 return ret; 186 } 187 } 188 189 190 /* end of taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c */