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