donau

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

commit 7975fa1d04ebae33572c2d1c8aecba0b5cd86877
parent 96acfb7bb41e3ee3c660e3228ab713fc3ddb79d8
Author: Matyja Lukas Adam <lukas.matyja@students.bfh.ch>
Date:   Tue,  5 Mar 2024 09:46:54 +0100

[lib] add charity delete

Diffstat:
Msrc/include/donau_service.h | 2+-
Msrc/lib/Makefile.am | 1+
Msrc/lib/donau_api_charity_delete.c | 194+++++++++++++++++++++++++++++++------------------------------------------------
Msrc/lib/donau_api_charity_post.c | 55+------------------------------------------------------
4 files changed, 79 insertions(+), 173 deletions(-)

diff --git a/src/include/donau_service.h b/src/include/donau_service.h @@ -1297,7 +1297,7 @@ DONAU_charity_delete ( * @param rgh the charity request handle */ void -DONAU_charity_post_delete ( +DONAU_charity_delete_cancel ( struct DONAU_CharityDeleteHandle *rgh); #endif diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am @@ -23,6 +23,7 @@ libdonau_la_SOURCES = \ donau_api_handle.c \ donau_api_charity_get.c \ donau_api_charity_post.c \ + donau_api_charity_delete.c \ donau_api_charities_get.c \ donau_api_curl_defaults.c donau_api_curl_defaults.h diff --git a/src/lib/donau_api_charity_delete.c b/src/lib/donau_api_charity_delete.c @@ -18,7 +18,7 @@ */ /** - * @file lib/donau_api_charity_get.c + * @file lib/donau_api_charity_delete.c * @brief Implementation of the "handle" component of the donau's HTTP API * @author Lukas Matyja */ @@ -32,7 +32,7 @@ /** * Handle for a GET /charities/$CHARITY_ID request. */ -struct DONAU_CharityGetHandle +struct DONAU_CharityDeleteHandle { /** * The url for the /charities/$CHARITY_ID request. @@ -47,7 +47,7 @@ struct DONAU_CharityGetHandle /** * Function to call with the result. */ - DONAU_GetCharityResponseCallback cb; + DONAU_DeleteCharityResponseCallback cb; /** * Charity id we are querying. @@ -59,59 +59,12 @@ struct DONAU_CharityGetHandle */ void *cb_cls; -}; - -/** - * Decode the JSON in @a resp_obj from the /charities/$ID response - * and store the data in the @a charity_data. - * - * @param[in] resp_obj JSON object to parse - * @param[out] charity_data where to store the results we decoded - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - * (malformed JSON) - */ -static enum GNUNET_GenericReturnValue -handle_charity_get_ok (const json_t *resp_obj, - struct DONAU_CharityGetHandle *cgh, - struct DONAU_GetCharityResponse *gcresp) -{ - struct DONAU_Charity *charity = &gcresp->details.ok.charity; - const char *name; - if (JSON_OBJECT != json_typeof (resp_obj)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_fixed_auto ("charity_pub", - &charity->charity_pub), - GNUNET_JSON_spec_string ("name", &name), - TALER_JSON_spec_amount_any ("max_per_year", - &charity->max_per_year), - TALER_JSON_spec_amount_any ("receipts_to_date", - &charity-> - receipts_to_date), - GNUNET_JSON_spec_uint64 ("current_year", - &charity->current_year), - GNUNET_JSON_spec_end () - }; - if (GNUNET_OK != - GNUNET_JSON_parse (resp_obj, - spec, - NULL, - NULL)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - charity->name = GNUNET_strdup (name); + /** + * Header. + */ + struct curl_slist *xhdr; - cgh->cb (cgh->cb_cls, - gcresp); - cgh->cb = NULL; - return GNUNET_OK; -} +}; /** @@ -123,136 +76,141 @@ handle_charity_get_ok (const json_t *resp_obj, * @param resp_obj parsed JSON result, NULL on error */ static void -handle_charity_get_finished (void *cls, +handle_charity_delete_finished (void *cls, long response_code, const void *resp_obj) { - // struct DONAU_Charity *cd = NULL; - - struct DONAU_CharityGetHandle *cgh = cls; + struct DONAU_CharityDeleteHandle *cdh = cls; const json_t *j = resp_obj; - struct DONAU_GetCharityResponse gcresp = { + struct DONAU_DeleteCharityResponse dcresp = { .hr.reply = j, .hr.http_status = (unsigned int) response_code }; - cgh->job = NULL; + cdh->job = NULL; switch (response_code) { case 0: - gcresp.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + dcresp.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; - case MHD_HTTP_OK: - if (GNUNET_OK != - handle_charity_get_ok (j, - cgh, - &gcresp)) - { - gcresp.hr.http_status = 0; - gcresp.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; - } + case MHD_HTTP_NO_CONTENT: break; case MHD_HTTP_BAD_REQUEST: - /* This should never happen, either us or the donau is buggy + /* This should never happen, either us or the exchange is buggy (or API version conflict); just pass JSON reply to the application */ - gcresp.hr.ec = TALER_JSON_get_error_code (j); - gcresp.hr.hint = TALER_JSON_get_error_hint (j); + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); + break; + case MHD_HTTP_FORBIDDEN: + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); + /* Nothing really to verify, exchange says one of the signatures is + invalid; as we checked them, this should never happen, we + should pass the JSON reply to the application */ break; case MHD_HTTP_NOT_FOUND: + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); /* Nothing really to verify, this should never happen, we should pass the JSON reply to the application */ - gcresp.hr.ec = TALER_JSON_get_error_code (j); - gcresp.hr.hint = TALER_JSON_get_error_hint (j); + break; + case MHD_HTTP_CONFLICT: + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); /* Server had an internal issue; we should retry, but this API leaves this to the application */ - gcresp.hr.ec = TALER_JSON_get_error_code (j); - gcresp.hr.hint = TALER_JSON_get_error_hint (j); break; default: /* unexpected response code */ GNUNET_break_op (0); - gcresp.hr.ec = TALER_JSON_get_error_code (j); - gcresp.hr.hint = TALER_JSON_get_error_hint (j); + dcresp.hr.ec = TALER_JSON_get_error_code (j); + dcresp.hr.hint = TALER_JSON_get_error_hint (j); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u/%d for GET %s\n", + "Unexpected response code %u/%d for DELETE %s\n", (unsigned int) response_code, - (int) gcresp.hr.ec, - cgh->url); + (int) dcresp.hr.ec, + cdh->url); break; } - if (NULL != cgh->cb) + if (NULL != cdh->cb) { - cgh->cb (cgh->cb_cls, - &gcresp); - cgh->cb = NULL; + cdh->cb (cdh->cb_cls, + &dcresp); + cdh->cb = NULL; } - DONAU_charity_get_cancel (cgh); + DONAU_charity_delete_cancel (cdh); } -struct DONAU_CharityGetHandle * -DONAU_charity_get ( +struct DONAU_CharityDeleteHandle * +DONAU_charity_delete ( struct GNUNET_CURL_Context *ctx, const char *url, const uint64_t id, - const struct DONAU_BearerToken bearer, // TODO: check authorization - DONAU_GetCharityResponseCallback cb, + const struct DONAU_BearerToken bearer, + DONAU_DeleteCharityResponseCallback cb, void *cb_cls) { - struct DONAU_CharityGetHandle *cgh; + struct DONAU_CharityDeleteHandle *cdh; CURL *eh; TALER_LOG_DEBUG ("Connecting to the donau (%s)\n", url); - cgh = GNUNET_new (struct DONAU_CharityGetHandle); - cgh->url = GNUNET_strdup (url); - cgh->cb = cb; - cgh->charity_id = id; - cgh->cb_cls = cb_cls; + cdh = GNUNET_new (struct DONAU_CharityDeleteHandle); + cdh->url = GNUNET_strdup (url); + cdh->cb = cb; + cdh->charity_id = id; + cdh->cb_cls = cb_cls; char arg_str[sizeof (id) * 2 + 32]; GNUNET_snprintf (arg_str, sizeof (arg_str), "charities/%llu", (unsigned long long) id); - cgh->url = TALER_url_join (url, + cdh->url = TALER_url_join (url, arg_str, NULL); - if (NULL == cgh->url) + if (NULL == cdh->url) { - GNUNET_free (cgh); + GNUNET_free (cdh); return NULL; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Requesting a charity with URL `%s'.\n", - cgh->url); - eh = DONAU_curl_easy_get_ (cgh->url); + eh = DONAU_curl_easy_get_ (cdh->url); if (NULL == eh) { GNUNET_break (0); - GNUNET_free (cgh->url); - GNUNET_free (cgh); + GNUNET_free (cdh->url); + GNUNET_free (cdh); return NULL; } - cgh->job = GNUNET_CURL_job_add_with_ct_json (ctx, + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting charity deletion with URL `%s'.\n", + cdh->url); + cdh->job = GNUNET_CURL_job_add_with_ct_json (ctx, eh, - &handle_charity_get_finished, - cgh); - return cgh; + &handle_charity_delete_finished, + cdh); + return cdh; } void -DONAU_charity_get_cancel ( - struct DONAU_CharityGetHandle *cgh) +DONAU_charity_delete_cancel ( + struct DONAU_CharityDeleteHandle *cdh) { - if (NULL != cgh->job) + if (NULL != cdh->job) { - GNUNET_CURL_job_cancel (cgh->job); - cgh->job = NULL; + GNUNET_CURL_job_cancel (cdh->job); + cdh->job = NULL; } - GNUNET_free (cgh->url); - GNUNET_free (cgh); + GNUNET_free (cdh->url); + GNUNET_free (cdh); } diff --git a/src/lib/donau_api_charity_post.c b/src/lib/donau_api_charity_post.c @@ -68,59 +68,6 @@ struct DONAU_CharityPostHandle }; /** - * Decode the JSON in @a resp_obj from the /charities/$ID response - * and store the data in the @a charity_data. - * - * @param[in] resp_obj JSON object to parse - * @param[out] charity_data where to store the results we decoded - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - * (malformed JSON) - */ -//static enum GNUNET_GenericReturnValue -//handle_charity_get_ok (const json_t *resp_obj, -// struct DONAU_CharityGetHandle *cgh, -// struct DONAU_GetCharityResponse *gcresp) -//{ -// struct DONAU_Charity *charity = &gcresp->details.ok.charity; -// const char *name; -// if (JSON_OBJECT != json_typeof (resp_obj)) -// { -// GNUNET_break_op (0); -// return GNUNET_SYSERR; -// } -// -// struct GNUNET_JSON_Specification spec[] = { -// GNUNET_JSON_spec_fixed_auto ("charity_pub", -// &charity->charity_pub), -// GNUNET_JSON_spec_string ("name", &name), -// TALER_JSON_spec_amount_any ("max_per_year", -// &charity->max_per_year), -// TALER_JSON_spec_amount_any ("receipts_to_date", -// &charity-> -// receipts_to_date), -// GNUNET_JSON_spec_uint64 ("current_year", -// &charity->current_year), -// GNUNET_JSON_spec_end () -// }; -// if (GNUNET_OK != -// GNUNET_JSON_parse (resp_obj, -// spec, -// NULL, -// NULL)) -// { -// GNUNET_break_op (0); -// return GNUNET_SYSERR; -// } -// charity->name = GNUNET_strdup (name); -// -// cgh->cb (cgh->cb_cls, -// gcresp); -// cgh->cb = NULL; -// return GNUNET_OK; -//} - - -/** * Function called when we're done processing the * HTTP POST /charities request. * @@ -183,7 +130,7 @@ struct DONAU_CharityPostHandle * DONAU_charity_post ( struct GNUNET_CURL_Context *ctx, const char *url, - struct DONAU_CharityRequest *charity_req, + struct DONAU_CharityRequest *charity_req, // make it const const struct DONAU_BearerToken bearer, DONAU_PostCharityResponseCallback cb, void *cb_cls)