diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2022-07-23 21:02:26 +0200 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2022-07-23 21:02:26 +0200 |
commit | 60c96242e10b3e2bab8c4a75565767795f79cbd3 (patch) | |
tree | 63b0ae7aa59c2ebcc1df18e0e974220f024e2c6d /src/lib/merchant_api_merchant_get_tip.c | |
parent | 02e9d6e2cd9f0b27f1636f4d27f8ca026b837b6f (diff) | |
download | merchant-60c96242e10b3e2bab8c4a75565767795f79cbd3.tar.gz merchant-60c96242e10b3e2bab8c4a75565767795f79cbd3.tar.bz2 merchant-60c96242e10b3e2bab8c4a75565767795f79cbd3.zip |
-improve get tip API, note: long-polling not actually implemented
Diffstat (limited to 'src/lib/merchant_api_merchant_get_tip.c')
-rw-r--r-- | src/lib/merchant_api_merchant_get_tip.c | 137 |
1 files changed, 57 insertions, 80 deletions
diff --git a/src/lib/merchant_api_merchant_get_tip.c b/src/lib/merchant_api_merchant_get_tip.c index 020167c4..9c21377e 100644 --- a/src/lib/merchant_api_merchant_get_tip.c +++ b/src/lib/merchant_api_merchant_get_tip.c @@ -60,20 +60,15 @@ struct TALER_MERCHANT_TipMerchantGetHandle }; -static int +static enum GNUNET_GenericReturnValue parse_pickups (const json_t *pa, - const struct TALER_Amount *total_authorized, - const struct TALER_Amount *total_picked_up, - const char *reason, - struct GNUNET_TIME_Timestamp expiration, - const struct TALER_ReservePublicKeyP *reserve_pub, + struct TALER_MERCHANT_TipStatusResponse *tsr, struct TALER_MERCHANT_TipMerchantGetHandle *tgh) { unsigned int pa_len = json_array_size (pa); struct TALER_MERCHANT_PickupDetail pickups[pa_len]; size_t index; json_t *value; - int ret = GNUNET_OK; json_array_foreach (pa, index, value) { @@ -95,27 +90,14 @@ parse_pickups (const json_t *pa, NULL)) { GNUNET_break_op (0); - ret = GNUNET_SYSERR; - break; + return GNUNET_SYSERR; } } - if (GNUNET_OK == ret) - { - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = MHD_HTTP_OK - }; - - tgh->cb (tgh->cb_cls, - &hr, - total_authorized, - total_picked_up, - reason, - expiration, - reserve_pub, - pa_len, - pickups); - } - return ret; + tsr->details.success.pickups_length = pa_len; + tsr->details.success.pickups = pickups; + tgh->cb (tgh->cb_cls, + tsr); + return GNUNET_OK; } @@ -134,9 +116,9 @@ handle_merchant_tip_get_finished (void *cls, { struct TALER_MERCHANT_TipMerchantGetHandle *tgh = cls; const json_t *json = response; - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = (unsigned int) response_code, - .reply = json + struct TALER_MERCHANT_TipStatusResponse tsr = { + .hr.http_status = (unsigned int) response_code, + .hr.reply = json }; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -147,22 +129,17 @@ handle_merchant_tip_get_finished (void *cls, { case MHD_HTTP_OK: { - struct TALER_Amount total_authorized; - struct TALER_Amount total_picked_up; - const char *reason; - struct GNUNET_TIME_Timestamp expiration; - struct TALER_ReservePublicKeyP reserve_pub; struct GNUNET_JSON_Specification spec[] = { TALER_JSON_spec_amount_any ("total_authorized", - &total_authorized), + &tsr.details.success.total_authorized), TALER_JSON_spec_amount_any ("total_picked_up", - &total_picked_up), + &tsr.details.success.total_picked_up), GNUNET_JSON_spec_string ("reason", - &reason), + &tsr.details.success.reason), GNUNET_JSON_spec_timestamp ("expiration", - &expiration), + &tsr.details.success.expiration), GNUNET_JSON_spec_fixed_auto ("reserve_pub", - &reserve_pub), + &tsr.details.success.reserve_pub), GNUNET_JSON_spec_end () }; @@ -171,8 +148,8 @@ handle_merchant_tip_get_finished (void *cls, spec, NULL, NULL)) { - hr.http_status = 0; - hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + tsr.hr.http_status = 0; + tsr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; } else { @@ -181,75 +158,55 @@ handle_merchant_tip_get_finished (void *cls, if (! json_is_array (pickups)) { tgh->cb (tgh->cb_cls, - &hr, - &total_authorized, - &total_picked_up, - reason, - expiration, - &reserve_pub, - 0, - NULL); + &tsr); TALER_MERCHANT_merchant_tip_get_cancel (tgh); return; } - else if (GNUNET_OK == parse_pickups (pickups, - &total_authorized, - &total_picked_up, - reason, - expiration, - &reserve_pub, - tgh)) + if (GNUNET_OK == + parse_pickups (pickups, + &tsr, + tgh)) { GNUNET_JSON_parse_free (spec); TALER_MERCHANT_merchant_tip_get_cancel (tgh); return; } - else - { - hr.http_status = 0; - hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - } + tsr.hr.http_status = 0; + tsr.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); + tsr.hr.ec = TALER_JSON_get_error_code (json); + tsr.hr.hint = TALER_JSON_get_error_hint (json); /* Nothing really to verify, merchant says we need to authenticate. */ break; case MHD_HTTP_NOT_FOUND: /* legal, can happen if instance or tip reserve is unknown */ - hr.ec = TALER_JSON_get_error_code (json); - hr.hint = TALER_JSON_get_error_hint (json); + tsr.hr.ec = TALER_JSON_get_error_code (json); + tsr.hr.hint = TALER_JSON_get_error_hint (json); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: /* Server had an internal issue; we should retry, but this API leaves this to the application */ - hr.ec = TALER_JSON_get_error_code (json); - hr.hint = TALER_JSON_get_error_hint (json); + tsr.hr.ec = TALER_JSON_get_error_code (json); + tsr.hr.hint = TALER_JSON_get_error_hint (json); break; default: /* unexpected response code */ GNUNET_break_op (0); TALER_MERCHANT_parse_error_details_ (json, response_code, - &hr); + &tsr.hr); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected response code %u/%d\n", (unsigned int) response_code, - (int) hr.ec); + (int) tsr.hr.ec); break; } tgh->cb (tgh->cb_cls, - &hr, - NULL, - NULL, - NULL, - GNUNET_TIME_UNIT_ZERO_TS, - NULL, - 0, - NULL); + &tsr); TALER_MERCHANT_merchant_tip_get_cancel (tgh); } @@ -258,6 +215,8 @@ struct TALER_MERCHANT_TipMerchantGetHandle * TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx, const char *backend_url, const struct TALER_TipIdentifierP *tip_id, + const struct TALER_Amount *min_pick_up, + struct GNUNET_TIME_Relative lp_timeout, bool pickups, TALER_MERCHANT_TipMerchantGetCallback cb, void *cb_cls) @@ -274,6 +233,7 @@ TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx, { char res_str[sizeof (*tip_id) * 2]; char arg_str[sizeof (res_str) + 48]; + char timeout_str[32]; char *end; end = GNUNET_STRINGS_data_to_string (tip_id, @@ -285,9 +245,26 @@ TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx, sizeof (arg_str), "private/tips/%s", res_str); + GNUNET_snprintf (timeout_str, + sizeof (timeout_str), + "%llu", + ((unsigned long long) + lp_timeout.rel_value_us / + GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us)); tgh->url = TALER_url_join (backend_url, arg_str, - "pickups", pickups ? "yes" : NULL, + "pickups", + pickups + ? "yes" + : NULL, + "min_amount", + min_pick_up + ? TALER_amount2s (min_pick_up) + : NULL, + "timeout_ms", + GNUNET_TIME_relative_is_zero (lp_timeout) + ? NULL + : timeout_str, NULL); } if (NULL == tgh->url) @@ -308,8 +285,8 @@ TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx, void -TALER_MERCHANT_merchant_tip_get_cancel (struct - TALER_MERCHANT_TipMerchantGetHandle *tgh) +TALER_MERCHANT_merchant_tip_get_cancel ( + struct TALER_MERCHANT_TipMerchantGetHandle *tgh) { if (NULL != tgh->job) { |