From fd0355b114f234c7592df028e76d6924f7454958 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 1 Aug 2021 16:40:06 +0200 Subject: -more json_pack fun --- src/backend/taler-merchant-httpd_exchanges.c | 9 +- .../taler-merchant-httpd_private-post-orders.c | 128 ++++++++-------- .../taler-merchant-httpd_private-post-transfers.c | 168 ++++++++++----------- 3 files changed, 148 insertions(+), 157 deletions(-) (limited to 'src') diff --git a/src/backend/taler-merchant-httpd_exchanges.c b/src/backend/taler-merchant-httpd_exchanges.c index b1657e17..16d9e481 100644 --- a/src/backend/taler-merchant-httpd_exchanges.c +++ b/src/backend/taler-merchant-httpd_exchanges.c @@ -1288,10 +1288,11 @@ TMH_EXCHANGES_init (const struct GNUNET_CONFIGURATION_Handle *cfg) if (! exchange->trusted) continue; - j_exchange = json_pack ("{s:s, s:o}", - "url", exchange->url, - "master_pub", GNUNET_JSON_from_data_auto ( - &exchange->master_pub)); + j_exchange = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("url", + exchange->url), + GNUNET_JSON_pack_data_auto ("master_pub", + &exchange->master_pub)); GNUNET_assert (0 == json_array_append_new (TMH_trusted_exchanges, j_exchange)); diff --git a/src/backend/taler-merchant-httpd_private-post-orders.c b/src/backend/taler-merchant-httpd_private-post-orders.c index c3923995..10f97944 100644 --- a/src/backend/taler-merchant-httpd_private-post-orders.c +++ b/src/backend/taler-merchant-httpd_private-post-orders.c @@ -421,16 +421,18 @@ execute_order (struct MHD_Connection *connection, if (0 == GNUNET_memcmp (&orig_post, h_post_data)) { - ret = TALER_MHD_reply_json_pack ( + ret = TALER_MHD_REPLY_JSON_PACK ( connection, MHD_HTTP_OK, - "{s:s, s:o?}", - "order_id", - order_id, - "token", - (GNUNET_YES == GNUNET_is_zero (&token)) - ? NULL - : GNUNET_JSON_from_data_auto (&token)); + GNUNET_JSON_pack_string ("order_id", + order_id), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_data_varsize ( + "token", + (GNUNET_YES == GNUNET_is_zero (&token)) + ? NULL + : GNUNET_JSON_from_data_auto (&token), + sizeof (token)))); } else { @@ -515,37 +517,37 @@ execute_order (struct MHD_Connection *connection, switch (qs) { case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - ret = TALER_MHD_reply_json_pack (connection, - MHD_HTTP_GONE, - "{s:s,s:I,s:I,s:o?}", - "product_id", - inventory_products[out_of_stock_index]. - product_id, - "requested_quantity", - inventory_products[out_of_stock_index]. - quantity, - "available_quantity", - pd.total_stock - pd.total_sold - - pd.total_lost, - "restock_expected", - (0 == pd.next_restock.abs_value_us) - ? NULL - : GNUNET_JSON_from_time_abs ( - pd.next_restock)); + ret = TALER_MHD_REPLY_JSON_PACK ( + connection, + MHD_HTTP_GONE, + GNUNET_JSON_pack_string ( + "product_id", + inventory_products[out_of_stock_index].product_id), + GNUNET_JSON_pack_uint64 ( + "requested_quantity", + inventory_products[out_of_stock_index].quantity), + GNUNET_JSON_pack_uint64 ( + "available_quantity", + pd.total_stock - pd.total_sold - pd.total_lost), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_time_abs ( + "restock_expected", + pd.next_restock))); TALER_MERCHANTDB_product_details_free (&pd); return ret; case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - return TALER_MHD_reply_json_pack (connection, - MHD_HTTP_GONE, - "{s:s,s:I,s:I}", - "product_id", - inventory_products[out_of_stock_index]. - product_id, - "requested_quantity", - inventory_products[out_of_stock_index]. - quantity, - "available_quantity", - (json_int_t) 0); + return TALER_MHD_REPLY_JSON_PACK ( + connection, + MHD_HTTP_GONE, + GNUNET_JSON_pack_string ( + "product_id", + inventory_products[out_of_stock_index].product_id), + GNUNET_JSON_pack_uint64 ( + "requested_quantity", + inventory_products[out_of_stock_index].quantity), + GNUNET_JSON_pack_uint64 ( + "available_quantity", + 0)); case GNUNET_DB_STATUS_SOFT_ERROR: GNUNET_break (0); return TALER_MHD_reply_with_error ( @@ -568,16 +570,18 @@ execute_order (struct MHD_Connection *connection, { MHD_RESULT ret; - ret = TALER_MHD_reply_json_pack ( + ret = TALER_MHD_REPLY_JSON_PACK ( connection, MHD_HTTP_OK, - "{s:s, s:o?}", - "order_id", - order_id, - "token", - (GNUNET_YES == GNUNET_is_zero (claim_token)) - ? NULL - : GNUNET_JSON_from_data_auto (claim_token)); + GNUNET_JSON_pack_string ("order_id", + order_id), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_data_varsize ( + "token", + (GNUNET_YES == GNUNET_is_zero (&claim_token)) + ? NULL + : GNUNET_JSON_from_data_auto (&claim_token), + sizeof (claim_token)))); GNUNET_JSON_parse_free (spec); return ret; } @@ -937,8 +941,9 @@ patch_order (struct MHD_Connection *connection, TALER_EC_MERCHANT_PRIVATE_POST_ORDERS_PROPOSAL_PARSE_ERROR, "'merchant' field already set, but must be provided by backend"); } - jmerchant = json_pack ("{s:s}", - "name", settings->name); + jmerchant = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("name", + settings->name)); GNUNET_assert (NULL != jmerchant); { json_t *loca; @@ -1220,21 +1225,22 @@ merge_inventory (struct MHD_Connection *connection, { json_t *p; - p = json_pack ("{s:s, s:o, s:s, s:o, s:o, s:s, s:I}", - "description", - pd.description, - "description_i18n", - pd.description_i18n, - "unit", - pd.unit, - "price", - TALER_JSON_from_amount (&pd.price), - "taxes", - pd.taxes, - "image", - pd.image, - "quantity", - (json_int_t) inventory_products[i].quantity); + p = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("description", + pd.description), + GNUNET_JSON_pack_object_steal ("description_i18n", + pd.description_i18n), + GNUNET_JSON_pack_string ("unit", + pd.unit), + TALER_JSON_pack_amount ("price", + &pd.price), + GNUNET_JSON_pack_object_steal ("taxes", + pd.taxes), + GNUNET_JSON_pack_string ("image", + pd.image), + GNUNET_JSON_pack_uint64 ("quantity", + inventory_products[i]. + quantity)); GNUNET_assert (NULL != p); GNUNET_assert (0 == json_array_append_new (np, diff --git a/src/backend/taler-merchant-httpd_private-post-transfers.c b/src/backend/taler-merchant-httpd_private-post-transfers.c index a9a0da75..1dd5d589 100644 --- a/src/backend/taler-merchant-httpd_private-post-transfers.c +++ b/src/backend/taler-merchant-httpd_private-post-transfers.c @@ -25,6 +25,7 @@ #include #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" +#include "taler-merchant-httpd_helper.h" #include "taler-merchant-httpd_private-post-transfers.h" @@ -320,44 +321,35 @@ check_transfer (void *cls, /* Build the `TrackTransferConflictDetails` */ ptc->response_code = MHD_HTTP_ACCEPTED; ptc->response - = TALER_MHD_make_json_pack ( - "{s:I, s:s, s:s, s:o, s:o," - " s:I, s:o, s:o, s:o, s:o," - " s:o, s:o, s:o, s:o, s:o }", - "code", - (json_int_t) - TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_REPORTS, - "hint", - TALER_ErrorCode_get_hint ( + = TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_REPORTS), - "exchange_url", - exchange_url, - "deposit_timestamp", - GNUNET_JSON_from_time_abs (deposit_timestamp), - "refund_deadline", - GNUNET_JSON_from_time_abs (refund_deadline), - /* first block of 5 */ - "conflict_offset", - (json_int_t) ptc->current_offset, - "coin_pub", - GNUNET_JSON_from_data_auto (&ttd->coin_pub), - "h_wire", - GNUNET_JSON_from_data_auto (h_wire), - "deposit_exchange_sig", - GNUNET_JSON_from_data_auto (exchange_sig), - "deposit_exchange_pub", - GNUNET_JSON_from_data_auto (exchange_pub), - /* first block of 5 */ - "h_contract_terms", - GNUNET_JSON_from_data_auto (&ttd->h_contract_terms), - "amount_with_fee", - TALER_JSON_from_amount (amount_with_fee), - "coin_value", - TALER_JSON_from_amount (&ttd->coin_value), - "coin_fee", - TALER_JSON_from_amount (&ttd->coin_fee), - "deposit_fee", - TALER_JSON_from_amount (deposit_fee)); + GNUNET_JSON_pack_string ("exchange_url", + exchange_url), + GNUNET_JSON_pack_time_abs ("deposit_timestamp", + deposit_timestamp), + GNUNET_JSON_pack_time_abs ("refund_deadline", + refund_deadline), + GNUNET_JSON_pack_uint64 ("conflict_offset", + ptc->current_offset), + GNUNET_JSON_pack_data_auto ("coin_pub", + &ttd->coin_pub), + GNUNET_JSON_pack_data_auto ("h_wire", + h_wire), + GNUNET_JSON_pack_data_auto ("deposit_exchange_sig", + exchange_sig), + GNUNET_JSON_pack_data_auto ("deposit_exchange_pub", + exchange_pub), + GNUNET_JSON_pack_data_auto ("h_contract_terms", + &ttd->h_contract_terms), + TALER_JSON_pack_amount ("amount_with_fee", + amount_with_fee), + TALER_JSON_pack_amount ("coin_value", + &ttd->coin_value), + TALER_JSON_pack_amount ("coin_fee", + &ttd->coin_fee), + TALER_JSON_pack_amount ("deposit_fee", + deposit_fee)); return; } ptc->check_transfer_result = GNUNET_OK; @@ -433,21 +425,25 @@ check_wire_fee (struct PostTransfersContext *ptc, /* Wire fee check failed, export proof to client */ ptc->response_code = MHD_HTTP_ACCEPTED; ptc->response = - TALER_MHD_make_json_pack ( - "{s:I, s:s, s:o, s:o, s:o, s:o, s:o, s:o, s:o, s:o}", - "code", - (json_int_t) TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_BAD_WIRE_FEE, - "hint", - TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_BAD_WIRE_FEE), - "wire_fee", TALER_JSON_from_amount (wire_fee), - "execution_time", GNUNET_JSON_from_time_abs (execution_time), - "expected_wire_fee", TALER_JSON_from_amount (&expected_fee), - "expected_closing_fee", TALER_JSON_from_amount (&closing_fee), - "start_date", GNUNET_JSON_from_time_abs (start_date), - "end_date", GNUNET_JSON_from_time_abs (end_date), - "master_sig", GNUNET_JSON_from_data_auto (&master_sig), - "master_pub", GNUNET_JSON_from_data_auto (&ptc->master_pub)); + TALER_JSON_pack_amount ("wire_fee", + wire_fee), + GNUNET_JSON_pack_time_abs ("execution_time", + execution_time), + TALER_JSON_pack_amount ("expected_wire_fee", + &expected_fee), + TALER_JSON_pack_amount ("expected_closing_fee", + &closing_fee), + GNUNET_JSON_pack_time_abs ("start_date", + start_date), + GNUNET_JSON_pack_time_abs ("end_date", + end_date), + GNUNET_JSON_pack_data_auto ("master_sig", + &master_sig), + GNUNET_JSON_pack_data_auto ("master_pub", + &ptc->master_pub)); GNUNET_free (wire_method); return GNUNET_SYSERR; } @@ -482,24 +478,19 @@ wire_transfer_cb (void *cls, resume_transfer_with_response ( ptc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - "{s:I, s:I, s:I}", - "code", - (json_int_t) TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_EXCHANGE_UNKNOWN, - "exchange_code", (json_int_t) hr->ec, - "exchange_http_status", (json_int_t) hr->http_status)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( + TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_EXCHANGE_UNKNOWN), + TMH_pack_exchange_reply (hr))); return; default: resume_transfer_with_response ( ptc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - "{s:I, s:I, s:I, s:O}", - "code", - (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS, - "exchange_code", (json_int_t) hr->ec, - "exchange_http_status", (json_int_t) hr->http_status, - "exchange_reply", hr->reply)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( + TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS), + TMH_pack_exchange_reply (hr))); return; } TMH_db->preflight (TMH_db->cls); @@ -583,12 +574,8 @@ process_transfer_with_exchange (void *cls, resume_transfer_with_response ( ptc, MHD_HTTP_GATEWAY_TIMEOUT, - TALER_MHD_make_json_pack ( - "{s:s, s:I}" - "hint", TALER_ErrorCode_get_hint ( - TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT), - "code", - (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT)); + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec (TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT))); return; } if (NULL == eh) @@ -598,16 +585,10 @@ process_transfer_with_exchange (void *cls, resume_transfer_with_response ( ptc, MHD_HTTP_BAD_GATEWAY, - TALER_MHD_make_json_pack ( - (NULL != hr->reply) - ? "{s:s, s:I, s:I, s:I, s:O}" - : "{s:s, s:I, s:I, s:I}", - "hint", TALER_ErrorCode_get_hint ( + TALER_MHD_MAKE_JSON_PACK ( + TALER_JSON_pack_ec ( TALER_EC_MERCHANT_GENERIC_EXCHANGE_CONNECT_FAILURE), - "code", (json_int_t) TALER_EC_MERCHANT_GENERIC_EXCHANGE_CONNECT_FAILURE, - "exchange_http_status", (json_int_t) hr->http_status, - "exchange_code", (json_int_t) hr->ec, - "exchange_reply", hr->reply)); + TMH_pack_exchange_reply (hr))); return; } @@ -827,13 +808,13 @@ hashmap_update_and_free (void *cls, 0 == json_array_append_new ( ja, - json_pack ("{s:s,s:o,s:o}", - "order_id", - entry->order_id, - "deposit_value", - TALER_JSON_from_amount (&entry->deposit_value), - "deposit_fee", - TALER_JSON_from_amount (&entry->deposit_fee)))); + GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("order_id", + entry->order_id), + TALER_JSON_pack_amount ("deposit_value", + &entry->deposit_value), + TALER_JSON_pack_amount ("deposit_fee", + &entry->deposit_fee)))); } GNUNET_free (entry->order_id); GNUNET_free (entry); @@ -1340,14 +1321,17 @@ TMH_private_post_transfers (const struct TMH_RequestHandler *rh, &hashmap_update_and_free, deposit_sums); GNUNET_CONTAINER_multihashmap_destroy (map); - return TALER_MHD_reply_json_pack ( + return TALER_MHD_REPLY_JSON_PACK ( connection, MHD_HTTP_OK, - "{s:o,s:o,s:o,s:o}", - "total", TALER_JSON_from_amount (&total_amount), - "wire_fee", TALER_JSON_from_amount (&wire_fee), - "execution_time", GNUNET_JSON_from_time_abs (execution_time), - "deposit_sums", deposit_sums); + TALER_JSON_pack_amount ("total", + &total_amount), + TALER_JSON_pack_amount ("wire_fee", + &wire_fee), + GNUNET_JSON_pack_time_abs ("execution_time", + execution_time), + GNUNET_JSON_pack_array_steal ("deposit_sums", + deposit_sums)); } /* end of 'verified == true' (not an 'if'!) */ } /* end of 'switch (qs)' */ GNUNET_assert (0); -- cgit v1.2.3