gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] 216/277: clean up logic: use new EXCHANGE_YNA enum, av


From: gnunet
Subject: [taler-merchant] 216/277: clean up logic: use new EXCHANGE_YNA enum, avoid crazy macro construction to build URLs, use full power of TALER_url_join() instead
Date: Sun, 05 Jul 2020 20:52:09 +0200

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

grothoff pushed a commit to branch master
in repository merchant.

commit 458d1ac531f52e7802e054bfd151631af4d9d6e0
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Jun 14 15:17:11 2020 +0200

    clean up logic: use new EXCHANGE_YNA enum, avoid crazy macro construction 
to build URLs, use full power of TALER_url_join() instead
---
 src/include/taler_merchant_service.h |  41 ++------
 src/lib/merchant_api_get_orders.c    | 178 ++++++++++++-----------------------
 src/lib/merchant_api_get_reserves.c  |  17 ++--
 src/lib/merchant_api_get_tips.c      | 103 +++++---------------
 src/lib/merchant_api_get_transfers.c |  11 +--
 5 files changed, 103 insertions(+), 247 deletions(-)

diff --git a/src/include/taler_merchant_service.h 
b/src/include/taler_merchant_service.h
index 6a401ae..4df40af 100644
--- a/src/include/taler_merchant_service.h
+++ b/src/include/taler_merchant_service.h
@@ -1251,28 +1251,6 @@ TALER_MERCHANT_orders_get (
   void *cb_cls);
 
 
-/**
- * Possible values for a binary filter.
- */
-enum TALER_MERCHANT_YesNoAll
-{
-  /**
-   * If condition is yes.
-   */
-  TALER_MERCHANT_YNA_YES = 1,
-
-  /**
-  * If condition is no.
-  */
-  TALER_MERCHANT_YNA_NO = 2,
-
-  /**
-   * Condition disabled.
-   */
-  TALER_MERCHANT_YNA_ALL = 3
-};
-
-
 /**
  * Make a GET /orders request with filters.
  *
@@ -1295,9 +1273,9 @@ struct TALER_MERCHANT_OrdersGetHandle *
 TALER_MERCHANT_orders_get2 (
   struct GNUNET_CURL_Context *ctx,
   const char *backend_url,
-  enum TALER_MERCHANT_YesNoAll paid,
-  enum TALER_MERCHANT_YesNoAll refunded,
-  enum TALER_MERCHANT_YesNoAll wired,
+  enum TALER_EXCHANGE_YesNoAll paid,
+  enum TALER_EXCHANGE_YesNoAll refunded,
+  enum TALER_EXCHANGE_YesNoAll wired,
   struct GNUNET_TIME_Absolute date,
   uint64_t start_row,
   int64_t delta,
@@ -1350,11 +1328,6 @@ struct TALER_MERCHANT_RefundDetail
    */
   struct TALER_Amount refund_amount;
 
-  /**
-   * Applicable refund transaction fee.
-   */
-  struct TALER_Amount refund_fee;
-
   /**
    * Public key of the exchange affirming the refund,
    * only valid if the @e hr http_status is #MHD_HTTP_OK.
@@ -2402,7 +2375,7 @@ TALER_MERCHANT_transfers_get (
   const struct GNUNET_TIME_Absolute after,
   int64_t limit,
   uint64_t offset,
-  enum TALER_MERCHANT_YesNoAll verified,
+  enum TALER_EXCHANGE_YesNoAll verified,
   TALER_MERCHANT_GetTransfersCallback cb,
   void *cb_cls);
 
@@ -2566,8 +2539,8 @@ struct TALER_MERCHANT_ReservesGetHandle *
 TALER_MERCHANT_reserves_get (struct GNUNET_CURL_Context *ctx,
                              const char *backend_url,
                              struct GNUNET_TIME_Absolute after,
-                             enum TALER_MERCHANT_YesNoAll active,
-                             enum TALER_MERCHANT_YesNoAll failures,
+                             enum TALER_EXCHANGE_YesNoAll active,
+                             enum TALER_EXCHANGE_YesNoAll failures,
                              TALER_MERCHANT_ReservesGetCallback cb,
                              void *cb_cls);
 
@@ -2945,7 +2918,7 @@ TALER_MERCHANT_tips_get (struct GNUNET_CURL_Context *ctx,
 struct TALER_MERCHANT_TipsGetHandle *
 TALER_MERCHANT_tips_get2 (struct GNUNET_CURL_Context *ctx,
                           const char *backend_url,
-                          enum TALER_MERCHANT_YesNoAll expired,
+                          enum TALER_EXCHANGE_YesNoAll expired,
                           int64_t limit,
                           uint64_t offset,
                           TALER_MERCHANT_TipsGetCallback cb,
diff --git a/src/lib/merchant_api_get_orders.c 
b/src/lib/merchant_api_get_orders.c
index 1b65b99..9d1ac3e 100644
--- a/src/lib/merchant_api_get_orders.c
+++ b/src/lib/merchant_api_get_orders.c
@@ -216,9 +216,9 @@ TALER_MERCHANT_orders_get (
 {
   return TALER_MERCHANT_orders_get2 (ctx,
                                      backend_url,
-                                     TALER_MERCHANT_YNA_ALL,
-                                     TALER_MERCHANT_YNA_ALL,
-                                     TALER_MERCHANT_YNA_ALL,
+                                     TALER_EXCHANGE_YNA_ALL,
+                                     TALER_EXCHANGE_YNA_ALL,
+                                     TALER_EXCHANGE_YNA_ALL,
                                      GNUNET_TIME_UNIT_FOREVER_ABS,
                                      UINT64_MAX,
                                      -20, /* default is most recent 20 entries 
*/
@@ -250,9 +250,9 @@ struct TALER_MERCHANT_OrdersGetHandle *
 TALER_MERCHANT_orders_get2 (
   struct GNUNET_CURL_Context *ctx,
   const char *backend_url,
-  enum TALER_MERCHANT_YesNoAll paid,
-  enum TALER_MERCHANT_YesNoAll refunded,
-  enum TALER_MERCHANT_YesNoAll wired,
+  enum TALER_EXCHANGE_YesNoAll paid,
+  enum TALER_EXCHANGE_YesNoAll refunded,
+  enum TALER_EXCHANGE_YesNoAll wired,
   struct GNUNET_TIME_Absolute date,
   uint64_t start_row,
   int64_t delta,
@@ -278,123 +278,69 @@ TALER_MERCHANT_orders_get2 (
 
   /* build ogh->url with the various optional arguments */
   {
-    struct GNUNET_Buffer buf = { 0 };
-    bool first = true;
-    /**
-     * Macro to append @a a and @a b to @a buf, using
-     * the right separators between key (@a a) and
-     * value (@a b). Uses "first" to decide between
-     * using "?" and "&" as the separator.
-     *
-     * @param a a key
-     * @param b a value
-     */
-#define APPEND(a,b)                             \
-  do { \
-    if (first) \
-      GNUNET_buffer_write_str (&buf, \
-                               "?");            \
-    else \
-      GNUNET_buffer_write_str (&buf, \
-                               "&"); \
-    first = false; \
-    GNUNET_buffer_write_str (&buf, (a)); \
-    GNUNET_buffer_write_str (&buf, "="); \
-    GNUNET_buffer_write_str (&buf, (b)); \
-  } while (0)
-
-    {
-      char *url;
-
-      url = TALER_url_join (backend_url,
-                            "private/orders",
-                            NULL);
-      if (NULL == url)
-        goto finished;
-      GNUNET_buffer_write_str (&buf,
-                               url);
-      GNUNET_free (url);
-    }
-    if (TALER_MERCHANT_YNA_ALL != paid)
-      APPEND ("paid",
-              (TALER_MERCHANT_YNA_YES == paid) ? "yes" : "no");
-    if (TALER_MERCHANT_YNA_ALL != refunded)
-      APPEND ("refunded",
-              (TALER_MERCHANT_YNA_YES == refunded) ? "yes" : "no");
-    if (TALER_MERCHANT_YNA_ALL != wired)
-      APPEND ("wired",
-              (TALER_MERCHANT_YNA_YES == wired) ? "yes" : "no");
+    const char *dstr;
+    bool have_date;
+    bool have_srow;
+    char cbuf[30];
+    char dbuf[30];
+    char tbuf[30];
+
+    GNUNET_snprintf (tbuf,
+                     sizeof (tbuf),
+                     "%llu",
+                     (unsigned long long) timeout_ms);
+    GNUNET_snprintf (dbuf,
+                     sizeof (dbuf),
+                     "%lld",
+                     (long long) delta);
+    GNUNET_snprintf (cbuf,
+                     sizeof (cbuf),
+                     "%llu",
+                     (unsigned long long) start_row);
+    dstr = GNUNET_STRINGS_absolute_time_to_string (date);
     if (delta > 0)
     {
-      if (0 != date.abs_value_us)
-      {
-        const char *str;
-
-        str = GNUNET_STRINGS_absolute_time_to_string (date);
-        APPEND ("date",
-                str);
-      }
-      if (0 != start_row)
-      {
-        char cbuf[30];
-
-        GNUNET_snprintf (cbuf,
-                         sizeof (cbuf),
-                         "%llu",
-                         (unsigned long long) start_row);
-        APPEND ("start",
-                cbuf);
-      }
+      have_date = (0 != date.abs_value_us);
+      have_srow = (0 != start_row);
     }
     else
     {
-      if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != date.abs_value_us)
-      {
-        const char *str;
-
-        str = GNUNET_STRINGS_absolute_time_to_string (date);
-        APPEND ("date",
-                str);
-      }
-      if (UINT64_MAX != start_row)
-      {
-        char cbuf[30];
-
-        GNUNET_snprintf (cbuf,
-                         sizeof (cbuf),
-                         "%llu",
-                         (unsigned long long) start_row);
-        APPEND ("start",
-                cbuf);
-      }
+      have_date = (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
+                   date.abs_value_us);
+      have_srow = (UINT64_MAX != start_row);
     }
-    if (-20 != delta)
-    {
-      char cbuf[30];
-
-      GNUNET_snprintf (cbuf,
-                       sizeof (cbuf),
-                       "%lld",
-                       (long long) delta);
-      APPEND ("delta",
-              cbuf);
-    }
-    if (0 != timeout_ms)
-    {
-      char cbuf[30];
-
-      GNUNET_snprintf (cbuf,
-                       sizeof (cbuf),
-                       "%llu",
-                       (unsigned long long) timeout_ms);
-      APPEND ("timeout_ms",
-              cbuf);
-    }
-    ogh->url = GNUNET_buffer_reap_str (&buf);
-#undef APPEND
+    ogh->url = TALER_url_join (backend_url,
+                               "private/orders",
+                               "paid",
+                               (TALER_EXCHANGE_YNA_ALL != paid)
+                               ? TALER_yna_to_string (paid)
+                               : NULL,
+                               "refunded",
+                               (TALER_EXCHANGE_YNA_ALL != refunded)
+                               ? TALER_yna_to_string (refunded)
+                               : NULL,
+                               "wired",
+                               (TALER_EXCHANGE_YNA_ALL != wired)
+                               ? TALER_yna_to_string (wired)
+                               : NULL,
+                               "date",
+                               (have_date)
+                               ? dstr
+                               : NULL,
+                               "start",
+                               (have_srow)
+                               ? cbuf
+                               : NULL,
+                               "delta",
+                               (-20 != delta)
+                               ? dbuf
+                               : NULL,
+                               "timeout_ms",
+                               (0 != timeout_ms)
+                               ? tbuf
+                               : NULL,
+                               NULL);
   }
-
-finished:
   if (NULL == ogh->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
diff --git a/src/lib/merchant_api_get_reserves.c 
b/src/lib/merchant_api_get_reserves.c
index 102d433..017dcd9 100644
--- a/src/lib/merchant_api_get_reserves.c
+++ b/src/lib/merchant_api_get_reserves.c
@@ -226,8 +226,8 @@ struct TALER_MERCHANT_ReservesGetHandle *
 TALER_MERCHANT_reserves_get (struct GNUNET_CURL_Context *ctx,
                              const char *backend_url,
                              struct GNUNET_TIME_Absolute after,
-                             enum TALER_MERCHANT_YesNoAll active,
-                             enum TALER_MERCHANT_YesNoAll failures,
+                             enum TALER_EXCHANGE_YesNoAll active,
+                             enum TALER_EXCHANGE_YesNoAll failures,
                              TALER_MERCHANT_ReservesGetCallback cb,
                              void *cb_cls)
 {
@@ -241,15 +241,10 @@ TALER_MERCHANT_reserves_get (struct GNUNET_CURL_Context 
*ctx,
   rgh->ctx = ctx;
   rgh->cb = cb;
   rgh->cb_cls = cb_cls;
-  if (TALER_MERCHANT_YNA_YES == active)
-    active_s = "yes";
-  if (TALER_MERCHANT_YNA_NO == active)
-    active_s = "no";
-  if (TALER_MERCHANT_YNA_YES == failures)
-    failures_s = "yes";
-  if (TALER_MERCHANT_YNA_NO == failures)
-    failures_s = "no";
-  after_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (after));
+  active_s = TALER_yna_to_string (active);
+  failures_s = TALER_yna_to_string (failures);
+  after_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (
+                             after));
   rgh->url = TALER_url_join (backend_url,
                              "reserves",
                              "active",
diff --git a/src/lib/merchant_api_get_tips.c b/src/lib/merchant_api_get_tips.c
index 63bcb94..d15ed82 100644
--- a/src/lib/merchant_api_get_tips.c
+++ b/src/lib/merchant_api_get_tips.c
@@ -243,7 +243,7 @@ TALER_MERCHANT_tips_get (
 struct TALER_MERCHANT_TipsGetHandle *
 TALER_MERCHANT_tips_get2 (struct GNUNET_CURL_Context *ctx,
                           const char *backend_url,
-                          enum TALER_MERCHANT_YesNoAll expired,
+                          enum TALER_EXCHANGE_YesNoAll expired,
                           int64_t limit,
                           uint64_t offset,
                           TALER_MERCHANT_TipsGetCallback cb,
@@ -265,90 +265,33 @@ TALER_MERCHANT_tips_get2 (struct GNUNET_CURL_Context *ctx,
 
   /* build tgh->url with the various optional arguments */
   {
-    struct GNUNET_Buffer buf = { 0 };
-    bool first = true;
-    /**
-     * Macro to append @a a and @a b to @a buf, using
-     * the right separators between key (@a a) and
-     * value (@a b). Uses "first" to decide between
-     * using "?" and "&" as the separator.
-     *
-     * @param a a key
-     * @param b a value
-     */
-#define APPEND(a,b)                             \
-  do { \
-    if (first) \
-    GNUNET_buffer_write_str (&buf, \
-                             "?");            \
-    else \
-    GNUNET_buffer_write_str (&buf, \
-                             "&"); \
-    first = false; \
-    GNUNET_buffer_write_str (&buf, (a)); \
-    GNUNET_buffer_write_str (&buf, "="); \
-    GNUNET_buffer_write_str (&buf, (b)); \
-  } while (0)
+    char cbuf[30];
+    char lbuf[30];
+    bool have_offset;
 
-    {
-      char *url;
+    GNUNET_snprintf (lbuf,
+                     sizeof (lbuf),
+                     "%lld",
+                     (long long) limit);
 
-      url = TALER_url_join (backend_url,
-                            "private/tips",
-                            NULL);
-      if (NULL == url)
-        goto finished;
-      GNUNET_buffer_write_str (&buf,
-                               url);
-      GNUNET_free (url);
-    }
-    if (TALER_MERCHANT_YNA_NO != expired)
-      APPEND ("expired",
-              (TALER_MERCHANT_YNA_YES == expired) ? "yes" : "all");
     if (limit > 0)
-    {
-      if (0 != offset)
-      {
-        char cbuf[30];
-
-        GNUNET_snprintf (cbuf,
-                         sizeof (cbuf),
-                         "%llu",
-                         (unsigned long long) offset);
-        APPEND ("offset",
-                cbuf);
-      }
-    }
+      have_offset = (0 != offset);
     else
-    {
-      if (UINT64_MAX != offset)
-      {
-        char cbuf[30];
-
-        GNUNET_snprintf (cbuf,
-                         sizeof (cbuf),
-                         "%llu",
-                         (unsigned long long) offset);
-        APPEND ("offset",
-                cbuf);
-      }
-    }
-    if (-20 != limit)
-    {
-      char cbuf[30];
-
-      GNUNET_snprintf (cbuf,
-                       sizeof (cbuf),
-                       "%lld",
-                       (long long) limit);
-      APPEND ("limit",
-              cbuf);
-    }
-    tgh->url = GNUNET_buffer_reap_str (&buf);
-#undef APPEND
+      have_offset = (UINT64_MAX != offset);
+    GNUNET_snprintf (cbuf,
+                     sizeof (cbuf),
+                     "%llu",
+                     (unsigned long long) offset);
+    tgh->url = TALER_url_join (backend_url,
+                               "private/tips",
+                               "expired",
+                               (TALER_EXCHANGE_YNA_ALL != expired)
+                               ? TALER_yna_to_string (expired)
+                               : NULL,
+                               "offset", (have_offset) ? cbuf : NULL,
+                               "limit", (-20 != limit) ? lbuf : NULL,
+                               NULL);
   }
-
-finished:
   if (NULL == tgh->url)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
diff --git a/src/lib/merchant_api_get_transfers.c 
b/src/lib/merchant_api_get_transfers.c
index ac8382c..552311c 100644
--- a/src/lib/merchant_api_get_transfers.c
+++ b/src/lib/merchant_api_get_transfers.c
@@ -250,7 +250,7 @@ TALER_MERCHANT_transfers_get (
   const struct GNUNET_TIME_Absolute after,
   int64_t limit,
   uint64_t offset,
-  enum TALER_MERCHANT_YesNoAll verified,
+  enum TALER_EXCHANGE_YesNoAll verified,
   TALER_MERCHANT_GetTransfersCallback cb,
   void *cb_cls)
 {
@@ -266,10 +266,7 @@ TALER_MERCHANT_transfers_get (
   gth->ctx = ctx;
   gth->cb = cb;
   gth->cb_cls = cb_cls;
-  if (TALER_MERCHANT_YNA_YES == verified)
-    verified_s = "yes";
-  if (TALER_MERCHANT_YNA_NO == verified)
-    verified_s = "no";
+  verified_s = TALER_yna_to_string (verified);
   GNUNET_snprintf (limit_s,
                    sizeof (limit_s),
                    "%lld",
@@ -285,7 +282,9 @@ TALER_MERCHANT_transfers_get (
                              "payto_uri",
                              payto_uri,
                              "verified",
-                             verified_s,
+                             (TALER_EXCHANGE_YNA_ALL != verified)
+                             ? verified_s
+                             : NULL,
                              "limit",
                              0 != limit
                              ? limit_s

-- 
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]