Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, 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 Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file merchant_api_delete_unit.c
18 : * @brief Implementation of DELETE /private/units/$ID
19 : * @author Bohdan Potuzhnyi
20 : */
21 : #include "platform.h"
22 : #include <curl/curl.h>
23 : #include <jansson.h>
24 : #include <microhttpd.h>
25 : #include <gnunet/gnunet_util_lib.h>
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler_merchant_service.h"
28 : #include "merchant_api_curl_defaults.h"
29 : #include "merchant_api_common.h"
30 : #include <taler/taler_json_lib.h>
31 :
32 :
33 : /**
34 : * Handle for a DELETE /private/units/$ID operation.
35 : */
36 : struct TALER_MERCHANT_UnitDeleteHandle
37 : {
38 : /**
39 : * Fully qualified request URL.
40 : */
41 : char *url;
42 :
43 : /**
44 : * In-flight CURL job.
45 : */
46 : struct GNUNET_CURL_Job *job;
47 :
48 : /**
49 : * Completion callback.
50 : */
51 : TALER_MERCHANT_UnitDeleteCallback cb;
52 :
53 : /**
54 : * Closure for @a cb.
55 : */
56 : void *cb_cls;
57 :
58 : /**
59 : * Execution context.
60 : */
61 : struct GNUNET_CURL_Context *ctx;
62 : };
63 :
64 :
65 : /**
66 : * Called when the HTTP request finishes.
67 : *
68 : * @param cls operation handle
69 : * @param response_code HTTP status (0 on failure)
70 : * @param response parsed JSON reply (NULL if unavailable)
71 : */
72 : static void
73 6 : handle_delete_unit_finished (void *cls,
74 : long response_code,
75 : const void *response)
76 : {
77 6 : struct TALER_MERCHANT_UnitDeleteHandle *udh = cls;
78 6 : const json_t *json = response;
79 6 : struct TALER_MERCHANT_HttpResponse hr = {
80 6 : .http_status = (unsigned int) response_code,
81 : .reply = json
82 : };
83 :
84 6 : udh->job = NULL;
85 6 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
86 : "DELETE /private/units finished with status %u\n",
87 : (unsigned int) response_code);
88 6 : switch (response_code)
89 : {
90 4 : case MHD_HTTP_NO_CONTENT:
91 4 : break;
92 2 : case MHD_HTTP_BAD_REQUEST:
93 : case MHD_HTTP_UNAUTHORIZED:
94 : case MHD_HTTP_FORBIDDEN:
95 : case MHD_HTTP_NOT_FOUND:
96 : case MHD_HTTP_CONFLICT:
97 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
98 2 : hr.ec = TALER_JSON_get_error_code (json);
99 2 : hr.hint = TALER_JSON_get_error_hint (json);
100 2 : break;
101 0 : case 0:
102 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
103 0 : break;
104 0 : default:
105 0 : TALER_MERCHANT_parse_error_details_ (json,
106 : response_code,
107 : &hr);
108 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
109 : "Unexpected response %u/%d for DELETE /private/units\n",
110 : (unsigned int) response_code,
111 : (int) hr.ec);
112 0 : GNUNET_break_op (0);
113 0 : break;
114 : }
115 6 : udh->cb (udh->cb_cls,
116 : &hr);
117 6 : TALER_MERCHANT_unit_delete_cancel (udh);
118 6 : }
119 :
120 :
121 : struct TALER_MERCHANT_UnitDeleteHandle *
122 6 : TALER_MERCHANT_unit_delete (struct GNUNET_CURL_Context *ctx,
123 : const char *backend_url,
124 : const char *unit_id,
125 : TALER_MERCHANT_UnitDeleteCallback cb,
126 : void *cb_cls)
127 : {
128 : struct TALER_MERCHANT_UnitDeleteHandle *udh;
129 : CURL *eh;
130 : char *path;
131 :
132 6 : GNUNET_asprintf (&path,
133 : "private/units/%s",
134 : unit_id);
135 6 : udh = GNUNET_new (struct TALER_MERCHANT_UnitDeleteHandle);
136 6 : udh->ctx = ctx;
137 6 : udh->cb = cb;
138 6 : udh->cb_cls = cb_cls;
139 6 : udh->url = TALER_url_join (backend_url,
140 : path,
141 : NULL);
142 6 : GNUNET_free (path);
143 6 : if (NULL == udh->url)
144 : {
145 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 : "Failed to build /private/units/%s URL\n",
147 : unit_id);
148 0 : GNUNET_free (udh);
149 0 : return NULL;
150 : }
151 6 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 : "Requesting DELETE on '%s'\n",
153 : udh->url);
154 6 : eh = TALER_MERCHANT_curl_easy_get_ (udh->url);
155 6 : GNUNET_assert (CURLE_OK ==
156 : curl_easy_setopt (eh,
157 : CURLOPT_CUSTOMREQUEST,
158 : MHD_HTTP_METHOD_DELETE));
159 6 : udh->job = GNUNET_CURL_job_add (ctx,
160 : eh,
161 : &handle_delete_unit_finished,
162 : udh);
163 6 : return udh;
164 : }
165 :
166 :
167 : void
168 6 : TALER_MERCHANT_unit_delete_cancel (struct TALER_MERCHANT_UnitDeleteHandle *udh)
169 : {
170 6 : if (NULL != udh->job)
171 0 : GNUNET_CURL_job_cancel (udh->job);
172 6 : GNUNET_free (udh->url);
173 6 : GNUNET_free (udh);
174 6 : }
175 :
176 :
177 : /* end of merchant_api_delete_unit.c */
|