merchant_api_get_webhooks.c (6714B)
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 parse_webhooks (const json_t *ia, 82 struct TALER_MERCHANT_WebhooksGetResponse *wgr, 83 struct TALER_MERCHANT_WebhooksGetHandle *wgh) 84 { 85 unsigned int whook_len = (unsigned int) json_array_size (ia); 86 87 if ( (json_array_size (ia) != (size_t) whook_len) || 88 (whook_len > MAX_WEBHOOKS) ) 89 { 90 GNUNET_break (0); 91 return GNUNET_SYSERR; 92 } 93 { 94 struct TALER_MERCHANT_WebhookEntry whook[GNUNET_NZL (whook_len)]; 95 size_t index; 96 json_t *value; 97 98 json_array_foreach (ia, index, value) { 99 struct TALER_MERCHANT_WebhookEntry *ie = &whook[index]; 100 struct GNUNET_JSON_Specification spec[] = { 101 GNUNET_JSON_spec_string ("webhook_id", 102 &ie->webhook_id), 103 GNUNET_JSON_spec_end () 104 }; 105 106 if (GNUNET_OK != 107 GNUNET_JSON_parse (value, 108 spec, 109 NULL, NULL)) 110 { 111 GNUNET_break_op (0); 112 return GNUNET_SYSERR; 113 } 114 } 115 wgr->details.ok.webhooks_length = whook_len; 116 wgr->details.ok.webhooks = whook; 117 wgh->cb (wgh->cb_cls, 118 wgr); 119 wgh->cb = NULL; /* just to be sure */ 120 } 121 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 handle_get_webhooks_finished (void *cls, 135 long response_code, 136 const void *response) 137 { 138 struct TALER_MERCHANT_WebhooksGetHandle *wgh = cls; 139 const json_t *json = response; 140 struct TALER_MERCHANT_WebhooksGetResponse wgr = { 141 .hr.http_status = (unsigned int) response_code, 142 .hr.reply = json 143 }; 144 145 wgh->job = NULL; 146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 147 "Got /webhooks response with status code %u\n", 148 (unsigned int) response_code); 149 switch (response_code) 150 { 151 case MHD_HTTP_OK: 152 { 153 const json_t *webhooks; 154 struct GNUNET_JSON_Specification spec[] = { 155 GNUNET_JSON_spec_array_const ("webhooks", 156 &webhooks), 157 GNUNET_JSON_spec_end () 158 }; 159 160 if (GNUNET_OK != 161 GNUNET_JSON_parse (json, 162 spec, 163 NULL, NULL)) 164 { 165 wgr.hr.http_status = 0; 166 wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 167 break; 168 } 169 if (GNUNET_OK == 170 parse_webhooks (webhooks, 171 &wgr, 172 wgh)) 173 { 174 TALER_MERCHANT_webhooks_get_cancel (wgh); 175 return; 176 } 177 wgr.hr.http_status = 0; 178 wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 179 break; 180 } 181 case MHD_HTTP_UNAUTHORIZED: 182 wgr.hr.ec = TALER_JSON_get_error_code (json); 183 wgr.hr.hint = TALER_JSON_get_error_hint (json); 184 /* Nothing really to verify, merchant says we need to authenticate. */ 185 break; 186 default: 187 /* unexpected response code */ 188 wgr.hr.ec = TALER_JSON_get_error_code (json); 189 wgr.hr.hint = TALER_JSON_get_error_hint (json); 190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 191 "Unexpected response code %u/%d\n", 192 (unsigned int) response_code, 193 (int) wgr.hr.ec); 194 break; 195 } 196 wgh->cb (wgh->cb_cls, 197 &wgr); 198 TALER_MERCHANT_webhooks_get_cancel (wgh); 199 } 200 201 202 struct TALER_MERCHANT_WebhooksGetHandle * 203 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 wgh = GNUNET_new (struct TALER_MERCHANT_WebhooksGetHandle); 213 wgh->ctx = ctx; 214 wgh->cb = cb; 215 wgh->cb_cls = cb_cls; 216 wgh->url = TALER_url_join (backend_url, 217 "private/webhooks", 218 NULL); 219 if (NULL == wgh->url) 220 { 221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 222 "Could not construct request URL.\n"); 223 GNUNET_free (wgh); 224 return NULL; 225 } 226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 227 "Requesting URL '%s'\n", 228 wgh->url); 229 eh = TALER_MERCHANT_curl_easy_get_ (wgh->url); 230 wgh->job = GNUNET_CURL_job_add (ctx, 231 eh, 232 &handle_get_webhooks_finished, 233 wgh); 234 return wgh; 235 } 236 237 238 void 239 TALER_MERCHANT_webhooks_get_cancel ( 240 struct TALER_MERCHANT_WebhooksGetHandle *wgh) 241 { 242 if (NULL != wgh->job) 243 GNUNET_CURL_job_cancel (wgh->job); 244 GNUNET_free (wgh->url); 245 GNUNET_free (wgh); 246 }