exchange_api_management_set_wire_fee.c (6647B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020-2022 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU 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 15 <http://www.gnu.org/licenses/> 16 */ 17 /** 18 * @file lib/exchange_api_management_set_wire_fee.c 19 * @brief functions to set wire fees at an exchange 20 * @author Christian Grothoff 21 */ 22 #include "taler/platform.h" 23 #include "taler/taler_json_lib.h" 24 #include <gnunet/gnunet_curl_lib.h> 25 #include <microhttpd.h> 26 #include "exchange_api_curl_defaults.h" 27 #include "taler/taler_exchange_service.h" 28 #include "taler/taler_signatures.h" 29 #include "taler/taler_curl_lib.h" 30 #include "taler/taler_json_lib.h" 31 32 33 struct TALER_EXCHANGE_ManagementSetWireFeeHandle 34 { 35 36 /** 37 * The url for this request. 38 */ 39 char *url; 40 41 /** 42 * Minor context that holds body and headers. 43 */ 44 struct TALER_CURL_PostContext post_ctx; 45 46 /** 47 * Handle for the request. 48 */ 49 struct GNUNET_CURL_Job *job; 50 51 /** 52 * Function to call with the result. 53 */ 54 TALER_EXCHANGE_ManagementSetWireFeeCallback cb; 55 56 /** 57 * Closure for @a cb. 58 */ 59 void *cb_cls; 60 61 /** 62 * Reference to the execution context. 63 */ 64 struct GNUNET_CURL_Context *ctx; 65 }; 66 67 68 /** 69 * Function called when we're done processing the 70 * HTTP /management/wire request. 71 * 72 * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *` 73 * @param response_code HTTP response code, 0 on error 74 * @param response response body, NULL if not in JSON 75 */ 76 static void 77 handle_set_wire_fee_finished (void *cls, 78 long response_code, 79 const void *response) 80 { 81 struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh = cls; 82 const json_t *json = response; 83 struct TALER_EXCHANGE_ManagementSetWireFeeResponse swr = { 84 .hr.http_status = (unsigned int) response_code, 85 .hr.reply = json 86 }; 87 88 swfh->job = NULL; 89 switch (response_code) 90 { 91 case MHD_HTTP_NO_CONTENT: 92 break; 93 case MHD_HTTP_FORBIDDEN: 94 swr.hr.ec = TALER_JSON_get_error_code (json); 95 swr.hr.hint = TALER_JSON_get_error_hint (json); 96 break; 97 case MHD_HTTP_NOT_FOUND: 98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 99 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n", 100 swfh->url); 101 if (NULL != json) 102 { 103 swr.hr.ec = TALER_JSON_get_error_code (json); 104 swr.hr.hint = TALER_JSON_get_error_hint (json); 105 } 106 else 107 { 108 swr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 109 swr.hr.hint = TALER_ErrorCode_get_hint (swr.hr.ec); 110 } 111 break; 112 case MHD_HTTP_CONFLICT: 113 swr.hr.ec = TALER_JSON_get_error_code (json); 114 swr.hr.hint = TALER_JSON_get_error_hint (json); 115 break; 116 case MHD_HTTP_PRECONDITION_FAILED: 117 swr.hr.ec = TALER_JSON_get_error_code (json); 118 swr.hr.hint = TALER_JSON_get_error_hint (json); 119 break; 120 default: 121 /* unexpected response code */ 122 GNUNET_break_op (0); 123 swr.hr.ec = TALER_JSON_get_error_code (json); 124 swr.hr.hint = TALER_JSON_get_error_hint (json); 125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 126 "Unexpected response code %u/%d for exchange management set wire fee\n", 127 (unsigned int) response_code, 128 (int) swr.hr.ec); 129 break; 130 } 131 if (NULL != swfh->cb) 132 { 133 swfh->cb (swfh->cb_cls, 134 &swr); 135 swfh->cb = NULL; 136 } 137 TALER_EXCHANGE_management_set_wire_fees_cancel (swfh); 138 } 139 140 141 struct TALER_EXCHANGE_ManagementSetWireFeeHandle * 142 TALER_EXCHANGE_management_set_wire_fees ( 143 struct GNUNET_CURL_Context *ctx, 144 const char *exchange_base_url, 145 const char *wire_method, 146 struct GNUNET_TIME_Timestamp validity_start, 147 struct GNUNET_TIME_Timestamp validity_end, 148 const struct TALER_WireFeeSet *fees, 149 const struct TALER_MasterSignatureP *master_sig, 150 TALER_EXCHANGE_ManagementSetWireFeeCallback cb, 151 void *cb_cls) 152 { 153 struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh; 154 CURL *eh; 155 json_t *body; 156 157 swfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetWireFeeHandle); 158 swfh->cb = cb; 159 swfh->cb_cls = cb_cls; 160 swfh->ctx = ctx; 161 swfh->url = TALER_url_join (exchange_base_url, 162 "management/wire-fee", 163 NULL); 164 if (NULL == swfh->url) 165 { 166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 167 "Could not construct request URL.\n"); 168 GNUNET_free (swfh); 169 return NULL; 170 } 171 body = GNUNET_JSON_PACK ( 172 GNUNET_JSON_pack_string ("wire_method", 173 wire_method), 174 GNUNET_JSON_pack_data_auto ("master_sig", 175 master_sig), 176 GNUNET_JSON_pack_timestamp ("fee_start", 177 validity_start), 178 GNUNET_JSON_pack_timestamp ("fee_end", 179 validity_end), 180 TALER_JSON_pack_amount ("closing_fee", 181 &fees->closing), 182 TALER_JSON_pack_amount ("wire_fee", 183 &fees->wire)); 184 eh = TALER_EXCHANGE_curl_easy_get_ (swfh->url); 185 if ( (NULL == eh) || 186 (GNUNET_OK != 187 TALER_curl_easy_post (&swfh->post_ctx, 188 eh, 189 body)) ) 190 { 191 GNUNET_break (0); 192 if (NULL != eh) 193 curl_easy_cleanup (eh); 194 json_decref (body); 195 GNUNET_free (swfh->url); 196 GNUNET_free (swfh); 197 return NULL; 198 } 199 json_decref (body); 200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 201 "Requesting URL '%s'\n", 202 swfh->url); 203 swfh->job = GNUNET_CURL_job_add2 (ctx, 204 eh, 205 swfh->post_ctx.headers, 206 &handle_set_wire_fee_finished, 207 swfh); 208 if (NULL == swfh->job) 209 { 210 TALER_EXCHANGE_management_set_wire_fees_cancel (swfh); 211 return NULL; 212 } 213 return swfh; 214 } 215 216 217 void 218 TALER_EXCHANGE_management_set_wire_fees_cancel ( 219 struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh) 220 { 221 if (NULL != swfh->job) 222 { 223 GNUNET_CURL_job_cancel (swfh->job); 224 swfh->job = NULL; 225 } 226 TALER_curl_easy_post_finished (&swfh->post_ctx); 227 GNUNET_free (swfh->url); 228 GNUNET_free (swfh); 229 }