merchant_api_get-private-webhooks-WEBHOOK_ID.c (6702B)
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_get-private-webhooks-WEBHOOK_ID-new.c 19 * @brief Implementation of the GET /private/webhooks/$WEBHOOK_ID request 20 * @author Priscilla Huang 21 * @author Christian Grothoff 22 */ 23 #include "taler/platform.h" 24 #include <curl/curl.h> 25 #include <jansson.h> 26 #include <microhttpd.h> /* just for HTTP status codes */ 27 #include <gnunet/gnunet_util_lib.h> 28 #include <gnunet/gnunet_curl_lib.h> 29 #include <taler/taler-merchant/get-private-webhooks-WEBHOOK_ID.h> 30 #include "merchant_api_curl_defaults.h" 31 #include <taler/taler_json_lib.h> 32 33 34 /** 35 * Handle for a GET /private/webhooks/$WEBHOOK_ID operation. 36 */ 37 struct TALER_MERCHANT_GetPrivateWebhookHandle 38 { 39 /** 40 * Base URL of the merchant backend. 41 */ 42 char *base_url; 43 44 /** 45 * The full 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_GetPrivateWebhookCallback cb; 58 59 /** 60 * Closure for @a cb. 61 */ 62 TALER_MERCHANT_GET_PRIVATE_WEBHOOK_RESULT_CLOSURE *cb_cls; 63 64 /** 65 * Reference to the execution context. 66 */ 67 struct GNUNET_CURL_Context *ctx; 68 69 /** 70 * Webhook identifier. 71 */ 72 char *webhook_id; 73 }; 74 75 76 /** 77 * Function called when we're done processing the 78 * HTTP GET /private/webhooks/$WEBHOOK_ID request. 79 * 80 * @param cls the `struct TALER_MERCHANT_GetPrivateWebhookHandle` 81 * @param response_code HTTP response code, 0 on error 82 * @param response response body, NULL if not in JSON 83 */ 84 static void 85 handle_get_webhook_finished (void *cls, 86 long response_code, 87 const void *response) 88 { 89 struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw = cls; 90 const json_t *json = response; 91 struct TALER_MERCHANT_GetPrivateWebhookResponse wgr = { 92 .hr.http_status = (unsigned int) response_code, 93 .hr.reply = json 94 }; 95 96 gpw->job = NULL; 97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 98 "Got /private/webhooks/$WEBHOOK_ID response with status code %u\n", 99 (unsigned int) response_code); 100 switch (response_code) 101 { 102 case MHD_HTTP_OK: 103 { 104 struct GNUNET_JSON_Specification spec[] = { 105 GNUNET_JSON_spec_string ("event_type", 106 &wgr.details.ok.event_type), 107 TALER_JSON_spec_web_url ("url", 108 &wgr.details.ok.url), 109 GNUNET_JSON_spec_string ("http_method", 110 &wgr.details.ok.http_method), 111 GNUNET_JSON_spec_mark_optional ( 112 GNUNET_JSON_spec_string ("header_template", 113 &wgr.details.ok.header_template), 114 NULL), 115 GNUNET_JSON_spec_mark_optional ( 116 GNUNET_JSON_spec_string ("body_template", 117 &wgr.details.ok.body_template), 118 NULL), 119 GNUNET_JSON_spec_end () 120 }; 121 122 if (GNUNET_OK == 123 GNUNET_JSON_parse (json, 124 spec, 125 NULL, NULL)) 126 { 127 gpw->cb (gpw->cb_cls, 128 &wgr); 129 GNUNET_JSON_parse_free (spec); 130 TALER_MERCHANT_get_private_webhook_cancel (gpw); 131 return; 132 } 133 wgr.hr.http_status = 0; 134 wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 135 GNUNET_JSON_parse_free (spec); 136 break; 137 } 138 case MHD_HTTP_UNAUTHORIZED: 139 wgr.hr.ec = TALER_JSON_get_error_code (json); 140 wgr.hr.hint = TALER_JSON_get_error_hint (json); 141 /* Nothing really to verify, merchant says we need to authenticate. */ 142 break; 143 case MHD_HTTP_NOT_FOUND: 144 wgr.hr.ec = TALER_JSON_get_error_code (json); 145 wgr.hr.hint = TALER_JSON_get_error_hint (json); 146 break; 147 default: 148 /* unexpected response code */ 149 wgr.hr.ec = TALER_JSON_get_error_code (json); 150 wgr.hr.hint = TALER_JSON_get_error_hint (json); 151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 152 "Unexpected response code %u/%d\n", 153 (unsigned int) response_code, 154 (int) wgr.hr.ec); 155 break; 156 } 157 gpw->cb (gpw->cb_cls, 158 &wgr); 159 TALER_MERCHANT_get_private_webhook_cancel (gpw); 160 } 161 162 163 struct TALER_MERCHANT_GetPrivateWebhookHandle * 164 TALER_MERCHANT_get_private_webhook_create ( 165 struct GNUNET_CURL_Context *ctx, 166 const char *url, 167 const char *webhook_id) 168 { 169 struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw; 170 171 gpw = GNUNET_new (struct TALER_MERCHANT_GetPrivateWebhookHandle); 172 gpw->ctx = ctx; 173 gpw->base_url = GNUNET_strdup (url); 174 gpw->webhook_id = GNUNET_strdup (webhook_id); 175 return gpw; 176 } 177 178 179 enum TALER_ErrorCode 180 TALER_MERCHANT_get_private_webhook_start ( 181 struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw, 182 TALER_MERCHANT_GetPrivateWebhookCallback cb, 183 TALER_MERCHANT_GET_PRIVATE_WEBHOOK_RESULT_CLOSURE *cb_cls) 184 { 185 CURL *eh; 186 187 gpw->cb = cb; 188 gpw->cb_cls = cb_cls; 189 { 190 char *path; 191 192 GNUNET_asprintf (&path, 193 "private/webhooks/%s", 194 gpw->webhook_id); 195 gpw->url = TALER_url_join (gpw->base_url, 196 path, 197 NULL); 198 GNUNET_free (path); 199 } 200 if (NULL == gpw->url) 201 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 202 eh = TALER_MERCHANT_curl_easy_get_ (gpw->url); 203 if (NULL == eh) 204 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 205 gpw->job = GNUNET_CURL_job_add (gpw->ctx, 206 eh, 207 &handle_get_webhook_finished, 208 gpw); 209 if (NULL == gpw->job) 210 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 211 return TALER_EC_NONE; 212 } 213 214 215 void 216 TALER_MERCHANT_get_private_webhook_cancel ( 217 struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw) 218 { 219 if (NULL != gpw->job) 220 { 221 GNUNET_CURL_job_cancel (gpw->job); 222 gpw->job = NULL; 223 } 224 GNUNET_free (gpw->url); 225 GNUNET_free (gpw->base_url); 226 GNUNET_free (gpw->webhook_id); 227 GNUNET_free (gpw); 228 } 229 230 231 /* end of merchant_api_get-private-webhooks-WEBHOOK_ID-new.c */