Line data Source code
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/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 0 : handle_get_webhook_finished (void *cls,
86 : long response_code,
87 : const void *response)
88 : {
89 0 : struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw = cls;
90 0 : const json_t *json = response;
91 0 : struct TALER_MERCHANT_GetPrivateWebhookResponse wgr = {
92 0 : .hr.http_status = (unsigned int) response_code,
93 : .hr.reply = json
94 : };
95 :
96 0 : gpw->job = NULL;
97 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98 : "Got /private/webhooks/$WEBHOOK_ID response with status code %u\n",
99 : (unsigned int) response_code);
100 0 : switch (response_code)
101 : {
102 0 : case MHD_HTTP_OK:
103 : {
104 : struct GNUNET_JSON_Specification spec[] = {
105 0 : GNUNET_JSON_spec_string ("event_type",
106 : &wgr.details.ok.event_type),
107 0 : TALER_JSON_spec_web_url ("url",
108 : &wgr.details.ok.url),
109 0 : GNUNET_JSON_spec_string ("http_method",
110 : &wgr.details.ok.http_method),
111 0 : GNUNET_JSON_spec_mark_optional (
112 : GNUNET_JSON_spec_string ("header_template",
113 : &wgr.details.ok.header_template),
114 : NULL),
115 0 : GNUNET_JSON_spec_mark_optional (
116 : GNUNET_JSON_spec_string ("body_template",
117 : &wgr.details.ok.body_template),
118 : NULL),
119 0 : GNUNET_JSON_spec_end ()
120 : };
121 :
122 0 : if (GNUNET_OK ==
123 0 : GNUNET_JSON_parse (json,
124 : spec,
125 : NULL, NULL))
126 : {
127 0 : gpw->cb (gpw->cb_cls,
128 : &wgr);
129 0 : GNUNET_JSON_parse_free (spec);
130 0 : TALER_MERCHANT_get_private_webhook_cancel (gpw);
131 0 : return;
132 : }
133 0 : wgr.hr.http_status = 0;
134 0 : wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
135 0 : GNUNET_JSON_parse_free (spec);
136 0 : break;
137 : }
138 0 : case MHD_HTTP_UNAUTHORIZED:
139 0 : wgr.hr.ec = TALER_JSON_get_error_code (json);
140 0 : wgr.hr.hint = TALER_JSON_get_error_hint (json);
141 : /* Nothing really to verify, merchant says we need to authenticate. */
142 0 : break;
143 0 : case MHD_HTTP_NOT_FOUND:
144 0 : wgr.hr.ec = TALER_JSON_get_error_code (json);
145 0 : wgr.hr.hint = TALER_JSON_get_error_hint (json);
146 0 : break;
147 0 : default:
148 : /* unexpected response code */
149 0 : wgr.hr.ec = TALER_JSON_get_error_code (json);
150 0 : wgr.hr.hint = TALER_JSON_get_error_hint (json);
151 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 : "Unexpected response code %u/%d\n",
153 : (unsigned int) response_code,
154 : (int) wgr.hr.ec);
155 0 : break;
156 : }
157 0 : gpw->cb (gpw->cb_cls,
158 : &wgr);
159 0 : TALER_MERCHANT_get_private_webhook_cancel (gpw);
160 : }
161 :
162 :
163 : struct TALER_MERCHANT_GetPrivateWebhookHandle *
164 0 : 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 0 : gpw = GNUNET_new (struct TALER_MERCHANT_GetPrivateWebhookHandle);
172 0 : gpw->ctx = ctx;
173 0 : gpw->base_url = GNUNET_strdup (url);
174 0 : gpw->webhook_id = GNUNET_strdup (webhook_id);
175 0 : return gpw;
176 : }
177 :
178 :
179 : enum TALER_ErrorCode
180 0 : 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 0 : gpw->cb = cb;
188 0 : gpw->cb_cls = cb_cls;
189 : {
190 : char *path;
191 :
192 0 : GNUNET_asprintf (&path,
193 : "private/webhooks/%s",
194 : gpw->webhook_id);
195 0 : gpw->url = TALER_url_join (gpw->base_url,
196 : path,
197 : NULL);
198 0 : GNUNET_free (path);
199 : }
200 0 : if (NULL == gpw->url)
201 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
202 0 : eh = TALER_MERCHANT_curl_easy_get_ (gpw->url);
203 0 : if (NULL == eh)
204 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
205 0 : gpw->job = GNUNET_CURL_job_add (gpw->ctx,
206 : eh,
207 : &handle_get_webhook_finished,
208 : gpw);
209 0 : if (NULL == gpw->job)
210 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
211 0 : return TALER_EC_NONE;
212 : }
213 :
214 :
215 : void
216 0 : TALER_MERCHANT_get_private_webhook_cancel (
217 : struct TALER_MERCHANT_GetPrivateWebhookHandle *gpw)
218 : {
219 0 : if (NULL != gpw->job)
220 : {
221 0 : GNUNET_CURL_job_cancel (gpw->job);
222 0 : gpw->job = NULL;
223 : }
224 0 : GNUNET_free (gpw->url);
225 0 : GNUNET_free (gpw->base_url);
226 0 : GNUNET_free (gpw->webhook_id);
227 0 : GNUNET_free (gpw);
228 0 : }
229 :
230 :
231 : /* end of merchant_api_get-private-webhooks-WEBHOOK_ID-new.c */
|