merchant_api_get_products.c (7052B)
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_products.c 19 * @brief Implementation of the GET /products 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 * Maximum number of products we return. 36 */ 37 #define MAX_PRODUCTS 1024 38 39 40 /** 41 * Handle for a GET /products operation. 42 */ 43 struct TALER_MERCHANT_ProductsGetHandle 44 { 45 /** 46 * The url for this request. 47 */ 48 char *url; 49 50 /** 51 * Handle for the request. 52 */ 53 struct GNUNET_CURL_Job *job; 54 55 /** 56 * Function to call with the result. 57 */ 58 TALER_MERCHANT_ProductsGetCallback cb; 59 60 /** 61 * Closure for @a cb. 62 */ 63 void *cb_cls; 64 65 /** 66 * Reference to the execution context. 67 */ 68 struct GNUNET_CURL_Context *ctx; 69 70 }; 71 72 73 /** 74 * Parse product information from @a ia. 75 * 76 * @param json overall JSON reply 77 * @param ia JSON array (or NULL!) with product data 78 * @param pgh operation handle 79 * @return #GNUNET_OK on success 80 */ 81 static enum GNUNET_GenericReturnValue 82 parse_products (const json_t *json, 83 const json_t *ia, 84 struct TALER_MERCHANT_ProductsGetHandle *pgh) 85 { 86 unsigned int ies_len = json_array_size (ia); 87 88 if ( (json_array_size (ia) != (size_t) ies_len) || 89 (ies_len > MAX_PRODUCTS) ) 90 { 91 GNUNET_break (0); 92 return GNUNET_SYSERR; 93 } 94 { 95 struct TALER_MERCHANT_InventoryEntry ies[GNUNET_NZL (ies_len)]; 96 size_t index; 97 json_t *value; 98 enum GNUNET_GenericReturnValue ret; 99 100 ret = GNUNET_OK; 101 json_array_foreach (ia, index, value) { 102 struct TALER_MERCHANT_InventoryEntry *ie = &ies[index]; 103 struct GNUNET_JSON_Specification spec[] = { 104 GNUNET_JSON_spec_string ("product_id", 105 &ie->product_id), 106 GNUNET_JSON_spec_uint64 ("product_serial", 107 &ie->product_serial), 108 GNUNET_JSON_spec_end () 109 }; 110 111 if (GNUNET_OK != 112 GNUNET_JSON_parse (value, 113 spec, 114 NULL, NULL)) 115 { 116 GNUNET_break_op (0); 117 ret = GNUNET_SYSERR; 118 continue; 119 } 120 if (GNUNET_SYSERR == ret) 121 break; 122 } 123 if (GNUNET_OK == ret) 124 { 125 struct TALER_MERCHANT_GetProductsResponse gpr = { 126 .hr.http_status = MHD_HTTP_OK, 127 .hr.reply = json, 128 .details.ok.products_length = ies_len, 129 .details.ok.products = ies 130 }; 131 132 pgh->cb (pgh->cb_cls, 133 &gpr); 134 pgh->cb = NULL; /* just to be sure */ 135 } 136 return ret; 137 } 138 } 139 140 141 /** 142 * Function called when we're done processing the 143 * HTTP /products request. 144 * 145 * @param cls the `struct TALER_MERCHANT_ProductsGetHandle` 146 * @param response_code HTTP response code, 0 on error 147 * @param response response body, NULL if not in JSON 148 */ 149 static void 150 handle_get_products_finished (void *cls, 151 long response_code, 152 const void *response) 153 { 154 struct TALER_MERCHANT_ProductsGetHandle *pgh = cls; 155 const json_t *json = response; 156 struct TALER_MERCHANT_GetProductsResponse gpr = { 157 .hr.http_status = (unsigned int) response_code, 158 .hr.reply = json 159 }; 160 161 pgh->job = NULL; 162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 163 "Got /products response with status code %u\n", 164 (unsigned int) response_code); 165 switch (response_code) 166 { 167 case MHD_HTTP_OK: 168 { 169 const json_t *products; 170 struct GNUNET_JSON_Specification spec[] = { 171 GNUNET_JSON_spec_array_const ("products", 172 &products), 173 GNUNET_JSON_spec_end () 174 }; 175 176 if (GNUNET_OK != 177 GNUNET_JSON_parse (json, 178 spec, 179 NULL, NULL)) 180 { 181 gpr.hr.http_status = 0; 182 gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 183 break; 184 } 185 if (GNUNET_OK == 186 parse_products (json, 187 products, 188 pgh)) 189 { 190 TALER_MERCHANT_products_get_cancel (pgh); 191 return; 192 } 193 gpr.hr.http_status = 0; 194 gpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 195 break; 196 } 197 case MHD_HTTP_UNAUTHORIZED: 198 gpr.hr.ec = TALER_JSON_get_error_code (json); 199 gpr.hr.hint = TALER_JSON_get_error_hint (json); 200 /* Nothing really to verify, merchant says we need to authenticate. */ 201 break; 202 default: 203 /* unexpected response code */ 204 gpr.hr.ec = TALER_JSON_get_error_code (json); 205 gpr.hr.hint = TALER_JSON_get_error_hint (json); 206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 207 "Unexpected response code %u/%d\n", 208 (unsigned int) response_code, 209 (int) gpr.hr.ec); 210 break; 211 } 212 pgh->cb (pgh->cb_cls, 213 &gpr); 214 TALER_MERCHANT_products_get_cancel (pgh); 215 } 216 217 218 struct TALER_MERCHANT_ProductsGetHandle * 219 TALER_MERCHANT_products_get ( 220 struct GNUNET_CURL_Context *ctx, 221 const char *backend_url, 222 TALER_MERCHANT_ProductsGetCallback cb, 223 void *cb_cls) 224 { 225 struct TALER_MERCHANT_ProductsGetHandle *pgh; 226 CURL *eh; 227 228 pgh = GNUNET_new (struct TALER_MERCHANT_ProductsGetHandle); 229 pgh->ctx = ctx; 230 pgh->cb = cb; 231 pgh->cb_cls = cb_cls; 232 pgh->url = TALER_url_join (backend_url, 233 "private/products", 234 NULL); 235 if (NULL == pgh->url) 236 { 237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 238 "Could not construct request URL.\n"); 239 GNUNET_free (pgh); 240 return NULL; 241 } 242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 243 "Requesting URL '%s'\n", 244 pgh->url); 245 eh = TALER_MERCHANT_curl_easy_get_ (pgh->url); 246 pgh->job = GNUNET_CURL_job_add (ctx, 247 eh, 248 &handle_get_products_finished, 249 pgh); 250 return pgh; 251 } 252 253 254 void 255 TALER_MERCHANT_products_get_cancel ( 256 struct TALER_MERCHANT_ProductsGetHandle *pgh) 257 { 258 if (NULL != pgh->job) 259 GNUNET_CURL_job_cancel (pgh->job); 260 GNUNET_free (pgh->url); 261 GNUNET_free (pgh); 262 }