taler-merchant-httpd_get-exchanges.c (5048B)
1 /* 2 This file is part of TALER 3 (C) 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, 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 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/backend/taler-merchant-httpd_get-exchanges.c 18 * @brief implement API for querying exchange status 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <jansson.h> 23 #include <taler/taler_util.h> 24 #include <taler/taler_json_lib.h> 25 #include "taler-merchant-httpd.h" 26 #include "taler-merchant-httpd_get-exchanges.h" 27 #include "taler-merchant-httpd_mhd.h" 28 #include "taler-merchant-httpd_get-exchanges.h" 29 #include "merchant-database/select_exchanges.h" 30 31 32 /** 33 * Callback on an exchange known to us. 34 * 35 * @param cls closure with `json_t *` array to expand 36 * @param exchange_url base URL of the exchange 37 * @param keys keys of the exchange, NULL if we do not have any yet 38 * @param next_download when will be the next download 39 * @param keys_expiration when does the current ``/keys`` response expire 40 * @param http_status last HTTP status from ``/keys`` 41 * @param ec last error code from fetching ``/keys`` 42 */ 43 static void 44 add_exchange ( 45 void *cls, 46 const char *exchange_url, 47 const json_t *keys, 48 struct GNUNET_TIME_Absolute next_download, 49 struct GNUNET_TIME_Absolute keys_expiration, 50 unsigned int http_status, 51 enum TALER_ErrorCode ec) 52 { 53 json_t *xa = cls; 54 json_t *xi; 55 json_t *debit_restrictions = NULL; 56 57 if (NULL != keys) 58 { 59 json_t *accounts; 60 json_t *account; 61 size_t i; 62 63 accounts = json_object_get (keys, 64 "accounts"); 65 debit_restrictions = json_object (); 66 json_array_foreach (accounts, i, account) 67 { 68 json_t *dr = json_object_get (account, 69 "debit_restrictions"); 70 const char *payto; 71 72 payto = json_string_value (json_object_get (account, 73 "payto_uri")); 74 75 if (NULL != dr) 76 { 77 GNUNET_break (0 == 78 json_object_set (debit_restrictions, 79 payto, 80 dr)); 81 } 82 else 83 { 84 GNUNET_break (0 == 85 json_object_set (debit_restrictions, 86 payto, 87 json_null ())); 88 } 89 } 90 } 91 92 xi = GNUNET_JSON_PACK ( 93 GNUNET_JSON_pack_string ("exchange_url", 94 exchange_url), 95 GNUNET_JSON_pack_timestamp ("next_download", 96 GNUNET_TIME_absolute_to_timestamp (next_download 97 )), 98 GNUNET_JSON_pack_conditional ( 99 ! GNUNET_TIME_absolute_is_zero (keys_expiration), 100 GNUNET_JSON_pack_timestamp ("keys_expiration", 101 GNUNET_TIME_absolute_to_timestamp ( 102 keys_expiration))), 103 GNUNET_JSON_pack_allow_null ( 104 GNUNET_JSON_pack_object_steal ("debit_restrictions", 105 debit_restrictions)), 106 GNUNET_JSON_pack_uint64 ("keys_http_status", 107 http_status), 108 GNUNET_JSON_pack_uint64 ("keys_ec", 109 ec), 110 GNUNET_JSON_pack_string ("keys_hint", 111 TALER_ErrorCode_get_hint (ec))); 112 GNUNET_assert (NULL != xi); 113 GNUNET_assert (0 == 114 json_array_append_new (xa, 115 xi)); 116 } 117 118 119 enum MHD_Result 120 MH_handler_exchanges (const struct TMH_RequestHandler *rh, 121 struct MHD_Connection *connection, 122 struct TMH_HandlerContext *hc) 123 { 124 json_t *exchanges; 125 enum GNUNET_DB_QueryStatus qs; 126 127 exchanges = json_array (); 128 GNUNET_assert (NULL != exchanges); 129 qs = TALER_MERCHANTDB_select_exchanges (TMH_db, 130 &add_exchange, 131 exchanges); 132 if (0 > qs) 133 { 134 GNUNET_break (0); 135 json_decref (exchanges); 136 return TALER_MHD_reply_with_error (connection, 137 MHD_HTTP_INTERNAL_SERVER_ERROR, 138 TALER_EC_GENERIC_DB_FETCH_FAILED, 139 "select_exchanges"); 140 } 141 return TALER_MHD_REPLY_JSON_PACK ( 142 connection, 143 MHD_HTTP_OK, 144 GNUNET_JSON_pack_array_steal ("exchanges", 145 exchanges)); 146 } 147 148 149 /* end of taler-merchant-httpd_get-exchanges.c */