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