taler-merchant-httpd_private-get-donau-instances.c (4945B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 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 taler-merchant-httpd_private-get-donau-instances.c 18 * @brief implementation of GET /donau 19 * @author Bohdan Potuzhnyi 20 * @author Vlada Svirsh 21 */ 22 #include "platform.h" 23 #include <jansson.h> 24 #include <taler/taler_json_lib.h> 25 #include <taler/taler_dbevents.h> 26 #include "taler_merchant_donau.h" 27 #include "taler_merchant_service.h" 28 #include "taler-merchant-httpd_private-get-donau-instances.h" 29 30 31 /** 32 * Add details about a Donau instance to the JSON array. 33 * 34 * @param cls json array to which the Donau instance details will be added 35 * @param donau_instance_serial the serial number of the Donau instance 36 * @param donau_url the URL of the Donau instance 37 * @param charity_name the name of the charity 38 * @param charity_pub_key the public key of the charity 39 * @param charity_id the ID of the charity 40 * @param charity_max_per_year the maximum donation amount per year 41 * @param charity_receipts_to_date the total donations received so far this year 42 * @param current_year the current year being tracked for donations 43 * @param donau_keys_json JSON object with key information specific to the Donau instance, NULL if not (yet) available. 44 */ 45 static void 46 add_donau_instance (void *cls, 47 uint64_t donau_instance_serial, 48 const char *donau_url, 49 const char *charity_name, 50 const struct DONAU_CharityPublicKeyP *charity_pub_key, 51 uint64_t charity_id, 52 const struct TALER_Amount *charity_max_per_year, 53 const struct TALER_Amount *charity_receipts_to_date, 54 int64_t current_year, 55 const json_t *donau_keys_json) 56 { 57 json_t *json_instances = cls; 58 59 GNUNET_assert ( 60 0 == json_array_append_new ( 61 json_instances, 62 GNUNET_JSON_PACK ( 63 GNUNET_JSON_pack_uint64 ("donau_instance_serial", 64 donau_instance_serial), 65 GNUNET_JSON_pack_string ("donau_url", 66 donau_url), 67 GNUNET_JSON_pack_string ("charity_name", 68 charity_name), 69 GNUNET_JSON_pack_data_auto ("charity_pub_key", 70 charity_pub_key), 71 GNUNET_JSON_pack_uint64 ("charity_id", 72 charity_id), 73 TALER_JSON_pack_amount ("charity_max_per_year", 74 charity_max_per_year), 75 TALER_JSON_pack_amount ("charity_receipts_to_date", 76 charity_receipts_to_date), 77 GNUNET_JSON_pack_int64 ("current_year", 78 current_year), 79 GNUNET_JSON_pack_allow_null ( 80 GNUNET_JSON_pack_object_incref ("donau_keys_json", 81 (json_t *) donau_keys_json)) 82 ))); 83 } 84 85 86 /** 87 * Handle a GET "/donau" request. 88 * 89 * @param rh context of the handler 90 * @param connection the MHD connection to handle 91 * @param[in,out] hc context with further information about the request 92 * @return MHD result code 93 */ 94 MHD_RESULT 95 TMH_private_get_donau_instances (const struct TMH_RequestHandler *rh, 96 struct MHD_Connection *connection, 97 struct TMH_HandlerContext *hc) 98 { 99 json_t *json_donau_instances = json_array (); 100 enum GNUNET_DB_QueryStatus qs; 101 102 TMH_db->preflight (TMH_db->cls); 103 qs = TMH_db->select_donau_instances (TMH_db->cls, 104 hc->instance->settings.id, 105 &add_donau_instance, 106 json_donau_instances); 107 if (0 > qs) 108 { 109 GNUNET_break (0); 110 json_decref (json_donau_instances); 111 return TALER_MHD_reply_with_error (connection, 112 MHD_HTTP_INTERNAL_SERVER_ERROR, 113 TALER_EC_GENERIC_DB_FETCH_FAILED, 114 NULL); 115 } 116 117 return TALER_MHD_REPLY_JSON_PACK (connection, 118 MHD_HTTP_OK, 119 GNUNET_JSON_pack_array_steal ( 120 "donau_instances", 121 json_donau_instances)); 122 }