From 9bae03573f31f22893839bbbbdaeba94821e3a57 Mon Sep 17 00:00:00 2001 From: Jonathan Buchanan Date: Fri, 19 Jun 2020 21:06:03 -0400 Subject: implement library method for DELETE /orders/ and tests --- src/lib/merchant_api_delete_order.c | 188 ++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/lib/merchant_api_delete_order.c (limited to 'src/lib/merchant_api_delete_order.c') diff --git a/src/lib/merchant_api_delete_order.c b/src/lib/merchant_api_delete_order.c new file mode 100644 index 00000000..4b08e8ca --- /dev/null +++ b/src/lib/merchant_api_delete_order.c @@ -0,0 +1,188 @@ +/* + This file is part of TALER + Copyright (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + +*/ +/** + * @file lib/merchant_api_delete_order.c + * @brief Implementation of the DELETE /orders/$ORDER_ID request of the merchant's HTTP API + * @author Jonathan Buchanan + */ +#include "platform.h" +#include +#include +#include /* just for HTTP status codes */ +#include +#include +#include "taler_merchant_service.h" +#include +#include + +/** + * Handle for a DELETE /orders/$ID operation. + */ +struct TALER_MERCHANT_OrderDeleteHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_OrderDeleteCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; +}; + + +/** + * Function called when we're done processing the + * HTTP DELETE /orders/$ORDER_ID request. + * + * @param cls the `struct TALER_MERCHANT_OrderDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param json response body, NULL if not in JSON + */ +static void +handle_delete_order_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_OrderDeleteHandle *odh = cls; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = NULL, + }; + + odh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /orders/$ID response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_NOT_FOUND: + break; + case MHD_HTTP_CONFLICT: + break; + default: + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u\n", + (unsigned int) response_code); + break; + } + odh->cb (odh->cb_cls, + &hr); + TALER_MERCHANT_order_delete_cancel (odh); +} + + +/** + * Make a DELETE /orders/$ID request to delete a order from our + * inventory. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param order_id identifier of the order + * @param cb function to call with the backend's deletion status + * @param cb_cls closure for @a cb + * @return the request handle; NULL upon error + */ +struct TALER_MERCHANT_OrderDeleteHandle * +TALER_MERCHANT_order_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *order_id, + TALER_MERCHANT_OrderDeleteCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_OrderDeleteHandle *odh; + + odh = GNUNET_new (struct TALER_MERCHANT_OrderDeleteHandle); + odh->ctx = ctx; + odh->cb = cb; + odh->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/orders/%s", + order_id); + + odh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == odh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request url.\n"); + GNUNET_free (odh); + return NULL; + } + + { + CURL *eh; + + eh = curl_easy_init (); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_URL, + odh->url)); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + odh->job = GNUNET_CURL_job_add (ctx, + eh, + GNUNET_YES, + &handle_delete_order_finished, + odh); + } + return odh; +} + + +/** + * Cancel DELETE /orders/$ID operation. + * + * @param odh operation to cancel + */ +void +TALER_MERCHANT_order_delete_cancel ( + struct TALER_MERCHANT_OrderDeleteHandle *odh) +{ + if (NULL != odh->job) + GNUNET_CURL_job_cancel (odh->job); + GNUNET_free (odh->url); + GNUNET_free (odh); +} -- cgit v1.2.3