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_set_global_fee.c
19 : * @brief functions to set global 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_ManagementSetGlobalFeeHandle
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_ManagementSetGlobalFeeCallback 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/global request.
70 : *
71 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
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_set_global_fee_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 0 : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh = 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 : sgfh->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 set global fee\n",
111 : (unsigned int) response_code,
112 : (int) hr.ec);
113 0 : break;
114 : }
115 0 : if (NULL != sgfh->cb)
116 : {
117 0 : sgfh->cb (sgfh->cb_cls,
118 : &hr);
119 0 : sgfh->cb = NULL;
120 : }
121 0 : TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
122 0 : }
123 :
124 :
125 : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *
126 0 : TALER_EXCHANGE_management_set_global_fees (
127 : struct GNUNET_CURL_Context *ctx,
128 : const char *exchange_base_url,
129 : struct GNUNET_TIME_Timestamp validity_start,
130 : struct GNUNET_TIME_Timestamp validity_end,
131 : const struct TALER_GlobalFeeSet *fees,
132 : struct GNUNET_TIME_Relative purse_timeout,
133 : struct GNUNET_TIME_Relative kyc_timeout,
134 : struct GNUNET_TIME_Relative history_expiration,
135 : uint32_t purse_account_limit,
136 : const struct TALER_MasterSignatureP *master_sig,
137 : TALER_EXCHANGE_ManagementSetGlobalFeeCallback cb,
138 : void *cb_cls)
139 : {
140 : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh;
141 : CURL *eh;
142 : json_t *body;
143 :
144 0 : sgfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle);
145 0 : sgfh->cb = cb;
146 0 : sgfh->cb_cls = cb_cls;
147 0 : sgfh->ctx = ctx;
148 0 : sgfh->url = TALER_url_join (exchange_base_url,
149 : "management/global-fee",
150 : NULL);
151 0 : if (NULL == sgfh->url)
152 : {
153 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 : "Could not construct request URL.\n");
155 0 : GNUNET_free (sgfh);
156 0 : return NULL;
157 : }
158 0 : body = GNUNET_JSON_PACK (
159 : GNUNET_JSON_pack_data_auto ("master_sig",
160 : master_sig),
161 : GNUNET_JSON_pack_timestamp ("fee_start",
162 : validity_start),
163 : GNUNET_JSON_pack_timestamp ("fee_end",
164 : validity_end),
165 : TALER_JSON_pack_amount ("history_fee",
166 : &fees->history),
167 : TALER_JSON_pack_amount ("kyc_fee",
168 : &fees->kyc),
169 : TALER_JSON_pack_amount ("account_fee",
170 : &fees->account),
171 : TALER_JSON_pack_amount ("purse_fee",
172 : &fees->purse),
173 : GNUNET_JSON_pack_time_rel ("purse_timeout",
174 : purse_timeout),
175 : GNUNET_JSON_pack_time_rel ("kyc_timeout",
176 : kyc_timeout),
177 : GNUNET_JSON_pack_time_rel ("history_expiration",
178 : history_expiration),
179 : GNUNET_JSON_pack_uint64 ("purse_account_limit",
180 : purse_account_limit));
181 0 : eh = TALER_EXCHANGE_curl_easy_get_ (sgfh->url);
182 0 : if ( (NULL == eh) ||
183 : (GNUNET_OK !=
184 0 : TALER_curl_easy_post (&sgfh->post_ctx,
185 : eh,
186 : body)) )
187 : {
188 0 : GNUNET_break (0);
189 0 : if (NULL != eh)
190 0 : curl_easy_cleanup (eh);
191 0 : json_decref (body);
192 0 : GNUNET_free (sgfh->url);
193 0 : return NULL;
194 : }
195 0 : json_decref (body);
196 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 : "Requesting URL '%s'\n",
198 : sgfh->url);
199 0 : sgfh->job = GNUNET_CURL_job_add2 (ctx,
200 : eh,
201 0 : sgfh->post_ctx.headers,
202 : &handle_set_global_fee_finished,
203 : sgfh);
204 0 : if (NULL == sgfh->job)
205 : {
206 0 : TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
207 0 : return NULL;
208 : }
209 0 : return sgfh;
210 : }
211 :
212 :
213 : void
214 0 : TALER_EXCHANGE_management_set_global_fees_cancel (
215 : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh)
216 : {
217 0 : if (NULL != sgfh->job)
218 : {
219 0 : GNUNET_CURL_job_cancel (sgfh->job);
220 0 : sgfh->job = NULL;
221 : }
222 0 : TALER_curl_easy_post_finished (&sgfh->post_ctx);
223 0 : GNUNET_free (sgfh->url);
224 0 : GNUNET_free (sgfh);
225 0 : }
|