gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] 02/02: webhook


From: gnunet
Subject: [taler-merchant] 02/02: webhook
Date: Fri, 02 Dec 2022 16:49:09 +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 09e5b8f57295e763e9e90812dd94c3bb44bf31f0
Author: priscilla <priscilla.huang@efrei.net>
AuthorDate: Fri Dec 2 10:48:29 2022 -0500

    webhook
---
 src/lib/merchant_api_delete_webhook.c              | 184 +++++++++++++++++
 src/lib/merchant_api_get_templates.c               |   4 +-
 src/lib/merchant_api_get_webhook.c                 | 219 +++++++++++++++++++++
 ...get_templates.c => merchant_api_get_webhooks.c} | 110 +++++------
 ...st_templates.c => merchant_api_patch_webhook.c} | 146 ++++++++------
 src/lib/merchant_api_post_templates.c              |   2 +-
 ...st_templates.c => merchant_api_post_webhooks.c} | 114 ++++++-----
 7 files changed, 604 insertions(+), 175 deletions(-)

diff --git a/src/lib/merchant_api_delete_webhook.c 
b/src/lib/merchant_api_delete_webhook.c
new file mode 100644
index 00000000..3825bc45
--- /dev/null
+++ b/src/lib/merchant_api_delete_webhook.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 /webhooks/$ID request of the merchant's 
HTTP API
+ * @author Priscilla HUANG
+ */
+#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 /webhooks/$ID operation.
+ */
+struct TALER_MERCHANT_WebhookDeleteHandle
+{
+  /**
+   * The url for this request.
+   */
+  char *url;
+
+  /**
+   * Handle for the request.
+   */
+  struct GNUNET_CURL_Job *job;
+
+  /**
+   * Function to call with the result.
+   */
+  TALER_MERCHANT_WebhookDeleteCallback 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 /webhooks/$ID request.
+ *
+ * @param cls the `struct TALER_MERCHANT_WebhookDeleteHandle`
+ * @param response_code HTTP response code, 0 on error
+ * @param response response body, NULL if not in JSON
+ */
+static void
+handle_delete_webhook_finished (void *cls,
+                                long response_code,
+                                const void *response)
+{
+  struct TALER_MERCHANT_WebhookDeleteHandle *wdh = 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 /webhooks/$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;
+  }
+  wdh->cb (wdh->cb_cls,
+           &hr);
+  TALER_MERCHANT_webhook_delete_cancel (tdh);
+}
+
+
+struct TALER_MERCHANT_WebhookDeleteHandle *
+TALER_MERCHANT_webhook_delete (
+  struct GNUNET_CURL_Context *ctx,
+  const char *backend_url,
+  const char *webhook_id,
+  TALER_MERCHANT_WebhookDeleteCallback cb,
+  void *cb_cls)
+{
+  struct TALER_MERCHANT_WebhookDeleteHandle *wdh;
+
+  wdh = GNUNET_new (struct TALER_MERCHANT_WebhookDeleteHandle);
+  wdh->ctx = ctx;
+  wdh->cb = cb;
+  wdh->cb_cls = cb_cls;
+  {
+    char *path;
+
+    GNUNET_asprintf (&path,
+                     "private/webhooks/%s",
+                     webhook_id);
+    wdh->url = TALER_url_join (backend_url,
+                               path,
+                               NULL);
+    GNUNET_free (path);
+  }
+  if (NULL == wdh->url)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not construct request URL.\n");
+    GNUNET_free (wdh);
+    return NULL;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Requesting URL '%s'\n",
+              wdh->url);
+  {
+    CURL *eh;
+
+    eh = TALER_MERCHANT_curl_easy_get_ (wdh->url);
+    GNUNET_assert (CURLE_OK ==
+                   curl_easy_setopt (eh,
+                                     CURLOPT_CUSTOMREQUEST,
+                                     MHD_HTTP_METHOD_DELETE));
+    wdh->job = GNUNET_CURL_job_add (ctx,
+                                    eh,
+                                    &handle_delete_template_finished,
+                                    wdh);
+  }
+  return wdh;
+}
+
+
+void
+TALER_MERCHANT_webhook_delete_cancel (
+  struct TALER_MERCHANT_WebhookDeleteHandle *wdh)
+{
+  if (NULL != wdh->job)
+    GNUNET_CURL_job_cancel (wdh->job);
+  GNUNET_free (wdh->url);
+  GNUNET_free (wdh);
+}
diff --git a/src/lib/merchant_api_get_templates.c 
b/src/lib/merchant_api_get_templates.c
index 5f74e7de..2dca928f 100644
--- a/src/lib/merchant_api_get_templates.c
+++ b/src/lib/merchant_api_get_templates.c
@@ -32,7 +32,7 @@
 
 
 /**
- * Handle for a GET /products operation.
+ * Handle for a GET /templates operation.
  */
 struct TALER_MERCHANT_TemplatesGetHandle
 {
@@ -68,7 +68,7 @@ struct TALER_MERCHANT_TemplatesGetHandle
  * Parse template information from @a ia.
  *
  * @param ia JSON array (or NULL!) with template data
- * @param pgh operation handle
+ * @param wgh operation handle
  * @return #GNUNET_OK on success
  */
 static int
diff --git a/src/lib/merchant_api_get_webhook.c 
b/src/lib/merchant_api_get_webhook.c
new file mode 100644
index 00000000..3d8dfc57
--- /dev/null
+++ b/src/lib/merchant_api_get_webhook.c
@@ -0,0 +1,219 @@
+/*
+  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_webhook.c
+ * @brief Implementation of the GET /webhooks/$ID request of the merchant's 
HTTP API
+ * @author Priscilla HUANG
+ */
+#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 /webhooks/$ID operation.
+ */
+struct TALER_MERCHANT_WebhookGetHandle
+{
+  /**
+   * The url for this request.
+   */
+  char *url;
+
+  /**
+   * Handle for the request.
+   */
+  struct GNUNET_CURL_Job *job;
+
+  /**
+   * Function to call with the result.
+   */
+  TALER_MERCHANT_WebhookGetCallback 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 /webhooks/$ID request.
+ *
+ * @param cls the `struct TALER_MERCHANT_WebhookGetHandle`
+ * @param response_code HTTP response code, 0 on error
+ * @param response response body, NULL if not in JSON
+ */
+static void
+handle_get_webhook_finished (void *cls,
+                             long response_code,
+                             const void *response)
+{
+  struct TALER_MERCHANT_WebhookGetHandle *wgh = cls;
+  const json_t *json = response;
+  struct TALER_MERCHANT_HttpResponse hr = {
+    .http_status = (unsigned int) response_code,
+    .reply = json
+  };
+
+  wgh->job = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Got /webhooks/$ID response with status code %u\n",
+              (unsigned int) response_code);
+  switch (response_code)
+  {
+  case MHD_HTTP_OK:
+    {
+      const char *event_type;
+      const char *url;
+      const char *http_method;
+      const char *header_template;
+      const char *body_template;
+      bool rst_ok = true;
+      struct GNUNET_JSON_Specification spec[] = {
+        GNUNET_JSON_spec_string ("event_type",
+                                 &event_type),
+        GNUNET_JSON_spec_string ("url",
+                                 &url),
+        GNUNET_JSON_spec_string ("http_method",
+                                 &http_method),
+        GNUNET_JSON_spec_string ("header_template",
+                                 &header_template),
+        GNUNET_JSON_spec_string ("body_template",
+                                 &body_template),
+        GNUNET_JSON_spec_end ()
+      };
+
+
+      if ( (rst_ok) &&
+           (GNUNET_OK ==
+            GNUNET_JSON_parse (json,
+                               spec,
+                               NULL, NULL)) )
+      {
+        wgh->cb (wgh->cb_cls,
+                 &hr,
+                 event_type,
+                 url,
+                 http_method,
+                 header_template,
+                 body_template);
+        GNUNET_JSON_parse_free (spec);
+        TALER_MERCHANT_webhook_get_cancel (wgh);
+        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;
+  }
+  wgh->cb (wgh->cb_cls,
+           &hr,
+           NULL,
+           NULL,
+           NULL);
+  TALER_MERCHANT_webhook_get_cancel (wgh);
+}
+
+
+struct TALER_MERCHANT_WebhookGetHandle *
+TALER_MERCHANT_webhook_get (
+  struct GNUNET_CURL_Context *ctx,
+  const char *backend_url,
+  const char *webhook_id,
+  TALER_MERCHANT_WebhookGetCallback cb,
+  void *cb_cls)
+{
+  struct TALER_MERCHANT_WebhookGetHandle *wgh;
+  CURL *eh;
+
+  wgh = GNUNET_new (struct TALER_MERCHANT_WebhookGetHandle);
+  wgh->ctx = ctx;
+  wgh->cb = cb;
+  wgh->cb_cls = cb_cls;
+  {
+    char *path;
+
+    GNUNET_asprintf (&path,
+                     "private/webhooks/%s",
+                     template_id);
+    wgh->url = TALER_url_join (backend_url,
+                               path,
+                               NULL);
+    GNUNET_free (path);
+  }
+  if (NULL == wgh->url)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not construct request URL.\n");
+    GNUNET_free (wgh);
+    return NULL;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Requesting URL '%s'\n",
+              wgh->url);
+  eh = TALER_MERCHANT_curl_easy_get_ (wgh->url);
+  wgh->job = GNUNET_CURL_job_add (ctx,
+                                  eh,
+                                  &handle_get_webhook_finished,
+                                  wgh);
+  return wgh;
+}
+
+
+void
+TALER_MERCHANT_webhook_get_cancel (
+  struct TALER_MERCHANT_WebhookGetHandle *wgh)
+{
+  if (NULL != wgh->job)
+    GNUNET_CURL_job_cancel (wgh->job);
+  GNUNET_free (wgh->url);
+  GNUNET_free (wgh);
+}
diff --git a/src/lib/merchant_api_get_templates.c 
b/src/lib/merchant_api_get_webhooks.c
similarity index 66%
copy from src/lib/merchant_api_get_templates.c
copy to src/lib/merchant_api_get_webhooks.c
index 5f74e7de..c80a94b9 100644
--- a/src/lib/merchant_api_get_templates.c
+++ b/src/lib/merchant_api_get_webhooks.c
@@ -15,8 +15,8 @@
   <http://www.gnu.org/licenses/>
 */
 /**
- * @file merchant_api_get_templates.c
- * @brief Implementation of the GET /templates request of the merchant's HTTP 
API
+ * @file merchant_api_get_webhooks.c
+ * @brief Implementation of the GET /webhooks request of the merchant's HTTP 
API
  * @author Priscilla HUANG
  */
 #include "platform.h"
@@ -32,9 +32,9 @@
 
 
 /**
- * Handle for a GET /products operation.
+ * Handle for a GET /webhooks operation.
  */
-struct TALER_MERCHANT_TemplatesGetHandle
+struct TALER_MERCHANT_WebhooksGetHandle
 {
   /**
    * The url for this request.
@@ -49,7 +49,7 @@ struct TALER_MERCHANT_TemplatesGetHandle
   /**
    * Function to call with the result.
    */
-  TALER_MERCHANT_TemplatesGetCallback cb;
+  TALER_MERCHANT_WebhooksGetCallback cb;
 
   /**
    * Closure for @a cb.
@@ -65,28 +65,28 @@ struct TALER_MERCHANT_TemplatesGetHandle
 
 
 /**
- * Parse template information from @a ia.
+ * Parse webhook information from @a ia.
  *
- * @param ia JSON array (or NULL!) with template data
- * @param pgh operation handle
+ * @param ia JSON array (or NULL!) with webhook data
+ * @param wgh operation handle
  * @return #GNUNET_OK on success
  */
 static int
-parse_templates (const json_t *ia,
-                struct TALER_MERCHANT_TemplatesGetHandle *tgh)
+parse_webhooks (const json_t *ia,
+                struct TALER_MERCHANT_webhooksGetHandle *wgh)
 {
   unsigned int ies_len = json_array_size (ia);
-  struct TALER_MERCHANT_TemplateEntry ies[ies_len];
+  struct TALER_MERCHANT_WebhookEntry 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 TALER_MERCHANT_WebhookEntry *ie = &ies[index];
     struct GNUNET_JSON_Specification spec[] = {
-      GNUNET_JSON_spec_string ("template_id",
-                               &ie->template_id),
+      GNUNET_JSON_spec_string ("webhook_id",
+                               &ie->webhook_id),
       GNUNET_JSON_spec_end ()
     };
 
@@ -108,11 +108,11 @@ parse_templates (const json_t *ia,
       .http_status = MHD_HTTP_OK
     };
 
-    tgh->cb (tgh->cb_cls,
+    wgh->cb (tgh->cb_cls,
              &hr,
              ies_len,
              ies);
-    tgh->cb = NULL; /* just to be sure */
+    wgh->cb = NULL; /* just to be sure */
   }
   return ret;
 }
@@ -120,36 +120,36 @@ parse_templates (const json_t *ia,
 
 /**
  * Function called when we're done processing the
- * HTTP /templates request.
+ * HTTP /webhooks request.
  *
- * @param cls the `struct TALER_MERCHANT_TemplatesGetHandle`
+ * @param cls the `struct TALER_MERCHANT_WebhooksGetHandle`
  * @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,
+handle_get_webhooks_finished (void *cls,
                               long response_code,
                               const void *response)
 {
-  struct TALER_MERCHANT_TemplatesGetHandle *tgh = cls;
+  struct TALER_MERCHANT_WebhooksGetHandle *tgh = cls;
   const json_t *json = response;
   struct TALER_MERCHANT_HttpResponse hr = {
     .http_status = (unsigned int) response_code,
     .reply = json
   };
 
-  tgh->job = NULL;
+  wgh->job = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got /templates response with status code %u\n",
+              "Got /webhooks response with status code %u\n",
               (unsigned int) response_code);
   switch (response_code)
   {
   case MHD_HTTP_OK:
     {
-      json_t *templates;
+      json_t *webhooks;
       struct GNUNET_JSON_Specification spec[] = {
-        GNUNET_JSON_spec_json ("templates",
-                               &templates),
+        GNUNET_JSON_spec_json ("webhooks",
+                               &webhooks),
         GNUNET_JSON_spec_end ()
       };
 
@@ -163,13 +163,13 @@ handle_get_templates_finished (void *cls,
       }
       else
       {
-        if ( (! json_is_array (templates)) ||
+        if ( (! json_is_array (webhooks)) ||
              (GNUNET_OK ==
-              parse_templates (templates,
-                              tgh)) )
+              parse_templates (webhooks,
+                              wgh)) )
         {
           GNUNET_JSON_parse_free (spec);
-          TALER_MERCHANT_templates_get_cancel (tgh);
+          TALER_MERCHANT_webhooks_get_cancel (wgh);
           return;
         }
         else
@@ -196,56 +196,56 @@ handle_get_templates_finished (void *cls,
                 (int) hr.ec);
     break;
   }
-  tgh->cb (tgh->cb_cls,
+  tgh->cb (wgh->cb_cls,
            &hr,
            0,
            NULL);
-  TALER_MERCHANT_templates_get_cancel (tgh);
+  TALER_MERCHANT_webhooks_get_cancel (wgh);
 }
 
 
-struct TALER_MERCHANT_TemplatesGetHandle *
-TALER_MERCHANT_templates_get (
+struct TALER_MERCHANT_WebhooksGetHandle *
+TALER_MERCHANT_webhooks_get (
   struct GNUNET_CURL_Context *ctx,
   const char *backend_url,
-  TALER_MERCHANT_TemplatesGetCallback cb,
+  TALER_MERCHANT_WebhooksGetCallback cb,
   void *cb_cls)
 {
-  struct TALER_MERCHANT_TemplatesGetHandle *tgh;
+  struct TALER_MERCHANT_WebhooksGetHandle *wgh;
   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",
+  wgh = GNUNET_new (struct TALER_MERCHANT_WebhooksGetHandle);
+  wgh->ctx = ctx;
+  wgh->cb = cb;
+  wgh->cb_cls = cb_cls;
+  wgh->url = TALER_url_join (backend_url,
+                             "private/webhooks",
                              NULL);
-  if (NULL == tgh->url)
+  if (NULL == wgh->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Could not construct request URL.\n");
-    GNUNET_free (tgh);
+    GNUNET_free (wgh);
     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,
+              wgh->url);
+  eh = TALER_MERCHANT_curl_easy_get_ (wgh->url);
+  wgh->job = GNUNET_CURL_job_add (ctx,
                                   eh,
-                                  &handle_get_templates_finished,
-                                  tgh);
-  return tgh;
+                                  &handle_get_webhooks_finished,
+                                  wgh);
+  return wgh;
 }
 
 
 void
-TALER_MERCHANT_templates_get_cancel (
-  struct TALER_MERCHANT_TemplatesGetHandle *tgh)
+TALER_MERCHANT_webhooks_get_cancel (
+  struct TALER_MERCHANT_WebhooksGetHandle *wgh)
 {
-  if (NULL != tgh->job)
-    GNUNET_CURL_job_cancel (tgh->job);
-  GNUNET_free (tgh->url);
-  GNUNET_free (tgh);
+  if (NULL != wgh->job)
+    GNUNET_CURL_job_cancel (wgh->job);
+  GNUNET_free (wgh->url);
+  GNUNET_free (wgh);
 }
diff --git a/src/lib/merchant_api_post_templates.c 
b/src/lib/merchant_api_patch_webhook.c
similarity index 59%
copy from src/lib/merchant_api_post_templates.c
copy to src/lib/merchant_api_patch_webhook.c
index f34d040d..279a536f 100644
--- a/src/lib/merchant_api_post_templates.c
+++ b/src/lib/merchant_api_patch_webhook.c
@@ -1,6 +1,6 @@
 /*
   This file is part of TALER
-  Copyright (C) 2020-2021 Taler Systems SA
+  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
@@ -17,10 +17,10 @@
   If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file merchant_api_post_templates.c
- * @brief Implementation of the POST /templates request
+ * @file merchant_api_patch_webhook.c
+ * @brief Implementation of the PATCH /webhooks/$ID request
  *        of the merchant's HTTP API
- * @author Priscilla HUANG
+ * @author Priscilla HUANG 
  */
 #include "platform.h"
 #include <curl/curl.h>
@@ -34,9 +34,9 @@
 
 
 /**
- * Handle for a POST /templates/$ID operation.
+ * Handle for a PATCH /webhooks/$ID operation.
  */
-struct TALER_MERCHANT_TemplatesPostHandle
+struct TALER_MERCHANT_WebhookPatchHandle
 {
 
   /**
@@ -52,7 +52,7 @@ struct TALER_MERCHANT_TemplatesPostHandle
   /**
    * Function to call with the result.
    */
-  TALER_MERCHANT_TemplatesPostCallback cb;
+  TALER_MERCHANT_WebhookPatchCallback cb;
 
   /**
    * Closure for @a cb.
@@ -74,27 +74,27 @@ struct TALER_MERCHANT_TemplatesPostHandle
 
 /**
  * Function called when we're done processing the
- * HTTP POST /products request.
+ * HTTP PATCH /webhooks/$ID request.
  *
- * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle`
+ * @param cls the `struct TALER_MERCHANT_WebhookPatchHandle`
  * @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,
+handle_patch_webhook_finished (void *cls,
                                long response_code,
                                const void *response)
 {
-  struct TALER_MERCHANT_TemplatesPostHandle *tph = cls;
+  struct TALER_MERCHANT_WebhookPatchHandle *wph = cls;
   const json_t *json = response;
   struct TALER_MERCHANT_HttpResponse hr = {
     .http_status = (unsigned int) response_code,
     .reply = json
   };
 
-  tph->job = NULL;
+  wph->job = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "POST /templates completed with response code %u\n",
+              "PATCH /webhooks/$ID completed with response code %u\n",
               (unsigned int) response_code);
   switch (response_code)
   {
@@ -106,6 +106,7 @@ handle_post_templates_finished (void *cls,
   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 */
@@ -125,9 +126,6 @@ handle_post_templates_finished (void *cls,
   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);
@@ -151,83 +149,105 @@ handle_post_templates_finished (void *cls,
     GNUNET_break_op (0);
     break;
   }
-  tph->cb (tph->cb_cls,
+  wph->cb (wph->cb_cls,
            &hr);
-  TALER_MERCHANT_templates_post_cancel (tph);
+  TALER_MERCHANT_webhook_patch_cancel (wph);
 }
 
 
-struct TALER_MERCHANT_TemplatesPostHandle *
-TALER_MERCHANT_templates_post (
+struct TALER_MERCHANT_WebhookPatchHandle *
+TALER_MERCHANT_webhook_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_TemplatesPostCallback cb,
+  const char *webhook_id,
+  const char *event_type,
+  const char *url,
+  const char *http_method,
+  const char *header_template,
+  const char *body_template,
+  TALER_MERCHANT_WebhookPatchCallback cb,
   void *cb_cls)
 {
-  struct TALER_MERCHANT_TemplatesPostHandle *tph;
+  struct TALER_MERCHANT_WebhookPatchHandle *wph;
   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_JSON_pack_string ("event_type",
+                             event_type),
+    GNUNET_JSON_pack_string ("url",
+                             url),
+    GNUNET_JSON_pack_string ("http_method",
+                             http_method),
+    GNUNET_JSON_pack_string ("header_template",
+                             header_template),
+    GNUNET_JSON_pack_string ("body_template",
+                             body_template));
+  wph = GNUNET_new (struct TALER_MERCHANT_WebhookPatchHandle);
+  wph->ctx = ctx;
+  wph->cb = cb;
+  wph->cb_cls = cb_cls;
+  {
+    char *path;
+
+    GNUNET_asprintf (&path,
+                     "private/webhooks/%s",
+                     webhook_id);
+    wph->url = TALER_url_join (backend_url,
+                               path,
+                               NULL);
+    GNUNET_free (path);
+  }
+  if (NULL == wph->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Could not construct request URL.\n");
     json_decref (req_obj);
-    GNUNET_free (tph);
+    GNUNET_free (wph);
     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));
+    eh = TALER_MERCHANT_curl_easy_get_ (wph->url);
+    if (GNUNET_OK !=
+        TALER_curl_easy_post (&wph->post_ctx,
+                              eh,
+                              req_obj))
+    {
+      GNUNET_break (0);
+      curl_easy_cleanup (eh);
+      json_decref (req_obj);
+      GNUNET_free (wph);
+      return NULL;
+    }
     json_decref (req_obj);
-    tph->job = GNUNET_CURL_job_add2 (ctx,
+    GNUNET_assert (CURLE_OK ==
+                   curl_easy_setopt (eh,
+                                     CURLOPT_CUSTOMREQUEST,
+                                     MHD_HTTP_METHOD_PATCH));
+    wph->job = GNUNET_CURL_job_add2 (ctx,
                                      eh,
-                                     tph->post_ctx.headers,
-                                     &handle_post_templates_finished,
-                                     tph);
-    GNUNET_assert (NULL != tph->job);
+                                     wph->post_ctx.headers,
+                                     &handle_patch_webhook_finished,
+                                     wph);
   }
-  return tph;
+  return wph;
 }
 
 
 void
-TALER_MERCHANT_templates_post_cancel (
-  struct TALER_MERCHANT_TemplatesPostHandle *tph)
+TALER_MERCHANT_webhook_patch_cancel (
+  struct TALER_MERCHANT_WebhookPatchHandle *wph)
 {
-  if (NULL != tph->job)
+  if (NULL != wph->job)
   {
-    GNUNET_CURL_job_cancel (tph->job);
-    tph->job = NULL;
+    GNUNET_CURL_job_cancel (wph->job);
+    wph->job = NULL;
   }
-  TALER_curl_easy_post_finished (&tph->post_ctx);
-  GNUNET_free (tph->url);
-  GNUNET_free (tph);
+  TALER_curl_easy_post_finished (&wph->post_ctx);
+  GNUNET_free (wph->url);
+  GNUNET_free (wph);
 }
 
 
-/* end of merchant_api_post_templates.c */
+/* end of merchant_api_patch_webhook.c */
diff --git a/src/lib/merchant_api_post_templates.c 
b/src/lib/merchant_api_post_templates.c
index f34d040d..b4d286aa 100644
--- a/src/lib/merchant_api_post_templates.c
+++ b/src/lib/merchant_api_post_templates.c
@@ -74,7 +74,7 @@ struct TALER_MERCHANT_TemplatesPostHandle
 
 /**
  * Function called when we're done processing the
- * HTTP POST /products request.
+ * HTTP POST /templates request.
  *
  * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle`
  * @param response_code HTTP response code, 0 on error
diff --git a/src/lib/merchant_api_post_templates.c 
b/src/lib/merchant_api_post_webhooks.c
similarity index 66%
copy from src/lib/merchant_api_post_templates.c
copy to src/lib/merchant_api_post_webhooks.c
index f34d040d..9d3b87e8 100644
--- a/src/lib/merchant_api_post_templates.c
+++ b/src/lib/merchant_api_post_webhooks.c
@@ -17,8 +17,8 @@
   If not, see <http://www.gnu.org/licenses/>
 */
 /**
- * @file merchant_api_post_templates.c
- * @brief Implementation of the POST /templates request
+ * @file merchant_api_post_webhooks.c
+ * @brief Implementation of the POST /webhooks request
  *        of the merchant's HTTP API
  * @author Priscilla HUANG
  */
@@ -34,9 +34,9 @@
 
 
 /**
- * Handle for a POST /templates/$ID operation.
+ * Handle for a POST /webhooks/$ID operation.
  */
-struct TALER_MERCHANT_TemplatesPostHandle
+struct TALER_MERCHANT_WebhooksPostHandle
 {
 
   /**
@@ -52,7 +52,7 @@ struct TALER_MERCHANT_TemplatesPostHandle
   /**
    * Function to call with the result.
    */
-  TALER_MERCHANT_TemplatesPostCallback cb;
+  TALER_MERCHANT_WebhooksPostCallback cb;
 
   /**
    * Closure for @a cb.
@@ -74,27 +74,27 @@ struct TALER_MERCHANT_TemplatesPostHandle
 
 /**
  * Function called when we're done processing the
- * HTTP POST /products request.
+ * HTTP POST /webhooks request.
  *
- * @param cls the `struct TALER_MERCHANT_TemplatesPostHandle`
+ * @param cls the `struct TALER_MERCHANT_WebhooksPostHandle`
  * @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,
+handle_post_webhooks_finished (void *cls,
                                long response_code,
                                const void *response)
 {
-  struct TALER_MERCHANT_TemplatesPostHandle *tph = cls;
+  struct TALER_MERCHANT_WebhooksPostHandle *wph = cls;
   const json_t *json = response;
   struct TALER_MERCHANT_HttpResponse hr = {
     .http_status = (unsigned int) response_code,
     .reply = json
   };
 
-  tph->job = NULL;
+  wph->job = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "POST /templates completed with response code %u\n",
+              "POST /webhooks completed with response code %u\n",
               (unsigned int) response_code);
   switch (response_code)
   {
@@ -151,83 +151,89 @@ handle_post_templates_finished (void *cls,
     GNUNET_break_op (0);
     break;
   }
-  tph->cb (tph->cb_cls,
+  wph->cb (wph->cb_cls,
            &hr);
-  TALER_MERCHANT_templates_post_cancel (tph);
+  TALER_MERCHANT_webhooks_post_cancel (wph);
 }
 
 
-struct TALER_MERCHANT_TemplatesPostHandle *
-TALER_MERCHANT_templates_post (
+struct TALER_MERCHANT_WebhooksPostHandle *
+TALER_MERCHANT_webhooks_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,
+  const char *webhook_id,
+  const char *event_type,
+  const char *url,
+  const char *http_method,
+  const char *header_template,
+  const char *body_template,
+  TALER_MERCHANT_WebhooksPostCallback cb,
   void *cb_cls)
 {
-  struct TALER_MERCHANT_TemplatesPostHandle *tph;
+  struct TALER_MERCHANT_WebhooksPostHandle *wph;
   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",
+    GNUNET_JSON_pack_string ("webhook_id",
+                             webhook_id),
+    GNUNET_JSON_pack_string ("event_type",
+                             event_type),
+    GNUNET_JSON_pack_string ("url",
+                             url),
+    GNUNET_JSON_pack_string ("http_method",
+                             http_method),
+    GNUNET_JSON_pack_string ("header_template",
+                             header_template),
+    GNUNET_JSON_pack_string ("body_template",
+                             body_template));
+  tph = GNUNET_new (struct TALER_MERCHANT_WebhooksPostHandle);
+  wph->ctx = ctx;
+  wph->cb = cb;
+  wph->cb_cls = cb_cls;
+  wph->url = TALER_url_join (backend_url,
+                             "private/webhooks",
                              NULL);
-  if (NULL == tph->url)
+  if (NULL == wph->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Could not construct request URL.\n");
     json_decref (req_obj);
-    GNUNET_free (tph);
+    GNUNET_free (wph);
     return NULL;
   }
   {
     CURL *eh;
 
-    eh = TALER_MERCHANT_curl_easy_get_ (tph->url);
+    eh = TALER_MERCHANT_curl_easy_get_ (wph->url);
     GNUNET_assert (GNUNET_OK ==
-                   TALER_curl_easy_post (&tph->post_ctx,
+                   TALER_curl_easy_post (&wph->post_ctx,
                                          eh,
                                          req_obj));
     json_decref (req_obj);
-    tph->job = GNUNET_CURL_job_add2 (ctx,
+    wph->job = GNUNET_CURL_job_add2 (ctx,
                                      eh,
-                                     tph->post_ctx.headers,
-                                     &handle_post_templates_finished,
-                                     tph);
-    GNUNET_assert (NULL != tph->job);
+                                     wph->post_ctx.headers,
+                                     &handle_post_webhooks_finished,
+                                     wph);
+    GNUNET_assert (NULL != wph->job);
   }
-  return tph;
+  return wph;
 }
 
 
 void
-TALER_MERCHANT_templates_post_cancel (
-  struct TALER_MERCHANT_TemplatesPostHandle *tph)
+TALER_MERCHANT_webhooks_post_cancel (
+  struct TALER_MERCHANT_WebhooksPostHandle *wph)
 {
-  if (NULL != tph->job)
+  if (NULL != wph->job)
   {
-    GNUNET_CURL_job_cancel (tph->job);
-    tph->job = NULL;
+    GNUNET_CURL_job_cancel (wph->job);
+    wph->job = NULL;
   }
-  TALER_curl_easy_post_finished (&tph->post_ctx);
-  GNUNET_free (tph->url);
-  GNUNET_free (tph);
+  TALER_curl_easy_post_finished (&wph->post_ctx);
+  GNUNET_free (wph->url);
+  GNUNET_free (wph);
 }
 
 
-/* end of merchant_api_post_templates.c */
+/* end of merchant_api_post_webhooks.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]