diff options
author | priscilla <priscilla.huang@efrei.net> | 2022-11-22 11:17:32 -0500 |
---|---|---|
committer | priscilla <priscilla.huang@efrei.net> | 2022-11-22 11:17:32 -0500 |
commit | e9735103820c6ed8fb74ef1afec44ee4be20e77c (patch) | |
tree | b267703f0bbb6c795c0c9633268b25ecf66c7f38 | |
parent | 3f6330059b2c60f83a37994a991d70d8f38ce314 (diff) |
api template
-rw-r--r-- | src/lib/Makefile.am | 5 | ||||
-rw-r--r-- | src/lib/merchant_api_delete_template.c | 184 | ||||
-rw-r--r-- | src/lib/merchant_api_get_template.c | 211 | ||||
-rw-r--r-- | src/lib/merchant_api_patch_template.c | 247 |
4 files changed, 647 insertions, 0 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index dec8712f..93870ece 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -21,6 +21,7 @@ libtalermerchant_la_SOURCES = \ merchant_api_delete_product.c \ merchant_api_delete_reserve.c \ merchant_api_delete_transfer.c \ + merchant_api_delete_template.c \ merchant_api_get_config.c \ merchant_api_get_instance.c \ merchant_api_get_instances.c \ @@ -32,12 +33,15 @@ libtalermerchant_la_SOURCES = \ merchant_api_get_reserves.c \ merchant_api_get_tips.c \ merchant_api_get_transfers.c \ + merchant_api_get_template.c \ + merchant_api_get_templates.c \ merchant_api_lock_product.c \ merchant_api_merchant_get_order.c \ merchant_api_merchant_get_tip.c \ merchant_api_patch_instance.c \ merchant_api_patch_order_forget.c \ merchant_api_patch_product.c \ + merchant_api_patch_template.c \ merchant_api_post_instance_auth.c \ merchant_api_post_instances.c \ merchant_api_post_orders.c \ @@ -49,6 +53,7 @@ libtalermerchant_la_SOURCES = \ merchant_api_post_products.c \ merchant_api_post_reserves.c \ merchant_api_post_transfers.c \ + merchant_api_post_templates.c \ merchant_api_tip_authorize.c \ merchant_api_tip_pickup.c \ merchant_api_tip_pickup2.c \ diff --git a/src/lib/merchant_api_delete_template.c b/src/lib/merchant_api_delete_template.c new file mode 100644 index 00000000..e044c07f --- /dev/null +++ b/src/lib/merchant_api_delete_template.c @@ -0,0 +1,184 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 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 + <http://www.gnu.org/licenses/> +*/ +/** + * @file merchant_api_delete_template.c + * @brief Implementation of the DELETE /template/$ID request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a DELETE /template/$ID operation. + */ +struct TALER_MERCHANT_TemplateDeleteHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_TemplateDeleteCallback 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 GET /template/$ID request. + * + * @param cls the `struct TALER_MERCHANT_TemplateDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_delete_template_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_TemplateDeleteHandle *tdh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + tdh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /template/$ID response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_CONFLICT: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + break; + } + tdh->cb (tdh->cb_cls, + &hr); + TALER_MERCHANT_template_delete_cancel (tdh); +} + + +struct TALER_MERCHANT_TemplateDeleteHandle * +TALER_MERCHANT_template_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *template_id, + TALER_MERCHANT_TemplateDeleteCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_TemplateDeleteHandle *tdh; + + tdh = GNUNET_new (struct TALER_MERCHANT_TemplateDeleteHandle); + tdh->ctx = ctx; + tdh->cb = cb; + tdh->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/template/%s", + template_id); + tdh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == tdh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (tdh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + tdh->url); + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (tdh->url); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + tdh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_delete_template_finished, + tdh); + } + return tdh; +} + + +void +TALER_MERCHANT_template_delete_cancel ( + struct TALER_MERCHANT_TemplateDeleteHandle *tdh) +{ + if (NULL != tdh->job) + GNUNET_CURL_job_cancel (tdh->job); + GNUNET_free (tdh->url); + GNUNET_free (tdh); +} diff --git a/src/lib/merchant_api_get_template.c b/src/lib/merchant_api_get_template.c new file mode 100644 index 00000000..01e4a91e --- /dev/null +++ b/src/lib/merchant_api_get_template.c @@ -0,0 +1,211 @@ +/* + This file is part of TALER + Copyright (C) 2014-2018, 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 + <http://www.gnu.org/licenses/> +*/ +/** + * @file merchant_api_get_template.c + * @brief Implementation of the GET /template/$ID request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_signatures.h> + + +/** + * Handle for a GET /template/$ID operation. + */ +struct TALER_MERCHANT_TemplateGetHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_TemplateGetCallback 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 GET /template/$ID request. + * + * @param cls the `struct TALER_MERCHANT_TemplateGetHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_get_template_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_TemplateGetHandle *tgh = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + tgh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /template/$ID response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_OK: + { + const char *template_description; + const char *image; + json_t *template_contract; + bool rst_ok = true; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("template_description", + &template_description), + GNUNET_JSON_spec_string ("image", + &image), + GNUNET_JSON_spec_json ("template_contract", + &template_contract), + GNUNET_JSON_spec_end () + }; + + + if ( (rst_ok) && + (GNUNET_OK == + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) ) + { + tgh->cb (tgh->cb_cls, + &hr, + template_description, + image, + template_contract); + GNUNET_JSON_parse_free (spec); + TALER_MERCHANT_template_get_cancel (tgh); + return; + } + hr.http_status = 0; + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + GNUNET_JSON_parse_free (spec); + break; + } + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + default: + /* unexpected response code */ + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + break; + } + tgh->cb (tgh->cb_cls, + &hr, + NULL, + NULL, + NULL); + TALER_MERCHANT_template_get_cancel (tgh); +} + + +struct TALER_MERCHANT_TemplateGetHandle * +TALER_MERCHANT_template_get ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *template_id, + TALER_MERCHANT_TemplateGetCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_TemplateGetHandle *tgh; + CURL *eh; + + tgh = GNUNET_new (struct TALER_MERCHANT_TemplateGetHandle); + tgh->ctx = ctx; + tgh->cb = cb; + tgh->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/template/%s", + template_id); + tgh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == tgh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (tgh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + tgh->url); + eh = TALER_MERCHANT_curl_easy_get_ (tgh->url); + tgh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_get_template_finished, + tgh); + return tgh; +} + + +void +TALER_MERCHANT_template_get_cancel ( + struct TALER_MERCHANT_TemplateGetHandle *tgh) +{ + if (NULL != tgh->job) + GNUNET_CURL_job_cancel (tgh->job); + GNUNET_free (tgh->url); + GNUNET_free (tgh); +} diff --git a/src/lib/merchant_api_patch_template.c b/src/lib/merchant_api_patch_template.c new file mode 100644 index 00000000..ba66a94f --- /dev/null +++ b/src/lib/merchant_api_patch_template.c @@ -0,0 +1,247 @@ +/* + This file is part of TALER + Copyright (C) 2020, 2021 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 <http://www.gnu.org/licenses/> +*/ +/** + * @file merchant_api_patch_template.c + * @brief Implementation of the PATCH /template/$ID request + * of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include <curl/curl.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include <taler/taler_json_lib.h> +#include <taler/taler_curl_lib.h> + + +/** + * Handle for a PATCH /template/$ID operation. + */ +struct TALER_MERCHANT_TemplatePatchHandle +{ + + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_TemplatePatchCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + + /** + * Minor context that holds body and headers. + */ + struct TALER_CURL_PostContext post_ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP PATCH /template/$ID request. + * + * @param cls the `struct TALER_MERCHANT_TemplatePatchHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_patch_template_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_TemplatePatchHandle *tph = cls; + const json_t *json = response; + struct TALER_MERCHANT_HttpResponse hr = { + .http_status = (unsigned int) response_code, + .reply = json + }; + + tph->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "PATCH /template/$ID completed with response code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case 0: + hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_BAD_REQUEST: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_break_op (0); + /* This should never happen, either us + * or the merchant is buggy (or API version conflict); + * just pass JSON reply to the application */ + break; + case MHD_HTTP_UNAUTHORIZED: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_FORBIDDEN: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we tried to abort the payment + * after it was successful. We should pass the JSON reply to the + * application */ + break; + case MHD_HTTP_NOT_FOUND: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_CONFLICT: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + hr.ec = TALER_JSON_get_error_code (json); + hr.hint = TALER_JSON_get_error_hint (json); + /* Server had an internal issue; we should retry, + but this API leaves this to the application */ + break; + default: + TALER_MERCHANT_parse_error_details_ (json, + response_code, + &hr); + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) hr.ec); + GNUNET_break_op (0); + break; + } + tph->cb (tph->cb_cls, + &hr); + TALER_MERCHANT_template_patch_cancel (tph); +} + + +struct TALER_MERCHANT_TemplatePatchHandle * +TALER_MERCHANT_template_patch ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const char *template_id, + const char *template_description, + const char *image, + const json_t *template_contract, + TALER_MERCHANT_TemplatePatchCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_TemplatePatchHandle *tph; + json_t *req_obj; + + req_obj = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("template_description", + template_description), + GNUNET_JSON_pack_string ("image", + image), + GNUNET_JSON_pack_object_incref ("template_contract", + (json_t *) template_contract)); + tph = GNUNET_new (struct TALER_MERCHANT_TemplatePatchHandle); + tph->ctx = ctx; + tph->cb = cb; + tph->cb_cls = cb_cls; + { + char *path; + + GNUNET_asprintf (&path, + "private/template/%s", + template_id); + tph->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == tph->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + json_decref (req_obj); + GNUNET_free (tph); + return NULL; + } + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (tph->url); + if (GNUNET_OK != + TALER_curl_easy_post (&tph->post_ctx, + eh, + req_obj)) + { + GNUNET_break (0); + curl_easy_cleanup (eh); + json_decref (req_obj); + GNUNET_free (tph); + return NULL; + } + json_decref (req_obj); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_PATCH)); + tph->job = GNUNET_CURL_job_add2 (ctx, + eh, + tph->post_ctx.headers, + &handle_patch_template_finished, + tph); + } + return tph; +} + + +void +TALER_MERCHANT_template_patch_cancel ( + struct TALER_MERCHANT_TemplatePatchHandle *tph) +{ + if (NULL != tph->job) + { + GNUNET_CURL_job_cancel (tph->job); + tph->job = NULL; + } + TALER_curl_easy_post_finished (&tph->post_ctx); + GNUNET_free (tph->url); + GNUNET_free (tph); +} + + +/* end of merchant_api_patch_template.c */ |