LCOV - code coverage report
Current view: top level - lib - exchange_api_management_set_global_fee.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 48.1 % 77 37
Test Date: 2025-12-28 14:06:02 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020-2022 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it under the
       6              :   terms of the GNU General Public License as published by the Free Software
       7              :   Foundation; either version 3, or (at your option) any later version.
       8              : 
       9              :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10              :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11              :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12              : 
      13              :   You should have received a copy of the GNU General Public License along with
      14              :   TALER; see the file COPYING.  If not, see
      15              :   <http://www.gnu.org/licenses/>
      16              : */
      17              : /**
      18              :  * @file lib/exchange_api_management_set_global_fee.c
      19              :  * @brief functions to set global fees at an exchange
      20              :  * @author Christian Grothoff
      21              :  */
      22              : #include "taler/platform.h"
      23              : #include "taler/taler_json_lib.h"
      24              : #include <gnunet/gnunet_curl_lib.h>
      25              : #include <microhttpd.h>
      26              : #include "exchange_api_curl_defaults.h"
      27              : #include "taler/taler_exchange_service.h"
      28              : #include "taler/taler_signatures.h"
      29              : #include "taler/taler_curl_lib.h"
      30              : #include "taler/taler_json_lib.h"
      31              : 
      32              : 
      33              : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle
      34              : {
      35              : 
      36              :   /**
      37              :    * The url for this request.
      38              :    */
      39              :   char *url;
      40              : 
      41              :   /**
      42              :    * Minor context that holds body and headers.
      43              :    */
      44              :   struct TALER_CURL_PostContext post_ctx;
      45              : 
      46              :   /**
      47              :    * Handle for the request.
      48              :    */
      49              :   struct GNUNET_CURL_Job *job;
      50              : 
      51              :   /**
      52              :    * Function to call with the result.
      53              :    */
      54              :   TALER_EXCHANGE_ManagementSetGlobalFeeCallback cb;
      55              : 
      56              :   /**
      57              :    * Closure for @a cb.
      58              :    */
      59              :   void *cb_cls;
      60              : 
      61              :   /**
      62              :    * Reference to the execution context.
      63              :    */
      64              :   struct GNUNET_CURL_Context *ctx;
      65              : };
      66              : 
      67              : 
      68              : /**
      69              :  * Function called when we're done processing the
      70              :  * HTTP /management/global request.
      71              :  *
      72              :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
      73              :  * @param response_code HTTP response code, 0 on error
      74              :  * @param response response body, NULL if not in JSON
      75              :  */
      76              : static void
      77           17 : handle_set_global_fee_finished (void *cls,
      78              :                                 long response_code,
      79              :                                 const void *response)
      80              : {
      81           17 :   struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh = cls;
      82           17 :   const json_t *json = response;
      83           17 :   struct TALER_EXCHANGE_ManagementSetGlobalFeeResponse sfr = {
      84           17 :     .hr.http_status = (unsigned int) response_code,
      85              :     .hr.reply = json
      86              :   };
      87              : 
      88           17 :   sgfh->job = NULL;
      89           17 :   switch (response_code)
      90              :   {
      91           17 :   case MHD_HTTP_NO_CONTENT:
      92           17 :     break;
      93            0 :   case MHD_HTTP_FORBIDDEN:
      94            0 :     sfr.hr.ec = TALER_JSON_get_error_code (json);
      95            0 :     sfr.hr.hint = TALER_JSON_get_error_hint (json);
      96            0 :     break;
      97            0 :   case MHD_HTTP_NOT_FOUND:
      98            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      99              :                 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
     100              :                 sgfh->url);
     101            0 :     if (NULL != json)
     102              :     {
     103            0 :       sfr.hr.ec = TALER_JSON_get_error_code (json);
     104            0 :       sfr.hr.hint = TALER_JSON_get_error_hint (json);
     105              :     }
     106              :     else
     107              :     {
     108            0 :       sfr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     109            0 :       sfr.hr.hint = TALER_ErrorCode_get_hint (sfr.hr.ec);
     110              :     }
     111            0 :     break;
     112            0 :   case MHD_HTTP_CONFLICT:
     113            0 :     sfr.hr.ec = TALER_JSON_get_error_code (json);
     114            0 :     sfr.hr.hint = TALER_JSON_get_error_hint (json);
     115            0 :     break;
     116            0 :   case MHD_HTTP_PRECONDITION_FAILED:
     117            0 :     sfr.hr.ec = TALER_JSON_get_error_code (json);
     118            0 :     sfr.hr.hint = TALER_JSON_get_error_hint (json);
     119            0 :     break;
     120            0 :   default:
     121              :     /* unexpected response code */
     122            0 :     GNUNET_break_op (0);
     123            0 :     sfr.hr.ec = TALER_JSON_get_error_code (json);
     124            0 :     sfr.hr.hint = TALER_JSON_get_error_hint (json);
     125            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     126              :                 "Unexpected response code %u/%d for exchange management set global fee\n",
     127              :                 (unsigned int) response_code,
     128              :                 (int) sfr.hr.ec);
     129            0 :     break;
     130              :   }
     131           17 :   if (NULL != sgfh->cb)
     132              :   {
     133           17 :     sgfh->cb (sgfh->cb_cls,
     134              :               &sfr);
     135           17 :     sgfh->cb = NULL;
     136              :   }
     137           17 :   TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
     138           17 : }
     139              : 
     140              : 
     141              : struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *
     142           17 : TALER_EXCHANGE_management_set_global_fees (
     143              :   struct GNUNET_CURL_Context *ctx,
     144              :   const char *exchange_base_url,
     145              :   struct GNUNET_TIME_Timestamp validity_start,
     146              :   struct GNUNET_TIME_Timestamp validity_end,
     147              :   const struct TALER_GlobalFeeSet *fees,
     148              :   struct GNUNET_TIME_Relative purse_timeout,
     149              :   struct GNUNET_TIME_Relative history_expiration,
     150              :   uint32_t purse_account_limit,
     151              :   const struct TALER_MasterSignatureP *master_sig,
     152              :   TALER_EXCHANGE_ManagementSetGlobalFeeCallback cb,
     153              :   void *cb_cls)
     154              : {
     155              :   struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh;
     156              :   CURL *eh;
     157              :   json_t *body;
     158              : 
     159           17 :   sgfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle);
     160           17 :   sgfh->cb = cb;
     161           17 :   sgfh->cb_cls = cb_cls;
     162           17 :   sgfh->ctx = ctx;
     163           17 :   sgfh->url = TALER_url_join (exchange_base_url,
     164              :                               "management/global-fee",
     165              :                               NULL);
     166           17 :   if (NULL == sgfh->url)
     167              :   {
     168            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     169              :                 "Could not construct request URL.\n");
     170            0 :     GNUNET_free (sgfh);
     171            0 :     return NULL;
     172              :   }
     173           17 :   body = GNUNET_JSON_PACK (
     174              :     GNUNET_JSON_pack_data_auto ("master_sig",
     175              :                                 master_sig),
     176              :     GNUNET_JSON_pack_timestamp ("fee_start",
     177              :                                 validity_start),
     178              :     GNUNET_JSON_pack_timestamp ("fee_end",
     179              :                                 validity_end),
     180              :     TALER_JSON_pack_amount ("history_fee",
     181              :                             &fees->history),
     182              :     TALER_JSON_pack_amount ("account_fee",
     183              :                             &fees->account),
     184              :     TALER_JSON_pack_amount ("purse_fee",
     185              :                             &fees->purse),
     186              :     GNUNET_JSON_pack_time_rel ("purse_timeout",
     187              :                                purse_timeout),
     188              :     GNUNET_JSON_pack_time_rel ("history_expiration",
     189              :                                history_expiration),
     190              :     GNUNET_JSON_pack_uint64 ("purse_account_limit",
     191              :                              purse_account_limit));
     192           17 :   eh = TALER_EXCHANGE_curl_easy_get_ (sgfh->url);
     193           34 :   if ( (NULL == eh) ||
     194              :        (GNUNET_OK !=
     195           17 :         TALER_curl_easy_post (&sgfh->post_ctx,
     196              :                               eh,
     197              :                               body)) )
     198              :   {
     199            0 :     GNUNET_break (0);
     200            0 :     if (NULL != eh)
     201            0 :       curl_easy_cleanup (eh);
     202            0 :     json_decref (body);
     203            0 :     GNUNET_free (sgfh->url);
     204            0 :     GNUNET_free (sgfh);
     205            0 :     return NULL;
     206              :   }
     207           17 :   json_decref (body);
     208           17 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     209              :               "Requesting URL '%s'\n",
     210              :               sgfh->url);
     211           34 :   sgfh->job = GNUNET_CURL_job_add2 (ctx,
     212              :                                     eh,
     213           17 :                                     sgfh->post_ctx.headers,
     214              :                                     &handle_set_global_fee_finished,
     215              :                                     sgfh);
     216           17 :   if (NULL == sgfh->job)
     217              :   {
     218            0 :     TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
     219            0 :     return NULL;
     220              :   }
     221           17 :   return sgfh;
     222              : }
     223              : 
     224              : 
     225              : void
     226           17 : TALER_EXCHANGE_management_set_global_fees_cancel (
     227              :   struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh)
     228              : {
     229           17 :   if (NULL != sgfh->job)
     230              :   {
     231            0 :     GNUNET_CURL_job_cancel (sgfh->job);
     232            0 :     sgfh->job = NULL;
     233              :   }
     234           17 :   TALER_curl_easy_post_finished (&sgfh->post_ctx);
     235           17 :   GNUNET_free (sgfh->url);
     236           17 :   GNUNET_free (sgfh);
     237           17 : }
        

Generated by: LCOV version 2.0-1