taler-merchant-httpd_get-units.c (4231B)
1 /* 2 This file is part of TALER 3 (C) 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 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-units.c 18 * @brief implement GET /units and shared unit list response 19 * @author Bohdan Potuzhnyi 20 */ 21 #include "platform.h" 22 #include "taler-merchant-httpd_get-units.h" 23 #include "merchant-database/iterate_units.h" 24 25 26 /** 27 * Context for building a unit list response. 28 */ 29 struct UnitListContext 30 { 31 /** 32 * Unit definitions to return. 33 */ 34 json_t *units; 35 36 /** 37 * Omit inactive units from the public catalogue. 38 */ 39 bool active_only; 40 }; 41 42 43 /** 44 * Add unit details to the response array. 45 * 46 * @param cls a `struct UnitListContext *` 47 * @param unit_serial serial number of the unit 48 * @param ud unit details 49 */ 50 static void 51 add_unit (void *cls, 52 uint64_t unit_serial, 53 const struct TALER_MERCHANTDB_UnitDetails *ud) 54 { 55 const struct UnitListContext *ulc = cls; 56 json_t *ua = ulc->units; 57 58 if (ulc->active_only && 59 (! ud->unit_active)) 60 return; 61 62 GNUNET_assert ( 63 0 == 64 json_array_append_new ( 65 ua, 66 GNUNET_JSON_PACK ( 67 GNUNET_JSON_pack_uint64 ("unit_serial", 68 unit_serial), 69 GNUNET_JSON_pack_string ("unit", 70 ud->unit), 71 GNUNET_JSON_pack_string ("unit_name_long", 72 ud->unit_name_long), 73 GNUNET_JSON_pack_object_incref ("unit_name_long_i18n", 74 (json_t *) ud->unit_name_long_i18n), 75 GNUNET_JSON_pack_string ("unit_name_short", 76 ud->unit_name_short), 77 GNUNET_JSON_pack_object_incref ("unit_name_short_i18n", 78 (json_t *) ud->unit_name_short_i18n), 79 GNUNET_JSON_pack_bool ("unit_allow_fraction", 80 ud->unit_allow_fraction), 81 GNUNET_JSON_pack_uint64 ("unit_precision_level", 82 ud->unit_precision_level), 83 GNUNET_JSON_pack_bool ("unit_active", 84 ud->unit_active), 85 GNUNET_JSON_pack_bool ("unit_builtin", 86 ud->unit_builtin)))); 87 } 88 89 90 enum MHD_Result 91 TMH_reply_units (struct MHD_Connection *connection, 92 struct TMH_HandlerContext *hc, 93 bool active_only) 94 { 95 struct UnitListContext ulc = { 96 .units = json_array (), 97 .active_only = active_only 98 }; 99 enum GNUNET_DB_QueryStatus qs; 100 101 GNUNET_assert (NULL != ulc.units); 102 qs = TALER_MERCHANTDB_iterate_units (TMH_db, 103 hc->instance->settings.id, 104 &add_unit, 105 &ulc); 106 if (0 > qs) 107 { 108 GNUNET_break (0); 109 json_decref (ulc.units); 110 return TALER_MHD_reply_with_error (connection, 111 MHD_HTTP_INTERNAL_SERVER_ERROR, 112 TALER_EC_GENERIC_DB_FETCH_FAILED, 113 NULL); 114 } 115 return TALER_MHD_REPLY_JSON_PACK (connection, 116 MHD_HTTP_OK, 117 GNUNET_JSON_pack_array_steal ("units", 118 ulc.units)); 119 } 120 121 122 enum MHD_Result 123 TMH_get_units (const struct TMH_RequestHandler *rh, 124 struct MHD_Connection *connection, 125 struct TMH_HandlerContext *hc) 126 { 127 (void) rh; 128 return TMH_reply_units (connection, 129 hc, 130 true); 131 } 132 133 134 /* end of taler-merchant-httpd_get-units.c */