Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018, 2020 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_product.c
19 : * @brief Implementation of the GET /product/$ID request of the merchant's HTTP API
20 : * @author Christian Grothoff
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 /products/$ID operation.
36 : */
37 : struct TALER_MERCHANT_ProductGetHandle
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_ProductGetCallback 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 /products/$ID request.
70 : *
71 : * @param cls the `struct TALER_MERCHANT_ProductGetHandle`
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_product_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 0 : struct TALER_MERCHANT_ProductGetHandle *pgh = cls;
81 0 : const json_t *json = response;
82 0 : struct TALER_MERCHANT_HttpResponse hr = {
83 0 : .http_status = (unsigned int) response_code,
84 : .reply = json
85 : };
86 :
87 0 : pgh->job = NULL;
88 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 : "Got /products/$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 char *description;
96 : json_t *description_i18n;
97 : const char *unit;
98 : struct TALER_Amount price;
99 : const char *image;
100 : json_t *taxes;
101 : int64_t total_stock;
102 : uint64_t total_sold;
103 : uint64_t total_lost;
104 : json_t *address;
105 0 : bool rst_ok = true;
106 0 : struct GNUNET_TIME_Timestamp next_restock
107 : = GNUNET_TIME_UNIT_ZERO_TS;
108 : struct GNUNET_JSON_Specification spec[] = {
109 0 : GNUNET_JSON_spec_string ("description",
110 : &description),
111 0 : GNUNET_JSON_spec_json ("description_i18n",
112 : &description_i18n),
113 0 : GNUNET_JSON_spec_string ("unit",
114 : &unit),
115 0 : TALER_JSON_spec_amount_any ("price",
116 : &price),
117 0 : GNUNET_JSON_spec_string ("image",
118 : &image),
119 0 : GNUNET_JSON_spec_json ("taxes",
120 : &taxes),
121 0 : GNUNET_JSON_spec_int64 ("total_stock",
122 : &total_stock),
123 0 : GNUNET_JSON_spec_uint64 ("total_sold",
124 : &total_sold),
125 0 : GNUNET_JSON_spec_uint64 ("total_lost",
126 : &total_lost),
127 0 : GNUNET_JSON_spec_json ("address",
128 : &address),
129 0 : GNUNET_JSON_spec_end ()
130 : };
131 :
132 0 : if (NULL !=
133 0 : json_object_get (json,
134 : "next_restock"))
135 : {
136 : struct GNUNET_JSON_Specification spect[] = {
137 0 : GNUNET_JSON_spec_timestamp ("next_restock",
138 : &next_restock),
139 0 : GNUNET_JSON_spec_end ()
140 : };
141 :
142 0 : if (GNUNET_OK !=
143 0 : GNUNET_JSON_parse (json,
144 : spect,
145 : NULL, NULL))
146 0 : rst_ok = false;
147 : }
148 :
149 :
150 0 : if ( (rst_ok) &&
151 : (GNUNET_OK ==
152 0 : GNUNET_JSON_parse (json,
153 : spec,
154 : NULL, NULL)) )
155 : {
156 0 : pgh->cb (pgh->cb_cls,
157 : &hr,
158 : description,
159 : description_i18n,
160 : unit,
161 : &price,
162 : image,
163 : taxes,
164 : total_stock,
165 : total_sold,
166 : total_lost,
167 : address,
168 : next_restock);
169 0 : GNUNET_JSON_parse_free (spec);
170 0 : TALER_MERCHANT_product_get_cancel (pgh);
171 0 : return;
172 : }
173 0 : hr.http_status = 0;
174 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
175 0 : GNUNET_JSON_parse_free (spec);
176 0 : break;
177 : }
178 0 : case MHD_HTTP_UNAUTHORIZED:
179 0 : hr.ec = TALER_JSON_get_error_code (json);
180 0 : hr.hint = TALER_JSON_get_error_hint (json);
181 : /* Nothing really to verify, merchant says we need to authenticate. */
182 0 : break;
183 0 : case MHD_HTTP_NOT_FOUND:
184 0 : hr.ec = TALER_JSON_get_error_code (json);
185 0 : hr.hint = TALER_JSON_get_error_hint (json);
186 0 : break;
187 0 : default:
188 : /* unexpected response code */
189 0 : hr.ec = TALER_JSON_get_error_code (json);
190 0 : hr.hint = TALER_JSON_get_error_hint (json);
191 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 : "Unexpected response code %u/%d\n",
193 : (unsigned int) response_code,
194 : (int) hr.ec);
195 0 : break;
196 : }
197 0 : pgh->cb (pgh->cb_cls,
198 : &hr,
199 : NULL,
200 : NULL,
201 : NULL,
202 : NULL,
203 : NULL,
204 : NULL,
205 : 0,
206 : 0,
207 : 0,
208 : NULL,
209 0 : GNUNET_TIME_UNIT_FOREVER_TS);
210 0 : TALER_MERCHANT_product_get_cancel (pgh);
211 : }
212 :
213 :
214 : struct TALER_MERCHANT_ProductGetHandle *
215 0 : TALER_MERCHANT_product_get (
216 : struct GNUNET_CURL_Context *ctx,
217 : const char *backend_url,
218 : const char *product_id,
219 : TALER_MERCHANT_ProductGetCallback cb,
220 : void *cb_cls)
221 : {
222 : struct TALER_MERCHANT_ProductGetHandle *pgh;
223 : CURL *eh;
224 :
225 0 : pgh = GNUNET_new (struct TALER_MERCHANT_ProductGetHandle);
226 0 : pgh->ctx = ctx;
227 0 : pgh->cb = cb;
228 0 : pgh->cb_cls = cb_cls;
229 : {
230 : char *path;
231 :
232 0 : GNUNET_asprintf (&path,
233 : "private/products/%s",
234 : product_id);
235 0 : pgh->url = TALER_url_join (backend_url,
236 : path,
237 : NULL);
238 0 : GNUNET_free (path);
239 : }
240 0 : if (NULL == pgh->url)
241 : {
242 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 : "Could not construct request URL.\n");
244 0 : GNUNET_free (pgh);
245 0 : return NULL;
246 : }
247 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 : "Requesting URL '%s'\n",
249 : pgh->url);
250 0 : eh = TALER_MERCHANT_curl_easy_get_ (pgh->url);
251 0 : pgh->job = GNUNET_CURL_job_add (ctx,
252 : eh,
253 : &handle_get_product_finished,
254 : pgh);
255 0 : return pgh;
256 : }
257 :
258 :
259 : void
260 0 : TALER_MERCHANT_product_get_cancel (
261 : struct TALER_MERCHANT_ProductGetHandle *pgh)
262 : {
263 0 : if (NULL != pgh->job)
264 0 : GNUNET_CURL_job_cancel (pgh->job);
265 0 : GNUNET_free (pgh->url);
266 0 : GNUNET_free (pgh);
267 0 : }
|