merchant_api_delete_unit.c (4922B)
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 handle_delete_unit_finished (void *cls, 74 long response_code, 75 const void *response) 76 { 77 struct TALER_MERCHANT_UnitDeleteHandle *udh = cls; 78 const json_t *json = response; 79 struct TALER_MERCHANT_HttpResponse hr = { 80 .http_status = (unsigned int) response_code, 81 .reply = json 82 }; 83 84 udh->job = NULL; 85 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 86 "DELETE /private/units finished with status %u\n", 87 (unsigned int) response_code); 88 switch (response_code) 89 { 90 case MHD_HTTP_NO_CONTENT: 91 break; 92 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 hr.ec = TALER_JSON_get_error_code (json); 99 hr.hint = TALER_JSON_get_error_hint (json); 100 break; 101 case 0: 102 hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 103 break; 104 default: 105 TALER_MERCHANT_parse_error_details_ (json, 106 response_code, 107 &hr); 108 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 GNUNET_break_op (0); 113 break; 114 } 115 udh->cb (udh->cb_cls, 116 &hr); 117 TALER_MERCHANT_unit_delete_cancel (udh); 118 } 119 120 121 struct TALER_MERCHANT_UnitDeleteHandle * 122 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 GNUNET_asprintf (&path, 133 "private/units/%s", 134 unit_id); 135 udh = GNUNET_new (struct TALER_MERCHANT_UnitDeleteHandle); 136 udh->ctx = ctx; 137 udh->cb = cb; 138 udh->cb_cls = cb_cls; 139 udh->url = TALER_url_join (backend_url, 140 path, 141 NULL); 142 GNUNET_free (path); 143 if (NULL == udh->url) 144 { 145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 146 "Failed to build /private/units/%s URL\n", 147 unit_id); 148 GNUNET_free (udh); 149 return NULL; 150 } 151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 152 "Requesting DELETE on '%s'\n", 153 udh->url); 154 eh = TALER_MERCHANT_curl_easy_get_ (udh->url); 155 GNUNET_assert (CURLE_OK == 156 curl_easy_setopt (eh, 157 CURLOPT_CUSTOMREQUEST, 158 MHD_HTTP_METHOD_DELETE)); 159 udh->job = GNUNET_CURL_job_add (ctx, 160 eh, 161 &handle_delete_unit_finished, 162 udh); 163 return udh; 164 } 165 166 167 void 168 TALER_MERCHANT_unit_delete_cancel (struct TALER_MERCHANT_UnitDeleteHandle *udh) 169 { 170 if (NULL != udh->job) 171 GNUNET_CURL_job_cancel (udh->job); 172 GNUNET_free (udh->url); 173 GNUNET_free (udh); 174 } 175 176 177 /* end of merchant_api_delete_unit.c */