merchant_api_delete_donau_instance.c (5496B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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 /** 18 * @file merchant_api_delete_donau_instance.c 19 * @brief Implementation of the DELETE /donau/$charity_id request of the merchant's HTTP API 20 * @author Bohdan Potuzhnyi 21 * @author Vlada Svirsh 22 */ 23 24 #include "platform.h" 25 #include <curl/curl.h> 26 #include <jansson.h> 27 #include <microhttpd.h> 28 #include <gnunet/gnunet_util_lib.h> 29 #include <gnunet/gnunet_curl_lib.h> 30 #include "taler_merchant_service.h" 31 #include "merchant_api_curl_defaults.h" 32 #include <taler/taler_json_lib.h> 33 #include <taler/taler_signatures.h> 34 /* DONAU RELATED IMPORTS */ 35 #include "taler_merchant_donau.h" 36 #include <donau/donau_service.h> 37 38 39 /** 40 * Handle for a DELETE /donau/$charity_id operation. 41 */ 42 struct TALER_MERCHANT_DonauInstanceDeleteHandle 43 { 44 /** 45 * The URL for this request. 46 */ 47 char *url; 48 49 /** 50 * Handle for the request. 51 */ 52 struct GNUNET_CURL_Job *job; 53 54 /** 55 * Function to call with the result. 56 */ 57 TALER_MERCHANT_DonauInstanceDeleteCallback cb; 58 59 /** 60 * Closure for @a cb. 61 */ 62 void *cb_cls; 63 64 /** 65 * Reference to the execution context. 66 */ 67 struct GNUNET_CURL_Context *ctx; 68 }; 69 70 /** 71 * Function called when we're done processing the 72 * HTTP DELETE /donau/$charity_id request. 73 * 74 * @param cls the struct TALER_MERCHANT_DonauInstanceDeleteHandle 75 * @param response_code HTTP response code, 0 on error 76 * @param response response body, NULL if not in JSON 77 */ 78 static void 79 handle_delete_donau_instance_finished (void *cls, 80 long response_code, 81 const void *response) 82 { 83 struct TALER_MERCHANT_DonauInstanceDeleteHandle *ddh = cls; 84 const json_t *json = response; 85 struct TALER_MERCHANT_HttpResponse hr = { 86 .http_status = (unsigned int) response_code, 87 .reply = json 88 }; 89 90 ddh->job = NULL; 91 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 93 "Got /donau/$charity_id response with status code %u\n", 94 (unsigned int) response_code); 95 96 switch (response_code) 97 { 98 case MHD_HTTP_NO_CONTENT: 99 break; 100 case MHD_HTTP_NOT_FOUND: 101 case MHD_HTTP_UNAUTHORIZED: 102 hr.ec = TALER_JSON_get_error_code (json); 103 hr.hint = TALER_JSON_get_error_hint (json); 104 break; 105 default: 106 /* Unexpected response */ 107 hr.ec = TALER_JSON_get_error_code (json); 108 hr.hint = TALER_JSON_get_error_hint (json); 109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 110 "Unexpected response code %u/%d for DELETE /donau/$charity_id\n", 111 (unsigned int) response_code, 112 (int) hr.ec); 113 break; 114 } 115 ddh->cb (ddh->cb_cls, 116 &hr); 117 TALER_MERCHANT_donau_instance_delete_cancel (ddh); 118 } 119 120 121 /** 122 * Initiates the DELETE /donau/$charity_id operation. 123 * 124 * @param ctx CURL context 125 * @param backend_url Base URL for the backend 126 * @param charity_id The ID of the charity to delete 127 * @param cb Callback function to handle the response 128 * @param cb_cls Closure for @a cb 129 * @return the handle for the operation, or NULL on error 130 */ 131 struct TALER_MERCHANT_DonauInstanceDeleteHandle * 132 TALER_MERCHANT_donau_instance_delete ( 133 struct GNUNET_CURL_Context *ctx, 134 const char *backend_url, 135 uint64_t charity_id, 136 TALER_MERCHANT_DonauInstanceDeleteCallback cb, 137 void *cb_cls) 138 { 139 struct TALER_MERCHANT_DonauInstanceDeleteHandle *ddh; 140 char *charity_id_str; 141 CURL *eh; 142 143 GNUNET_asprintf (&charity_id_str, 144 "private/donau/%ld", 145 charity_id); 146 if (NULL == charity_id_str) 147 return NULL; 148 149 ddh = GNUNET_new (struct TALER_MERCHANT_DonauInstanceDeleteHandle); 150 ddh->ctx = ctx; 151 ddh->cb = cb; 152 ddh->cb_cls = cb_cls; 153 154 ddh->url = TALER_url_join (backend_url, 155 charity_id_str, 156 NULL); 157 GNUNET_free (charity_id_str); 158 159 if (NULL == ddh->url) 160 { 161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 162 "Could not construct request URL.\n"); 163 GNUNET_free (ddh); 164 return NULL; 165 } 166 167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 168 "Requesting URL '%s'\n", 169 ddh->url); 170 171 eh = TALER_MERCHANT_curl_easy_get_ (ddh->url); 172 GNUNET_assert (CURLE_OK == curl_easy_setopt (eh, 173 CURLOPT_CUSTOMREQUEST, 174 "DELETE")); 175 ddh->job = GNUNET_CURL_job_add (ctx, 176 eh, 177 &handle_delete_donau_instance_finished, 178 ddh); 179 180 return ddh; 181 } 182 183 184 /** 185 * Cancel the DELETE /donau/$charity_id operation. 186 * 187 * @param ddh Handle for the operation to cancel. 188 */ 189 void 190 TALER_MERCHANT_donau_instance_delete_cancel ( 191 struct TALER_MERCHANT_DonauInstanceDeleteHandle *ddh) 192 { 193 if (NULL != ddh->job) 194 GNUNET_CURL_job_cancel (ddh->job); 195 196 GNUNET_free (ddh->url); 197 GNUNET_free (ddh); 198 }