diff options
-rw-r--r-- | src/backend/Makefile.am | 2 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 7 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-instances.c | 17 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-products.c | 96 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-products.h | 41 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-post-instances.c | 13 | ||||
-rw-r--r-- | src/include/taler_merchantdb_plugin.h | 39 |
7 files changed, 189 insertions, 26 deletions
diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index 916fc0f3..14aed023 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -27,6 +27,8 @@ taler_merchant_httpd_SOURCES = \ taler-merchant-httpd_private-delete-instances-ID.h \ taler-merchant-httpd_private-get-instances.c \ taler-merchant-httpd_private-get-instances.h \ + taler-merchant-httpd_private-get-products.c \ + taler-merchant-httpd_private-get-products.h \ taler-merchant-httpd_private-get-instances-ID.c \ taler-merchant-httpd_private-get-instances-ID.h \ taler-merchant-httpd_private-patch-instances-ID.c \ diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index 02150daf..af77a5db 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -31,6 +31,7 @@ #include "taler-merchant-httpd_private-delete-instances-ID.h" #include "taler-merchant-httpd_private-get-instances.h" #include "taler-merchant-httpd_private-get-instances-ID.h" +#include "taler-merchant-httpd_private-get-products.h" #include "taler-merchant-httpd_private-patch-instances-ID.h" #include "taler-merchant-httpd_private-post-instances.h" @@ -759,6 +760,12 @@ url_handler (void *cls, .skip_instance = true, .handler = &TMH_private_post_instances }, + /* GET /products: */ + { + .url_prefix = "/products", + .method = MHD_HTTP_METHOD_GET, + .handler = &TMH_private_get_products + }, { NULL } diff --git a/src/backend/taler-merchant-httpd_private-get-instances.c b/src/backend/taler-merchant-httpd_private-get-instances.c index 74dbfefb..e42cea24 100644 --- a/src/backend/taler-merchant-httpd_private-get-instances.c +++ b/src/backend/taler-merchant-httpd_private-get-instances.c @@ -98,7 +98,6 @@ TMH_private_get_instances (const struct TMH_RequestHandler *rh, struct MHD_Connection *connection, struct TMH_HandlerContext *hc) { - struct MHD_Response *response; json_t *ia; (void) hc; @@ -107,18 +106,10 @@ TMH_private_get_instances (const struct TMH_RequestHandler *rh, GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map, &add_instance, ia); - response = TALER_MHD_make_json_pack ("{s:o}", - "instances", ia); - GNUNET_assert (NULL != response); - { - MHD_RESULT ret; - - ret = MHD_queue_response (connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); - return ret; - } + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "instances", ia); } diff --git a/src/backend/taler-merchant-httpd_private-get-products.c b/src/backend/taler-merchant-httpd_private-get-products.c new file mode 100644 index 00000000..8f70c9fe --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-products.c @@ -0,0 +1,96 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file backend/taler-merchant-httpd_private-get-products.c + * @brief implement GET /products + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler-merchant-httpd_private-get-products.h" + + +/** + * Add product details to our JSON array. + * + * @param cls a `json_t *` JSON array to build + * @param key unused + * @param product_id ID of the product + * @param in_stock how many are currently in stock (possibly locked), -1 for infinite + * @param unit in which unit is the stock measured in + */ +static void +add_product (void *cls, + const struct GNUNET_HashCode *key, + const char *product_id, + long long in_stock, + const char *unit) +{ + json_t *pa = cls; + + (void) key; + GNUNET_assert (0 == + json_array_append_new ( + pa, + json_pack ( + "{s:s, s:I, s:s}", + "product_id", + product_id, + "stock", + (json_int_t) in_stock, + "unit", + unit))); +} + + +/** + * Handle a GET "/products" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_products (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + json_t *pa; + enum GNUNET_DB_QueryStatus qs; + + pa = json_array (); + GNUNET_assert (NULL != pa); + qs = TMH_db->lookup_products (TMH_db->cls, + hc->instance->settings.id, + &add_product, + pa); + if (0 > qs) + { + GNUNET_break (0); + json_decref (pa); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GET_PRODUCTS_DB_LOOKUP_ERROR, + "failed to lookup products in database"); + } + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "products", pa); +} + + +/* end of taler-merchant-httpd_private-get-products.c */ diff --git a/src/backend/taler-merchant-httpd_private-get-products.h b/src/backend/taler-merchant-httpd_private-get-products.h new file mode 100644 index 00000000..dbfd59eb --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-products.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ +/** + * @file backend/taler-merchant-httpd_private-get-products.h + * @brief implement GET /products + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_PRODUCTS_H +#define TALER_MERCHANT_HTTPD_PRIVATE_GET_PRODUCTS_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a GET "/products" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_products (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-get-products.h */ +#endif diff --git a/src/backend/taler-merchant-httpd_private-post-instances.c b/src/backend/taler-merchant-httpd_private-post-instances.c index 18466097..75c5b2e6 100644 --- a/src/backend/taler-merchant-httpd_private-post-instances.c +++ b/src/backend/taler-merchant-httpd_private-post-instances.c @@ -363,8 +363,7 @@ TMH_private_post_instances (const struct TMH_RequestHandler *rh, if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { TMH_db->rollback (TMH_db->cls); - // TODO: only on soft error do: - continue; + goto retry; } for (struct TMH_WireMethod *wm = wm_head; NULL != wm; @@ -393,13 +392,13 @@ TMH_private_post_instances (const struct TMH_RequestHandler *rh, if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { TMH_db->rollback (TMH_db->cls); - // TODO: only on soft error do: - continue; + goto retry; } qs = TMH_db->commit (TMH_db->cls); - if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) - break; /* success! */ - } +retry: + if (GNUNET_DB_STATUS_SOFT_ERROR != qs) + break; /* success! -- or hard failure */ + } /* for .. MAX_RETRIES */ if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) { GNUNET_JSON_parse_free (spec); diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index 4723ca46..fa67daa8 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -141,6 +141,25 @@ typedef void /** + * Typically called by `lookup_products`. + * + * @param cls a `json_t *` JSON array to build + * @param key unused + * @param product_id ID of the product + * @param in_stock how many are currently in stock (possibly locked), -1 for infinite + * @param unit in which unit is the stock measured in + */ +typedef void +(*TALER_MERCHANTDB_ProductsCallback)(void *cls, + const struct GNUNET_HashCode *key, + const char *product_id, + long long in_stock, + const char *unit); + + +/* **************** OLD: ******************** */ + +/** * Typically called by `find_contract_terms_by_date`. * * @param cls closure @@ -305,7 +324,6 @@ struct TALER_MERCHANTDB_Plugin void (*preflight) (void *cls); - /** * Start a transaction. * @@ -318,7 +336,6 @@ struct TALER_MERCHANTDB_Plugin (*start) (void *cls, const char *name); - /** * Roll back the current transaction of a database connection. * @@ -328,7 +345,6 @@ struct TALER_MERCHANTDB_Plugin void (*rollback) (void *cls); - /** * Commit the current transaction of a database connection. * @@ -338,7 +354,6 @@ struct TALER_MERCHANTDB_Plugin enum GNUNET_DB_QueryStatus (*commit)(void *cls); - /** * Lookup all of the instances this backend has configured. * @@ -353,7 +368,6 @@ struct TALER_MERCHANTDB_Plugin TALER_MERCHANTDB_InstanceCallback cb, void *cb_cls); - /** * Insert information about an instance into our database. * @@ -395,7 +409,6 @@ struct TALER_MERCHANTDB_Plugin void *cls, const char *merchant_id); - /** * Purge an instance and all associated information from our database. * Highly likely to cause undesired data loss. Use with caution. @@ -430,6 +443,20 @@ struct TALER_MERCHANTDB_Plugin (*inactivate_account)(void *cls, const struct GNUNET_HashCode *h_wire); + /** + * Lookup all of the products the given instance has configured. + * + * @param cls closure + * @param instance_id instance to lookup products for + * @param cb function to call on all products found + * @param cb_cls closure for @a cb + */ + enum GNUNET_DB_QueryStatus + (*lookup_products)(void *cls, + const char *instance_id, + TALER_MERCHANTDB_ProductsCallback cb, + void *cb_cls); + /* ****************** OLD API ******************** */ |