Line data Source code
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_drain_profits.c
19 : * @brief functions to set wire fees at an exchange
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "taler_json_lib.h"
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include "exchange_api_curl_defaults.h"
26 : #include "taler_exchange_service.h"
27 : #include "taler_signatures.h"
28 : #include "taler_curl_lib.h"
29 : #include "taler_json_lib.h"
30 :
31 :
32 : struct TALER_EXCHANGE_ManagementDrainProfitsHandle
33 : {
34 :
35 : /**
36 : * The url for this request.
37 : */
38 : char *url;
39 :
40 : /**
41 : * Minor context that holds body and headers.
42 : */
43 : struct TALER_CURL_PostContext post_ctx;
44 :
45 : /**
46 : * Handle for the request.
47 : */
48 : struct GNUNET_CURL_Job *job;
49 :
50 : /**
51 : * Function to call with the result.
52 : */
53 : TALER_EXCHANGE_ManagementDrainProfitsCallback cb;
54 :
55 : /**
56 : * Closure for @a cb.
57 : */
58 : void *cb_cls;
59 :
60 : /**
61 : * Reference to the execution context.
62 : */
63 : struct GNUNET_CURL_Context *ctx;
64 : };
65 :
66 :
67 : /**
68 : * Function called when we're done processing the
69 : * HTTP /management/drain request.
70 : *
71 : * @param cls the `struct TALER_EXCHANGE_ManagementDrainProfitsHandle *`
72 : * @param response_code HTTP response code, 0 on error
73 : * @param response response body, NULL if not in JSON
74 : */
75 : static void
76 0 : handle_drain_profits_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 0 : struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp = cls;
81 0 : const json_t *json = response;
82 0 : struct TALER_EXCHANGE_HttpResponse hr = {
83 0 : .http_status = (unsigned int) response_code,
84 : .reply = json
85 : };
86 :
87 0 : dp->job = NULL;
88 0 : switch (response_code)
89 : {
90 0 : case MHD_HTTP_NO_CONTENT:
91 0 : break;
92 0 : case MHD_HTTP_FORBIDDEN:
93 0 : hr.ec = TALER_JSON_get_error_code (json);
94 0 : hr.hint = TALER_JSON_get_error_hint (json);
95 0 : break;
96 0 : case MHD_HTTP_CONFLICT:
97 0 : hr.ec = TALER_JSON_get_error_code (json);
98 0 : hr.hint = TALER_JSON_get_error_hint (json);
99 0 : break;
100 0 : case MHD_HTTP_PRECONDITION_FAILED:
101 0 : hr.ec = TALER_JSON_get_error_code (json);
102 0 : hr.hint = TALER_JSON_get_error_hint (json);
103 0 : break;
104 0 : default:
105 : /* unexpected response code */
106 0 : GNUNET_break_op (0);
107 0 : hr.ec = TALER_JSON_get_error_code (json);
108 0 : hr.hint = TALER_JSON_get_error_hint (json);
109 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 : "Unexpected response code %u/%d for exchange management drain profits\n",
111 : (unsigned int) response_code,
112 : (int) hr.ec);
113 0 : break;
114 : }
115 0 : if (NULL != dp->cb)
116 : {
117 0 : dp->cb (dp->cb_cls,
118 : &hr);
119 0 : dp->cb = NULL;
120 : }
121 0 : TALER_EXCHANGE_management_drain_profits_cancel (dp);
122 0 : }
123 :
124 :
125 : struct TALER_EXCHANGE_ManagementDrainProfitsHandle *
126 0 : TALER_EXCHANGE_management_drain_profits (
127 : struct GNUNET_CURL_Context *ctx,
128 : const char *url,
129 : const struct TALER_WireTransferIdentifierRawP *wtid,
130 : const struct TALER_Amount *amount,
131 : struct GNUNET_TIME_Timestamp date,
132 : const char *account_section,
133 : const char *payto_uri,
134 : const struct TALER_MasterSignatureP *master_sig,
135 : TALER_EXCHANGE_ManagementDrainProfitsCallback cb,
136 : void *cb_cls)
137 : {
138 : struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp;
139 : CURL *eh;
140 : json_t *body;
141 :
142 0 : dp = GNUNET_new (struct TALER_EXCHANGE_ManagementDrainProfitsHandle);
143 0 : dp->cb = cb;
144 0 : dp->cb_cls = cb_cls;
145 0 : dp->ctx = ctx;
146 0 : dp->url = TALER_url_join (url,
147 : "management/drain",
148 : NULL);
149 0 : if (NULL == dp->url)
150 : {
151 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 : "Could not construct request URL.\n");
153 0 : GNUNET_free (dp);
154 0 : return NULL;
155 : }
156 0 : body = GNUNET_JSON_PACK (
157 : GNUNET_JSON_pack_string ("debit_account_section",
158 : account_section),
159 : GNUNET_JSON_pack_string ("credit_payto_uri",
160 : payto_uri),
161 : GNUNET_JSON_pack_data_auto ("wtid",
162 : wtid),
163 : GNUNET_JSON_pack_data_auto ("master_sig",
164 : master_sig),
165 : GNUNET_JSON_pack_timestamp ("date",
166 : date),
167 : TALER_JSON_pack_amount ("amount",
168 : amount));
169 0 : eh = TALER_EXCHANGE_curl_easy_get_ (dp->url);
170 0 : if ( (NULL == eh) ||
171 : (GNUNET_OK !=
172 0 : TALER_curl_easy_post (&dp->post_ctx,
173 : eh,
174 : body)) )
175 : {
176 0 : GNUNET_break (0);
177 0 : if (NULL != eh)
178 0 : curl_easy_cleanup (eh);
179 0 : json_decref (body);
180 0 : GNUNET_free (dp->url);
181 0 : return NULL;
182 : }
183 0 : json_decref (body);
184 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 : "Requesting URL '%s'\n",
186 : dp->url);
187 0 : dp->job = GNUNET_CURL_job_add2 (ctx,
188 : eh,
189 0 : dp->post_ctx.headers,
190 : &handle_drain_profits_finished,
191 : dp);
192 0 : if (NULL == dp->job)
193 : {
194 0 : TALER_EXCHANGE_management_drain_profits_cancel (dp);
195 0 : return NULL;
196 : }
197 0 : return dp;
198 : }
199 :
200 :
201 : void
202 0 : TALER_EXCHANGE_management_drain_profits_cancel (
203 : struct TALER_EXCHANGE_ManagementDrainProfitsHandle *dp)
204 : {
205 0 : if (NULL != dp->job)
206 : {
207 0 : GNUNET_CURL_job_cancel (dp->job);
208 0 : dp->job = NULL;
209 : }
210 0 : TALER_curl_easy_post_finished (&dp->post_ctx);
211 0 : GNUNET_free (dp->url);
212 0 : GNUNET_free (dp);
213 0 : }
|