Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014--2025 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 24 : handle_get_product_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 24 : struct TALER_MERCHANT_ProductGetHandle *pgh = cls;
81 24 : const json_t *json = response;
82 24 : struct TALER_MERCHANT_ProductGetResponse pgr = {
83 24 : .hr.http_status = (unsigned int) response_code,
84 : .hr.reply = json
85 : };
86 :
87 24 : pgh->job = NULL;
88 24 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 : "Got /products/$ID response with status code %u\n",
90 : (unsigned int) response_code);
91 24 : switch (response_code)
92 : {
93 12 : case MHD_HTTP_OK:
94 : {
95 : struct GNUNET_JSON_Specification spec[] = {
96 12 : GNUNET_JSON_spec_string (
97 : "product_name",
98 : &pgr.details.ok.product_name),
99 12 : GNUNET_JSON_spec_string (
100 : "description",
101 : &pgr.details.ok.description),
102 12 : GNUNET_JSON_spec_object_const (
103 : "description_i18n",
104 : &pgr.details.ok.description_i18n),
105 12 : GNUNET_JSON_spec_string (
106 : "unit",
107 : &pgr.details.ok.unit),
108 12 : TALER_JSON_spec_amount_any_array (
109 : "unit_price",
110 : &pgr.details.ok.unit_price_len,
111 : (struct TALER_Amount **) &pgr.details.ok.unit_price),
112 12 : TALER_JSON_spec_amount_any (
113 : "price",
114 : &pgr.details.ok.price),
115 12 : GNUNET_JSON_spec_mark_optional (
116 : GNUNET_JSON_spec_string (
117 : "image",
118 : &pgr.details.ok.image),
119 : NULL),
120 12 : GNUNET_JSON_spec_mark_optional (
121 : GNUNET_JSON_spec_array_const (
122 : "taxes",
123 : &pgr.details.ok.taxes),
124 : NULL),
125 12 : GNUNET_JSON_spec_int64 (
126 : "total_stock",
127 : &pgr.details.ok.total_stock),
128 12 : GNUNET_JSON_spec_string (
129 : "unit_total_stock",
130 : &pgr.details.ok.unit_total_stock),
131 12 : GNUNET_JSON_spec_bool (
132 : "unit_allow_fraction",
133 : &pgr.details.ok.unit_allow_fraction),
134 12 : GNUNET_JSON_spec_uint32 (
135 : "unit_precision_level",
136 : &pgr.details.ok.unit_precision_level),
137 12 : GNUNET_JSON_spec_uint64 (
138 : "total_sold",
139 : &pgr.details.ok.total_sold),
140 12 : GNUNET_JSON_spec_uint64 (
141 : "total_lost",
142 : &pgr.details.ok.total_lost),
143 12 : GNUNET_JSON_spec_mark_optional (
144 : GNUNET_JSON_spec_object_const (
145 : "address",
146 : &pgr.details.ok.location),
147 : NULL),
148 12 : GNUNET_JSON_spec_mark_optional (
149 : GNUNET_JSON_spec_timestamp (
150 : "next_restock",
151 : &pgr.details.ok.next_restock),
152 : NULL),
153 12 : GNUNET_JSON_spec_end ()
154 : };
155 :
156 12 : if (GNUNET_OK ==
157 12 : GNUNET_JSON_parse (json,
158 : spec,
159 : NULL, NULL))
160 : {
161 12 : pgh->cb (pgh->cb_cls,
162 : &pgr);
163 12 : GNUNET_JSON_parse_free (spec);
164 12 : TALER_MERCHANT_product_get_cancel (pgh);
165 12 : return;
166 : }
167 0 : pgr.hr.http_status = 0;
168 0 : pgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
169 0 : break;
170 : }
171 4 : case MHD_HTTP_UNAUTHORIZED:
172 4 : pgr.hr.ec = TALER_JSON_get_error_code (json);
173 4 : pgr.hr.hint = TALER_JSON_get_error_hint (json);
174 : /* Nothing really to verify, merchant says we need to authenticate. */
175 4 : break;
176 8 : case MHD_HTTP_NOT_FOUND:
177 8 : pgr.hr.ec = TALER_JSON_get_error_code (json);
178 8 : pgr.hr.hint = TALER_JSON_get_error_hint (json);
179 8 : break;
180 0 : default:
181 : /* unexpected response code */
182 0 : pgr.hr.ec = TALER_JSON_get_error_code (json);
183 0 : pgr.hr.hint = TALER_JSON_get_error_hint (json);
184 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 : "Unexpected response code %u/%d\n",
186 : (unsigned int) response_code,
187 : (int) pgr.hr.ec);
188 0 : break;
189 : }
190 12 : pgh->cb (pgh->cb_cls,
191 : &pgr);
192 12 : TALER_MERCHANT_product_get_cancel (pgh);
193 : }
194 :
195 :
196 : struct TALER_MERCHANT_ProductGetHandle *
197 24 : TALER_MERCHANT_product_get (
198 : struct GNUNET_CURL_Context *ctx,
199 : const char *backend_url,
200 : const char *product_id,
201 : TALER_MERCHANT_ProductGetCallback cb,
202 : void *cb_cls)
203 : {
204 : struct TALER_MERCHANT_ProductGetHandle *pgh;
205 : CURL *eh;
206 :
207 24 : pgh = GNUNET_new (struct TALER_MERCHANT_ProductGetHandle);
208 24 : pgh->ctx = ctx;
209 24 : pgh->cb = cb;
210 24 : pgh->cb_cls = cb_cls;
211 : {
212 : char *path;
213 :
214 24 : GNUNET_asprintf (&path,
215 : "private/products/%s",
216 : product_id);
217 24 : pgh->url = TALER_url_join (backend_url,
218 : path,
219 : NULL);
220 24 : GNUNET_free (path);
221 : }
222 24 : if (NULL == pgh->url)
223 : {
224 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 : "Could not construct request URL.\n");
226 0 : GNUNET_free (pgh);
227 0 : return NULL;
228 : }
229 24 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 : "Requesting URL '%s'\n",
231 : pgh->url);
232 24 : eh = TALER_MERCHANT_curl_easy_get_ (pgh->url);
233 24 : pgh->job = GNUNET_CURL_job_add (ctx,
234 : eh,
235 : &handle_get_product_finished,
236 : pgh);
237 24 : return pgh;
238 : }
239 :
240 :
241 : void
242 24 : TALER_MERCHANT_product_get_cancel (
243 : struct TALER_MERCHANT_ProductGetHandle *pgh)
244 : {
245 24 : if (NULL != pgh->job)
246 0 : GNUNET_CURL_job_cancel (pgh->job);
247 24 : GNUNET_free (pgh->url);
248 24 : GNUNET_free (pgh);
249 24 : }
|