donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

commit dac2dee0d9e6edaad5f50b436d82c04846c17b56
parent 454a3fc3011b9674f1d1817d8eac9b2b3b534ad3
Author: Matyja Lukas Adam <lukas.matyja@students.bfh.ch>
Date:   Wed, 28 Feb 2024 10:14:11 +0100

[lib] charities get

Diffstat:
Msrc/include/donau_service.h | 10+++-------
Msrc/lib/Makefile.am | 1+
Msrc/lib/donau_api_charities_get.c | 192++++++++++++++++++++++++++++++++-----------------------------------------------
Msrc/lib/donau_api_charity_get.c | 4++--
4 files changed, 84 insertions(+), 123 deletions(-)

diff --git a/src/include/donau_service.h b/src/include/donau_service.h @@ -785,7 +785,7 @@ struct DONAU_CharitySummary /** * charity id */ - unsigned long long charity_id; + uint64_t charity_id; /** * charity name @@ -838,12 +838,7 @@ struct DONAU_GetCharitiesResponse /** * Charity status information. */ - struct DONAU_CharitySummary charity; - - /** - * Number of charities - */ - uint64_t num_charity; + struct DONAU_CharitySummary *charity; } ok; @@ -886,6 +881,7 @@ DONAU_charities_get ( struct GNUNET_CURL_Context *ctx, const char *url, const struct DONAU_BearerToken bearer, + const struct GNUNET_TIME_Relative timeout, DONAU_GetCharitiesResponseCallback cb, void *cb_cls); diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am @@ -22,6 +22,7 @@ libdonau_la_LDFLAGS = \ libdonau_la_SOURCES = \ donau_api_handle.c \ donau_api_charity_get.c \ + donau_api_charities_get.c \ donau_api_curl_defaults.c donau_api_curl_defaults.h ## maybe need libtalercurl diff --git a/src/lib/donau_api_charities_get.c b/src/lib/donau_api_charities_get.c @@ -30,12 +30,12 @@ /** - * Handle for a GET /charities/$CHARITY_ID request. + * Handle for a GET /charities request. */ struct DONAU_CharitiesGetHandle { /** - * The url for the /charities/$CHARITY_ID request. + * The url for the /charities request. */ char *url; @@ -50,11 +50,6 @@ struct DONAU_CharitiesGetHandle DONAU_GetCharitiesResponseCallback cb; /** - * Charity id we are querying. - */ - unsigned long long charity_id; - - /** * Closure to pass to @e cb. */ void *cb_cls; @@ -62,96 +57,70 @@ struct DONAU_CharitiesGetHandle }; /** - * Decode the JSON in @a resp_obj from the /charities/$ID response - * and store the data in the @a charity_data. + * Decode the JSON in @a resp_obj from the /charities response + * and store the data in the @a charities_data. * * @param[in] resp_obj JSON object to parse - * @param[out] charity_data where to store the results we decoded + * @param[in] cgh contains the callback function + * @param[out] gcresp where to store the results we decoded * @return #GNUNET_OK on success, #GNUNET_SYSERR on error * (malformed JSON) */ static enum GNUNET_GenericReturnValue handle_charities_get_ok (const json_t *resp_obj, - struct DONAU_CharityGetHandle *cgh) + struct DONAU_CharitiesGetHandle *cgh, + struct DONAU_GetCharitiesResponse *gcresp) { - const json_t *charity_hist_array; + //const uint64_t num_charity = gcresp->details.ok.num_charity; TODO: Delete number of charities in service header + struct DONAU_CharitySummary *charities = gcresp->details.ok.charity; const char *name; if (JSON_OBJECT != json_typeof (resp_obj)) { GNUNET_break_op (0); return GNUNET_SYSERR; } - struct DONAU_GetCharityResponse charity_resp = { - .hr.reply = resp_obj, - .hr.http_status = MHD_HTTP_OK - }; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_string ("name", - &name), - GNUNET_JSON_spec_fixed_auto ("charity_pub", - &charity_resp.details.ok.charity->charity_pub), - TALER_JSON_spec_amount_any ("max_per_year", - &charity_resp.details.ok.charity->max_per_year), - TALER_JSON_spec_amount_any ("receipts_to_date", - &charty_resp.details.ok.charity-> - receipts_to_date), - GNUNET_JSON_spec_uint32 ("current_year", - &charity_resp.details.ok.charity->current_year), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (resp_obj, - spec, - NULL, - NULL)) + const unsigned long long num_charity + = json_array_size (resp_obj); + if (0 != num_charity) { - GNUNET_break_op (0); - return GNUNET_SYSERR; + unsigned int index; + json_t *charity_obj; + json_array_foreach (resp_obj, + index, + charity_obj) + { + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("name", &name), + TALER_JSON_spec_amount_any ("max_per_year", + &charities[index].max_per_year), + TALER_JSON_spec_amount_any ("receipts_to_date", + &charities[index]. + receipts_to_date), + GNUNET_JSON_spec_uint64 ("charity_id", + &charities[index].charity_id), + GNUNET_JSON_spec_end () + }; + if (GNUNET_OK != + GNUNET_JSON_parse (resp_obj, + spec, + NULL, + NULL)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + charities[index].name = GNUNET_strdup (name); + } } - charity_resp.details.ok.charity->name = GNUNET_strdup (name); - - // /* parse the charity history data */ - // charity_resp.details.ok.charity->num_hist - // = json_array_size (charity_hist_array); - // if (0 != charity_resp.details.ok.charity->num_hist) - // { - // json_t *charity_history_obj; - // unsigned int index; - - // charity_resp.details.ok.charity->donation_history - // = GNUNET_new_array (charity_resp.details.ok.charity->num_hist, - // struct DONAU_CharityHistoryYear); - // json_array_foreach (charity_hist_array, index, charity_history_obj) { - // struct DONAU_CharityHistoryYear *donation_history = &charity_resp.details.ok.charity->donation_history[index]; - // struct GNUNET_JSON_Specification history_spec[] = { - // TALER_JSON_spec_amount_any ("final_amount", - // &donation_history->final_amount), - // GNUNET_JSON_spec_uint32 ("year", - // &donation_history->year), - // GNUNET_JSON_spec_end () - // }; - - // if (GNUNET_OK != - // GNUNET_JSON_parse (charity_history_obj, - // history_spec, - // NULL, NULL)) - // { - // GNUNET_break_op (0); - // return GNUNET_SYSERR; - // } - // } - // } - cgh->cb (cgh->cb_cls, - &charity_resp); + gcresp); cgh->cb = NULL; return GNUNET_OK; } /** - * Callback used when downloading the reply to a /charity request + * Callback used when downloading the reply to a /charities request * is complete. * * @param cls the `struct KeysRequest` @@ -160,14 +129,14 @@ handle_charities_get_ok (const json_t *resp_obj, */ static void handle_charities_get_finished (void *cls, - long response_code, - const void *resp_obj) + long response_code, + const void *resp_obj) { - // struct DONAU_Charity *cd = NULL; + // struct DONAU_Charities *cd = NULL; - struct DONAU_CharityGetHandle *cgh = cls; + struct DONAU_CharitiesGetHandle *cgh = cls; const json_t *j = resp_obj; - struct DONAU_GetCharityResponse gcresp = { + struct DONAU_GetCharitiesResponse gcresp = { .hr.reply = j, .hr.http_status = (unsigned int) response_code }; @@ -180,8 +149,9 @@ handle_charities_get_finished (void *cls, break; case MHD_HTTP_OK: if (GNUNET_OK != - handle_charity_get_ok (j, - cgh)) + handle_charities_get_ok (j, + cgh, + &gcresp)) { gcresp.hr.http_status = 0; gcresp.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; @@ -223,50 +193,38 @@ handle_charities_get_finished (void *cls, &gcresp); cgh->cb = NULL; } - DONAU_charity_get_cancel (cgh); + DONAU_charities_get_cancel (cgh); } -struct DONAU_CharityGetHandle * +struct DONAU_CharitiesGetHandle * DONAU_charities_get ( struct GNUNET_CURL_Context *ctx, const char *url, - const uint64_t id, const struct DONAU_BearerToken bearer, // TODO: check authorization struct GNUNET_TIME_Relative timeout, - DONAU_GetCharityResponseCallback cb, + DONAU_GetCharitiesResponseCallback cb, void *cb_cls) { - struct DONAU_CharityGetHandle *cgh; + struct DONAU_CharitiesGetHandle *cgh; CURL *eh; TALER_LOG_DEBUG ("Connecting to the donau (%s)\n", url); - cgh = GNUNET_new (struct DONAU_CharityGetHandle); + cgh = GNUNET_new (struct DONAU_CharitiesGetHandle); cgh->url = GNUNET_strdup (url); cgh->cb = cb; - cgh->charity_id = id; cgh->cb_cls = cb_cls; - char arg_str[sizeof (id) * 2 + 32]; - char id_str[sizeof (id) * 2]; - char *end; - char timeout_str[32]; - - end = GNUNET_STRINGS_data_to_string (&id, - sizeof (id), - id_str, - sizeof (id_str)); - *end = '\0'; - GNUNET_snprintf (timeout_str, - sizeof (timeout_str), - "%llu", - (unsigned long long) - (timeout.rel_value_us - / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us)); - GNUNET_snprintf (arg_str, - sizeof (arg_str), - "charities/%s", - id_str); + char *arg_str; + unsigned long long tms + = timeout.rel_value_us + / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; + if (tms == 0) + arg_str = "charities"; + else + GNUNET_asprintf (&arg_str, + "charities?timeout_ms=%llu", + tms); cgh->url = TALER_url_join (url, arg_str, NULL); @@ -276,7 +234,7 @@ DONAU_charities_get ( return NULL; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Requesting a charity with URL `%s'.\n", + "Requesting all charities with URL `%s'.\n", cgh->url); eh = DONAU_curl_easy_get_ (cgh->url); if (NULL == eh) @@ -286,9 +244,16 @@ DONAU_charities_get ( GNUNET_free (cgh); return NULL; } - cgh->job = GNUNET_CURL_job_add (ctx, + if (0 != tms) + { + GNUNET_break (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_TIMEOUT_MS, + (long) (tms + 100L))); + } + cgh->job = GNUNET_CURL_job_add_with_ct_json (ctx, eh, - &handle_charity_get_finished, + &handle_charities_get_finished, cgh); return cgh; } @@ -296,7 +261,7 @@ DONAU_charities_get ( void DONAU_charities_get_cancel ( - struct DONAU_CharityGetHandle *cgh) + struct DONAU_CharitiesGetHandle *cgh) { if (NULL != cgh->job) { @@ -305,4 +270,4 @@ DONAU_charities_get_cancel ( } GNUNET_free (cgh->url); GNUNET_free (cgh); -} -\ No newline at end of file +} diff --git a/src/lib/donau_api_charity_get.c b/src/lib/donau_api_charity_get.c @@ -230,10 +230,10 @@ DONAU_charity_get ( else GNUNET_snprintf (arg_str, sizeof (arg_str), - "reserves/%llu?timeout_ms=%s", + "charities/%llu?timeout_ms=%s", (unsigned long long) id, - timeout_str); + timeout_str); //TODO: query by year cgh->url = TALER_url_join (url, arg_str, NULL);