gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] 03/03: update


From: gnunet
Subject: [taler-merchant] 03/03: update
Date: Tue, 22 Nov 2022 14:15:00 +0100

This is an automated email from the git hooks/post-receive script.

priscilla-huang pushed a commit to branch master
in repository merchant.

commit 3f6330059b2c60f83a37994a991d70d8f38ce314
Author: priscilla <priscilla.huang@efrei.net>
AuthorDate: Tue Nov 22 08:14:31 2022 -0500

    update
---
 src/include/taler_merchantdb_lib.h    |   9 ++
 src/include/taler_merchantdb_plugin.h |   6 +-
 src/lib/merchant_api_get_templates.c  | 251 ++++++++++++++++++++++++++++++++++
 src/lib/merchant_api_post_templates.c | 233 +++++++++++++++++++++++++++++++
 4 files changed, 496 insertions(+), 3 deletions(-)

diff --git a/src/include/taler_merchantdb_lib.h 
b/src/include/taler_merchantdb_lib.h
index 9cce9aac..8d84db79 100644
--- a/src/include/taler_merchantdb_lib.h
+++ b/src/include/taler_merchantdb_lib.h
@@ -59,6 +59,15 @@ TALER_MERCHANTDB_product_details_free (
   struct TALER_MERCHANTDB_ProductDetails *pd);
 
 
+/**
+ * Free members of @a tp, but not @a tp itself.
+ *
+ * @param[in] tp template details to clean up
+ */
+void
+TALER_MERCHANTDB_template_details_free (
+  struct TALER_MERCHANTDB_TemplateDetails *tp);
+
 #endif  /* MERCHANT_DB_H */
 
 /* end of taler_merchantdb_lib.h */
diff --git a/src/include/taler_merchantdb_plugin.h 
b/src/include/taler_merchantdb_plugin.h
index 51c5ec02..bfc2d51a 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -321,17 +321,17 @@ struct TALER_MERCHANTDB_TemplateDetails
   /**
    * Description of the template.
    */
-  const char *template_description;
+  char *template_description;
 
   /**
    * Base64-encoded product image, or NULL.
    */
-  const char *image;
+  char *image;
 
   /**
    * In this template contract, we can have additional information.
    */
-  const json_t *template_contract;
+  json_t *template_contract;
 };
 
 
diff --git a/src/lib/merchant_api_get_templates.c 
b/src/lib/merchant_api_get_templates.c
new file mode 100644
index 00000000..7082ee23
--- /dev/null
+++ b/src/lib/merchant_api_get_templates.c
@@ -0,0 +1,251 @@
+/*
+  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_templates.c
+ * @brief Implementation of the GET /templates 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 /products operation.
+ */
+struct TALER_MERCHANT_TemplatesGetHandle
+{
+  /**
+   * The url for this request.
+   */
+  char *url;
+
+  /**
+   * Handle for the request.
+   */
+  struct GNUNET_CURL_Job *job;
+
+  /**
+   * Function to call with the result.
+   */
+  TALER_MERCHANT_TemplatesGetCallback cb;
+
+  /**
+   * Closure for @a cb.
+   */
+  void *cb_cls;
+
+  /**
+   * Reference to the execution context.
+   */
+  struct GNUNET_CURL_Context *ctx;
+
+};
+
+
+/**
+ * Parse template information from @a ia.
+ *
+ * @param ia JSON array (or NULL!) with template data
+ * @param pgh operation handle
+ * @return #GNUNET_OK on success
+ */
+static int
+parse_templates (const json_t *ia,
+                struct TALER_MERCHANT_TemplatesGetHandle *tgh)
+{
+  unsigned int ies_len = json_array_size (ia);
+  struct TALER_MERCHANT_TemplateEntry ies[ies_len];
+  size_t index;
+  json_t *value;
+  int ret;
+
+  ret = GNUNET_OK;
+  json_array_foreach (ia, index, value) {
+    struct TALER_MERCHANT_TemplateEntry *ie = &ies[index];
+    struct GNUNET_JSON_Specification spec[] = {
+      GNUNET_JSON_spec_string ("template_id",
+                               &ie->template_id),
+      GNUNET_JSON_spec_end ()
+    };
+
+    if (GNUNET_OK !=
+        GNUNET_JSON_parse (value,
+                           spec,
+                           NULL, NULL))
+    {
+      GNUNET_break_op (0);
+      ret = GNUNET_SYSERR;
+      continue;
+    }
+    if (GNUNET_SYSERR == ret)
+      break;
+  }
+  if (GNUNET_OK == ret)
+  {
+    struct TALER_MERCHANT_HttpResponse hr = {
+      .http_status = MHD_HTTP_OK
+    };
+
+    tgh->cb (tgh->cb_cls,
+             &hr,
+             ies_len,
+             ies);
+    tgh->cb = NULL; /* just to be sure */
+  }
+  return ret;
+}
+
+
+/**
+ * Function called when we're done processing the
+ * HTTP /templates request.
+ *
+ * @param cls the `struct TALER_MERCHANT_TemplatesGetHandle`
+ * @param response_code HTTP response code, 0 on error
+ * @param response response body, NULL if not in JSON
+ */
+static void
+handle_get_templates_finished (void *cls,
+                              long response_code,
+                              const void *response)
+{
+  struct TALER_MERCHANT_TemplatesGetHandle *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 /templates response with status code %u\n",
+              (unsigned int) response_code);
+  switch (response_code)
+  {
+  case MHD_HTTP_OK:
+    {
+      json_t *templates;
+      struct GNUNET_JSON_Specification spec[] = {
+        GNUNET_JSON_spec_json ("templates",
+                               &templates),
+        GNUNET_JSON_spec_end ()
+      };
+
+      if (GNUNET_OK !=
+          GNUNET_JSON_parse (json,
+                             spec,
+                             NULL, NULL))
+      {
+        hr.http_status = 0;
+        hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
+      }
+      else
+      {
+        if ( (! json_is_array (templates)) ||
+             (GNUNET_OK ==
+              parse_templates (templates,
+                              tgh)) )
+        {
+          GNUNET_JSON_parse_free (spec);
+          TALER_MERCHANT_templates_get_cancel (tgh);
+          return;
+        }
+        else
+        {
+          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;
+  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,
+           0,
+           NULL);
+  TALER_MERCHANT_templates_get_cancel (tgh);
+}
+
+
+struct TALER_MERCHANT_TemplatesGetHandle *
+TALER_MERCHANT_templates_get (
+  struct GNUNET_CURL_Context *ctx,
+  const char *backend_url,
+  TALER_MERCHANT_TemplatesGetCallback cb,
+  void *cb_cls)
+{
+  struct TALER_MERCHANT_TemplatesGetHandle *tgh;
+  CURL *eh;
+
+  tgh = GNUNET_new (struct TALER_MERCHANT_TemplatesGetHandle);
+  tgh->ctx = ctx;
+  tgh->cb = cb;
+  tgh->cb_cls = cb_cls;
+  tgh->url = TALER_url_join (backend_url,
+                             "private/templates",
+                             NULL);
+  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_templates_finished,
+                                  tgh);
+  return tgh;
+}
+
+
+void
+TALER_MERCHANT_templates_get_cancel (
+  struct TALER_MERCHANT_TemplatesGetHandle *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_post_templates.c 
b/src/lib/merchant_api_post_templates.c
new file mode 100644
index 00000000..7723d10c
--- /dev/null
+++ b/src/lib/merchant_api_post_templates.c
@@ -0,0 +1,233 @@
+/*
+  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_post_templates.c
+ * @brief Implementation of the POST /templates 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 POST /templates/$ID operation.
+ */
+struct TALER_MERCHANT_TemplatesPostHandle
+{
+
+  /**
+   * The url for this request.
+   */
+  char *url;
+
+  /**
+   * Handle for the request.
+   */
+  struct GNUNET_CURL_Job *job;
+
+  /**
+   * Function to call with the result.
+   */
+  TALER_MERCHANT_TemplatesPostCallback 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 POST /products request.
+ *
+ * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle`
+ * @param response_code HTTP response code, 0 on error
+ * @param response response body, NULL if not in JSON
+ */
+static void
+handle_post_templates_finished (void *cls,
+                               long response_code,
+                               const void *response)
+{
+  struct TALER_MERCHANT_TemplatesPostHandle *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,
+              "POST /templates 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);
+    /* 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);
+    /* Nothing really to verify, this should never
+       happen, we should pass the JSON reply to the
+       application */
+    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_templates_post_cancel (tph);
+}
+
+
+struct TALER_MERCHANT_TemplatesPostHandle *
+TALER_MERCHANT_templates_post (
+  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_TemplatesPostCallback cb,
+  void *cb_cls)
+{
+  struct TALER_MERCHANT_TemplatesPostHandle *tph;
+  json_t *req_obj;
+
+  req_obj = GNUNET_JSON_PACK (
+    GNUNET_JSON_pack_string ("template_id",
+                             template_id),
+    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_TemplatesPostHandle);
+  tph->ctx = ctx;
+  tph->cb = cb;
+  tph->cb_cls = cb_cls;
+  tph->url = TALER_url_join (backend_url,
+                             "private/templates",
+                             NULL);
+  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);
+    GNUNET_assert (GNUNET_OK ==
+                   TALER_curl_easy_post (&tph->post_ctx,
+                                         eh,
+                                         req_obj));
+    json_decref (req_obj);
+    tph->job = GNUNET_CURL_job_add2 (ctx,
+                                     eh,
+                                     tph->post_ctx.headers,
+                                     &handle_post_templates_finished,
+                                     tph);
+    GNUNET_assert (NULL != tph->job);
+  }
+  return tph;
+}
+
+
+void
+TALER_MERCHANT_templates_post_cancel (
+  struct TALER_MERCHANT_TemplatesPostHandle *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_post_templates.c */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]