exchange_api_management_drain_profits.c (6067B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020-2023 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_drain_profits.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_ManagementDrainProfitsHandle 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_ManagementDrainProfitsCallback 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/drain request. 71 * 72 * @param cls the `struct TALER_EXCHANGE_ManagementDrainProfitsHandle *` 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_drain_profits_finished (void *cls, 78 long response_code, 79 const void *response) 80 { 81 struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp = cls; 82 const json_t *json = response; 83 struct TALER_EXCHANGE_ManagementDrainResponse dr = { 84 .hr.http_status = (unsigned int) response_code, 85 .hr.reply = json 86 }; 87 88 dp->job = NULL; 89 switch (response_code) 90 { 91 case MHD_HTTP_NO_CONTENT: 92 break; 93 case MHD_HTTP_FORBIDDEN: 94 dr.hr.ec = TALER_JSON_get_error_code (json); 95 dr.hr.hint = TALER_JSON_get_error_hint (json); 96 break; 97 case MHD_HTTP_CONFLICT: 98 dr.hr.ec = TALER_JSON_get_error_code (json); 99 dr.hr.hint = TALER_JSON_get_error_hint (json); 100 break; 101 case MHD_HTTP_PRECONDITION_FAILED: 102 dr.hr.ec = TALER_JSON_get_error_code (json); 103 dr.hr.hint = TALER_JSON_get_error_hint (json); 104 break; 105 default: 106 /* unexpected response code */ 107 GNUNET_break_op (0); 108 dr.hr.ec = TALER_JSON_get_error_code (json); 109 dr.hr.hint = TALER_JSON_get_error_hint (json); 110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 111 "Unexpected response code %u/%d for exchange management drain profits\n", 112 (unsigned int) response_code, 113 (int) dr.hr.ec); 114 break; 115 } 116 if (NULL != dp->cb) 117 { 118 dp->cb (dp->cb_cls, 119 &dr); 120 dp->cb = NULL; 121 } 122 TALER_EXCHANGE_management_drain_profits_cancel (dp); 123 } 124 125 126 struct TALER_EXCHANGE_ManagementDrainProfitsHandle * 127 TALER_EXCHANGE_management_drain_profits ( 128 struct GNUNET_CURL_Context *ctx, 129 const char *url, 130 const struct TALER_WireTransferIdentifierRawP *wtid, 131 const struct TALER_Amount *amount, 132 struct GNUNET_TIME_Timestamp date, 133 const char *account_section, 134 const struct TALER_FullPayto payto_uri, 135 const struct TALER_MasterSignatureP *master_sig, 136 TALER_EXCHANGE_ManagementDrainProfitsCallback cb, 137 void *cb_cls) 138 { 139 struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp; 140 CURL *eh; 141 json_t *body; 142 143 dp = GNUNET_new (struct TALER_EXCHANGE_ManagementDrainProfitsHandle); 144 dp->cb = cb; 145 dp->cb_cls = cb_cls; 146 dp->ctx = ctx; 147 dp->url = TALER_url_join (url, 148 "management/drain", 149 NULL); 150 if (NULL == dp->url) 151 { 152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 153 "Could not construct request URL.\n"); 154 GNUNET_free (dp); 155 return NULL; 156 } 157 body = GNUNET_JSON_PACK ( 158 GNUNET_JSON_pack_string ("debit_account_section", 159 account_section), 160 TALER_JSON_pack_full_payto ("credit_payto_uri", 161 payto_uri), 162 GNUNET_JSON_pack_data_auto ("wtid", 163 wtid), 164 GNUNET_JSON_pack_data_auto ("master_sig", 165 master_sig), 166 GNUNET_JSON_pack_timestamp ("date", 167 date), 168 TALER_JSON_pack_amount ("amount", 169 amount)); 170 eh = TALER_EXCHANGE_curl_easy_get_ (dp->url); 171 if ( (NULL == eh) || 172 (GNUNET_OK != 173 TALER_curl_easy_post (&dp->post_ctx, 174 eh, 175 body)) ) 176 { 177 GNUNET_break (0); 178 if (NULL != eh) 179 curl_easy_cleanup (eh); 180 json_decref (body); 181 GNUNET_free (dp->url); 182 return NULL; 183 } 184 json_decref (body); 185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 186 "Requesting URL '%s'\n", 187 dp->url); 188 dp->job = GNUNET_CURL_job_add2 (ctx, 189 eh, 190 dp->post_ctx.headers, 191 &handle_drain_profits_finished, 192 dp); 193 if (NULL == dp->job) 194 { 195 TALER_EXCHANGE_management_drain_profits_cancel (dp); 196 return NULL; 197 } 198 return dp; 199 } 200 201 202 void 203 TALER_EXCHANGE_management_drain_profits_cancel ( 204 struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp) 205 { 206 if (NULL != dp->job) 207 { 208 GNUNET_CURL_job_cancel (dp->job); 209 dp->job = NULL; 210 } 211 TALER_curl_easy_post_finished (&dp->post_ctx); 212 GNUNET_free (dp->url); 213 GNUNET_free (dp); 214 }