From c1853b3942273d05dbdf6d7501a89ce5f7f8beba Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 4 Jun 2023 11:47:48 +0200 Subject: another FIXME done --- src/include/taler_merchant_service.h | 43 +++++++++++--- src/lib/merchant_api_get_templates.c | 90 +++++++++++------------------ src/testing/testing_api_cmd_get_templates.c | 45 +++++++-------- 3 files changed, 92 insertions(+), 86 deletions(-) diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h index 40499929..a91e991e 100644 --- a/src/include/taler_merchant_service.h +++ b/src/include/taler_merchant_service.h @@ -4941,21 +4941,50 @@ struct TALER_MERCHANT_TemplateEntry }; -// FIXME: change signature! +/** + * Response to a GET /templates operation. + */ +struct TALER_MERCHANT_TemplatesGetResponse +{ + /** + * 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 templates array + */ + unsigned int templates_length; + + /** + * array of templates the requested instance offers + */ + const struct TALER_MERCHANT_TemplateEntry *templates; + } ok; + } details; +}; + + /** * Function called with the result of the GET /templates operation. * * @param cls closure - * @param hr HTTP response details - * @param templates_length length of the @a templates array - * @param templates array of templates the requested instance offers + * @param tgr response details */ typedef void (*TALER_MERCHANT_TemplatesGetCallback)( void *cls, - const struct TALER_MERCHANT_HttpResponse *hr, - unsigned int templates_length, - const struct TALER_MERCHANT_TemplateEntry templates[]); + const struct TALER_MERCHANT_TemplatesGetResponse *tgr); /** diff --git a/src/lib/merchant_api_get_templates.c b/src/lib/merchant_api_get_templates.c index 4af6846e..a9807ae7 100644 --- a/src/lib/merchant_api_get_templates.c +++ b/src/lib/merchant_api_get_templates.c @@ -71,19 +71,18 @@ struct TALER_MERCHANT_TemplatesGetHandle * @param tgh operation handle * @return #GNUNET_OK on success */ -static int +static enum GNUNET_GenericReturnValue parse_templates (const json_t *ia, + struct TALER_MERCHANT_TemplatesGetResponse *tgr, struct TALER_MERCHANT_TemplatesGetHandle *tgh) { - unsigned int ies_len = json_array_size (ia); - struct TALER_MERCHANT_TemplateEntry ies[GNUNET_NZL (ies_len)]; + unsigned int tmpl_len = json_array_size (ia); + struct TALER_MERCHANT_TemplateEntry tmpl[GNUNET_NZL (tmpl_len)]; size_t index; json_t *value; - int ret; - ret = GNUNET_OK; json_array_foreach (ia, index, value) { - struct TALER_MERCHANT_TemplateEntry *ie = &ies[index]; + struct TALER_MERCHANT_TemplateEntry *ie = &tmpl[index]; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_string ("template_id", &ie->template_id), @@ -96,25 +95,15 @@ parse_templates (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, - ies_len, - ies); - tgh->cb = NULL; /* just to be sure */ } - return ret; + tgr->details.ok.templates_length = tmpl_len; + tgr->details.ok.templates = tmpl; + tgh->cb (tgh->cb_cls, + tgr); + tgh->cb = NULL; /* just to be sure */ + return GNUNET_OK; } @@ -133,9 +122,9 @@ handle_get_templates_finished (void *cls, { struct TALER_MERCHANT_TemplatesGetHandle *tgh = cls; const json_t *json = response; - struct TALER_MERCHANT_HttpResponse hr = { - .http_status = (unsigned int) response_code, - .reply = json + struct TALER_MERCHANT_TemplatesGetResponse tgr = { + .hr.http_status = (unsigned int) response_code, + .hr.reply = json }; tgh->job = NULL; @@ -146,10 +135,10 @@ handle_get_templates_finished (void *cls, { case MHD_HTTP_OK: { - json_t *templates; + const json_t *templates; struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_json ("templates", - &templates), + GNUNET_JSON_spec_array_const ("templates", + &templates), GNUNET_JSON_spec_end () }; @@ -158,48 +147,39 @@ handle_get_templates_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_templates (templates, + &tgr, + tgh)) { - if ( (! json_is_array (templates)) || - (GNUNET_OK == - parse_templates (templates, - tgh)) ) - { - GNUNET_JSON_parse_free (spec); - TALER_MERCHANT_templates_get_cancel (tgh); - return; - } - else - { - hr.http_status = 0; - hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; - } + TALER_MERCHANT_templates_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_templates_get_cancel (tgh); } diff --git a/src/testing/testing_api_cmd_get_templates.c b/src/testing/testing_api_cmd_get_templates.c index 3e9d59c6..bb0700a1 100644 --- a/src/testing/testing_api_cmd_get_templates.c +++ b/src/testing/testing_api_cmd_get_templates.c @@ -71,33 +71,29 @@ struct GetTemplatesState * Callback for a GET /templates operation. * * @param cls closure for this function - * @param hr HTTP response details - * @param templates_length length of the @a templates array - * @param templates array of templates the requested instance offers + * @param tgr response details */ static void get_templates_cb (void *cls, - const struct TALER_MERCHANT_HttpResponse *hr, - unsigned int templates_length, - const struct TALER_MERCHANT_TemplateEntry templates[]) + const struct TALER_MERCHANT_TemplatesGetResponse *tgr) { struct GetTemplatesState *gis = cls; gis->igh = NULL; - if (gis->http_status != hr->http_status) + if (gis->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 (gis->is)); TALER_TESTING_interpreter_fail (gis->is); return; } - switch (hr->http_status) + switch (tgr->hr.http_status) { case MHD_HTTP_OK: - if (templates_length != gis->templates_length) + if (tgr->details.ok.templates_length != gis->templates_length) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Length of templates found does not match\n"); @@ -117,14 +113,14 @@ get_templates_cb (void *cls, if (GNUNET_OK != TALER_TESTING_get_trait_template_id (template_cmd, - &template_id)) + &template_id)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not fetch template id\n"); TALER_TESTING_interpreter_fail (gis->is); return; } - if (0 != strcmp (templates[i].template_id, + if (0 != strcmp (tgr->details.ok.templates[i].template_id, *template_id)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -143,8 +139,9 @@ get_templates_cb (void *cls, default: GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unhandled HTTP status %u (%d).\n", - hr->http_status, - hr->ec); + tgr->hr.http_status, + tgr->hr.ec); + break; } TALER_TESTING_interpreter_next (gis->is); } @@ -160,16 +157,16 @@ get_templates_cb (void *cls, */ static void get_templates_run (void *cls, - const struct TALER_TESTING_Command *cmd, - struct TALER_TESTING_Interpreter *is) + const struct TALER_TESTING_Command *cmd, + struct TALER_TESTING_Interpreter *is) { struct GetTemplatesState *gis = cls; gis->is = is; gis->igh = TALER_MERCHANT_templates_get (is->ctx, - gis->merchant_url, - &get_templates_cb, - gis); + gis->merchant_url, + &get_templates_cb, + gis); GNUNET_assert (NULL != gis->igh); } @@ -183,7 +180,7 @@ get_templates_run (void *cls, */ static void get_templates_cleanup (void *cls, - const struct TALER_TESTING_Command *cmd) + const struct TALER_TESTING_Command *cmd) { struct GetTemplatesState *gis = cls; @@ -202,9 +199,9 @@ get_templates_cleanup (void *cls, struct TALER_TESTING_Command TALER_TESTING_cmd_merchant_get_templates (const char *label, - const char *merchant_url, - unsigned int http_status, - ...) + const char *merchant_url, + unsigned int http_status, + ...) { struct GetTemplatesState *gis; -- cgit v1.2.3