summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/taler_merchant_service.h46
-rw-r--r--src/lib/merchant_api_get_tips.c89
-rw-r--r--src/testing/testing_api_cmd_get_tips.c36
3 files changed, 92 insertions, 79 deletions
diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h
index e8d76263..f936d54e 100644
--- a/src/include/taler_merchant_service.h
+++ b/src/include/taler_merchant_service.h
@@ -4444,21 +4444,53 @@ struct TALER_MERCHANT_TipEntry
};
-// FIXME: change signature!
+/**
+ * Response to a GET /private/tips request.
+ */
+struct TALER_MERCHANT_TipsGetResponse
+{
+ /**
+ * HTTP response details
+ */
+ struct TALER_MERCHANT_HttpResponse hr;
+
+ /**
+ * Details depending on status.
+ */
+ union
+ {
+
+ /**
+ * Details if status is #MHD_HTTP_OK.
+ */
+ struct
+ {
+ /**
+ * length of the @e tips array
+ */
+ unsigned int tips_length;
+
+ /**
+ * the array of tips
+ */
+ const struct TALER_MERCHANT_TipEntry *tips;
+
+ } ok;
+
+ } details;
+};
+
+
/**
* Callback to process a GET /private/tips request.
*
* @param cls closure
- * @param hr HTTP response details
- * @param tips_length length of the @a tips array
- * @param tips the array of tips, NULL on error
+ * @param tgr response details
*/
typedef void
(*TALER_MERCHANT_TipsGetCallback) (
void *cls,
- const struct TALER_MERCHANT_HttpResponse *hr,
- unsigned int tips_length,
- const struct TALER_MERCHANT_TipEntry tips[]);
+ const struct TALER_MERCHANT_TipsGetResponse *tgr);
/**
diff --git a/src/lib/merchant_api_get_tips.c b/src/lib/merchant_api_get_tips.c
index 7af6936b..4103c071 100644
--- a/src/lib/merchant_api_get_tips.c
+++ b/src/lib/merchant_api_get_tips.c
@@ -1,6 +1,6 @@
/*
This file is part of TALER
- Copyright (C) 2020 Taler Systems SA
+ Copyright (C) 2020-2023 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
@@ -68,20 +68,20 @@ struct TALER_MERCHANT_TipsGetHandle
* Parse tip information from @a ia.
*
* @param ia JSON array (or NULL!) tip order data
+ * @param[in] tgr response to complete
* @param tgh operation handle
* @return #GNUNET_OK on success
*/
-static int
+static enum GNUNET_GenericReturnValue
parse_tips (const json_t *ia,
+ struct TALER_MERCHANT_TipsGetResponse *tgr,
struct TALER_MERCHANT_TipsGetHandle *tgh)
{
unsigned int tes_len = json_array_size (ia);
- struct TALER_MERCHANT_TipEntry tes[tes_len];
+ struct TALER_MERCHANT_TipEntry tes[GNUNET_NZL (tes_len)];
size_t index;
json_t *value;
- int ret;
- ret = GNUNET_OK;
json_array_foreach (ia, index, value) {
struct TALER_MERCHANT_TipEntry *ie = &tes[index];
struct GNUNET_JSON_Specification spec[] = {
@@ -100,25 +100,15 @@ parse_tips (const json_t *ia,
NULL, NULL))
{
GNUNET_break_op (0);
- ret = GNUNET_SYSERR;
- continue;
+ return GNUNET_SYSERR;
}
- if (GNUNET_SYSERR == ret)
- break;
- }
- if (GNUNET_OK == ret)
- {
- struct TALER_MERCHANT_HttpResponse hr = {
- .http_status = MHD_HTTP_OK
- };
-
- tgh->cb (tgh->cb_cls,
- &hr,
- tes_len,
- tes);
- tgh->cb = NULL; /* just to be sure */
}
- return ret;
+ tgr->details.ok.tips_length = tes_len;
+ tgr->details.ok.tips = tes;
+ tgh->cb (tgh->cb_cls,
+ tgr);
+ tgh->cb = NULL; /* just to be sure */
+ return GNUNET_OK;
}
@@ -137,9 +127,9 @@ handle_get_tips_finished (void *cls,
{
struct TALER_MERCHANT_TipsGetHandle *tgh = cls;
const json_t *json = response;
- struct TALER_MERCHANT_HttpResponse hr = {
- .http_status = (unsigned int) response_code,
- .reply = json
+ struct TALER_MERCHANT_TipsGetResponse tgr = {
+ .hr.http_status = (unsigned int) response_code,
+ .hr.reply = json
};
tgh->job = NULL;
@@ -150,10 +140,10 @@ handle_get_tips_finished (void *cls,
{
case MHD_HTTP_OK:
{
- json_t *tips;
+ const json_t *tips;
struct GNUNET_JSON_Specification spec[] = {
- GNUNET_JSON_spec_json ("tips",
- &tips),
+ GNUNET_JSON_spec_array_const ("tips",
+ &tips),
GNUNET_JSON_spec_end ()
};
@@ -162,48 +152,39 @@ handle_get_tips_finished (void *cls,
spec,
NULL, NULL))
{
- hr.http_status = 0;
- hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
+ tgr.hr.http_status = 0;
+ tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
+ break;
}
- else
+ if (GNUNET_OK ==
+ parse_tips (tips,
+ &tgr,
+ tgh))
{
- if ( (! json_is_array (tips)) ||
- (GNUNET_OK ==
- parse_tips (tips,
- tgh)) )
- {
- GNUNET_JSON_parse_free (spec);
- TALER_MERCHANT_tips_get_cancel (tgh);
- return;
- }
- else
- {
- hr.http_status = 0;
- hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
- }
+ TALER_MERCHANT_tips_get_cancel (tgh);
+ return;
}
- GNUNET_JSON_parse_free (spec);
+ tgr.hr.http_status = 0;
+ tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
break;
}
case MHD_HTTP_UNAUTHORIZED:
- hr.ec = TALER_JSON_get_error_code (json);
- hr.hint = TALER_JSON_get_error_hint (json);
+ tgr.hr.ec = TALER_JSON_get_error_code (json);
+ tgr.hr.hint = TALER_JSON_get_error_hint (json);
/* Nothing really to verify, merchant says we need to authenticate. */
break;
default:
/* unexpected response code */
- hr.ec = TALER_JSON_get_error_code (json);
- hr.hint = TALER_JSON_get_error_hint (json);
+ tgr.hr.ec = TALER_JSON_get_error_code (json);
+ tgr.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);
+ (int) tgr.hr.ec);
break;
}
tgh->cb (tgh->cb_cls,
- &hr,
- 0,
- NULL);
+ &tgr);
TALER_MERCHANT_tips_get_cancel (tgh);
}
diff --git a/src/testing/testing_api_cmd_get_tips.c b/src/testing/testing_api_cmd_get_tips.c
index 89a82202..8ae2fa9e 100644
--- a/src/testing/testing_api_cmd_get_tips.c
+++ b/src/testing/testing_api_cmd_get_tips.c
@@ -1,6 +1,6 @@
/*
This file is part of TALER
- Copyright (C) 2020 Taler Systems SA
+ Copyright (C) 2020-2023 Taler Systems SA
TALER is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
@@ -80,41 +80,39 @@ struct GetTipsState
* Callback for a GET /private/tips operation.
*
* @param cls closure for this function
- * @param hr HTTP response details
- * @param tips_length length of the @a tips array
- * @param tips array of tips
+ * @param tgr response details
*/
static void
get_tips_cb (void *cls,
- const struct TALER_MERCHANT_HttpResponse *hr,
- unsigned int tips_length,
- const struct TALER_MERCHANT_TipEntry tips[])
+ const struct TALER_MERCHANT_TipsGetResponse *tgr)
{
struct GetTipsState *gts = cls;
gts->tgh = NULL;
- if (gts->http_status != hr->http_status)
+ if (gts->http_status != tgr->hr.http_status)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unexpected response code %u (%d) to command %s\n",
- hr->http_status,
- (int) hr->ec,
+ tgr->hr.http_status,
+ (int) tgr->hr.ec,
TALER_TESTING_interpreter_get_current_label (gts->is));
TALER_TESTING_interpreter_fail (gts->is);
return;
}
- switch (hr->http_status)
+ switch (tgr->hr.http_status)
{
case MHD_HTTP_OK:
- if (tips_length != gts->tips_length)
+ if (tgr->details.ok.tips_length != gts->tips_length)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Tips length does not match\n");
TALER_TESTING_interpreter_fail (gts->is);
return;
}
- for (unsigned int i = 0; i < tips_length; ++i)
+ for (unsigned int i = 0; i < tgr->details.ok.tips_length; ++i)
{
+ const struct TALER_MERCHANT_TipEntry *tip
+ = &tgr->details.ok.tips[i];
const struct TALER_TESTING_Command *tip_cmd;
tip_cmd = TALER_TESTING_interpreter_lookup_command (
@@ -133,7 +131,7 @@ get_tips_cb (void *cls,
return;
}
if (0 != GNUNET_memcmp (tip_id,
- &tips[i].tip_id))
+ &tip->tip_id))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Tip id does not match\n");
@@ -153,10 +151,12 @@ get_tips_cb (void *cls,
TALER_TESTING_interpreter_fail (gts->is);
return;
}
- if ((GNUNET_OK != TALER_amount_cmp_currency (tip_amount,
- &tips[i].tip_amount)) ||
- (0 != TALER_amount_cmp (tip_amount,
- &tips[i].tip_amount)))
+ if ( (GNUNET_OK !=
+ TALER_amount_cmp_currency (tip_amount,
+ &tip->tip_amount)) ||
+ (0 !=
+ TALER_amount_cmp (tip_amount,
+ &tip->tip_amount)) )
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Tip amount does not match\n");