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