Line data Source code
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_wallet_get_template.c
19 : * @brief Implementation of the GET /templates/$ID 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 : * Handle for a GET /templates/$ID operation.
36 : */
37 : struct TALER_MERCHANT_WalletTemplateGetHandle
38 : {
39 : /**
40 : * The url for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Handle for the request.
46 : */
47 : struct GNUNET_CURL_Job *job;
48 :
49 : /**
50 : * Function to call with the result.
51 : */
52 : TALER_MERCHANT_WalletTemplateGetCallback cb;
53 :
54 : /**
55 : * Closure for @a cb.
56 : */
57 : void *cb_cls;
58 :
59 : /**
60 : * Reference to the execution context.
61 : */
62 : struct GNUNET_CURL_Context *ctx;
63 :
64 : };
65 :
66 :
67 : /**
68 : * Function called when we're done processing the
69 : * HTTP GET /templates/$ID request.
70 : *
71 : * @param cls the `struct TALER_MERCHANT_TemplateGetHandle`
72 : * @param response_code HTTP response code, 0 on error
73 : * @param response response body, NULL if not in JSON
74 : */
75 : static void
76 0 : handle_get_template_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 0 : struct TALER_MERCHANT_WalletTemplateGetHandle *tgh = cls;
81 0 : const json_t *json = response;
82 0 : struct TALER_MERCHANT_WalletTemplateGetResponse tgr = {
83 0 : .hr.http_status = (unsigned int) response_code,
84 : .hr.reply = json
85 : };
86 :
87 0 : tgh->job = NULL;
88 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 : "Got /templates/$ID response with status code %u\n",
90 : (unsigned int) response_code);
91 0 : switch (response_code)
92 : {
93 0 : case MHD_HTTP_OK:
94 : {
95 : const json_t *contract;
96 : struct GNUNET_JSON_Specification spec[] = {
97 0 : GNUNET_JSON_spec_object_const ("template_contract",
98 : &contract),
99 0 : GNUNET_JSON_spec_end ()
100 : };
101 :
102 0 : if (GNUNET_OK ==
103 0 : GNUNET_JSON_parse (json,
104 : spec,
105 : NULL, NULL))
106 : {
107 0 : tgr.details.ok.template_contract = contract;
108 0 : tgh->cb (tgh->cb_cls,
109 : &tgr);
110 0 : TALER_MERCHANT_wallet_template_get_cancel (tgh);
111 0 : return;
112 : }
113 0 : tgr.hr.http_status = 0;
114 0 : tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
115 0 : break;
116 : }
117 0 : case MHD_HTTP_UNAUTHORIZED:
118 0 : tgr.hr.ec = TALER_JSON_get_error_code (json);
119 0 : tgr.hr.hint = TALER_JSON_get_error_hint (json);
120 : /* Nothing really to verify, merchant says we need to authenticate. */
121 0 : break;
122 0 : case MHD_HTTP_NOT_FOUND:
123 0 : tgr.hr.ec = TALER_JSON_get_error_code (json);
124 0 : tgr.hr.hint = TALER_JSON_get_error_hint (json);
125 0 : break;
126 0 : default:
127 : /* unexpected response code */
128 0 : tgr.hr.ec = TALER_JSON_get_error_code (json);
129 0 : tgr.hr.hint = TALER_JSON_get_error_hint (json);
130 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131 : "Unexpected response code %u/%d\n",
132 : (unsigned int) response_code,
133 : (int) tgr.hr.ec);
134 0 : break;
135 : }
136 0 : tgh->cb (tgh->cb_cls,
137 : &tgr);
138 0 : TALER_MERCHANT_wallet_template_get_cancel (tgh);
139 : }
140 :
141 :
142 : struct TALER_MERCHANT_WalletTemplateGetHandle *
143 0 : TALER_MERCHANT_wallet_template_get (
144 : struct GNUNET_CURL_Context *ctx,
145 : const char *backend_url,
146 : const char *template_id,
147 : TALER_MERCHANT_WalletTemplateGetCallback cb,
148 : void *cb_cls)
149 : {
150 : struct TALER_MERCHANT_WalletTemplateGetHandle *tgh;
151 : CURL *eh;
152 :
153 0 : tgh = GNUNET_new (struct TALER_MERCHANT_WalletTemplateGetHandle);
154 0 : tgh->ctx = ctx;
155 0 : tgh->cb = cb;
156 0 : tgh->cb_cls = cb_cls;
157 : {
158 : char *path;
159 :
160 0 : GNUNET_asprintf (&path,
161 : "templates/%s",
162 : template_id);
163 0 : tgh->url = TALER_url_join (backend_url,
164 : path,
165 : NULL);
166 0 : GNUNET_free (path);
167 : }
168 0 : if (NULL == tgh->url)
169 : {
170 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 : "Could not construct request URL.\n");
172 0 : GNUNET_free (tgh);
173 0 : return NULL;
174 : }
175 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 : "Requesting URL '%s'\n",
177 : tgh->url);
178 0 : eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
179 0 : tgh->job = GNUNET_CURL_job_add (ctx,
180 : eh,
181 : &handle_get_template_finished,
182 : tgh);
183 0 : return tgh;
184 : }
185 :
186 :
187 : void
188 0 : TALER_MERCHANT_wallet_template_get_cancel (
189 : struct TALER_MERCHANT_WalletTemplateGetHandle *tgh)
190 : {
191 0 : if (NULL != tgh->job)
192 0 : GNUNET_CURL_job_cancel (tgh->job);
193 0 : GNUNET_free (tgh->url);
194 0 : GNUNET_free (tgh);
195 0 : }
|