exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_management_set_global_fee.c (7099B)


      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 handle_set_global_fee_finished (void *cls,
     78                                 long response_code,
     79                                 const void *response)
     80 {
     81   struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh = cls;
     82   const json_t *json = response;
     83   struct TALER_EXCHANGE_ManagementSetGlobalFeeResponse sfr = {
     84     .hr.http_status = (unsigned int) response_code,
     85     .hr.reply = json
     86   };
     87 
     88   sgfh->job = NULL;
     89   switch (response_code)
     90   {
     91   case MHD_HTTP_NO_CONTENT:
     92     break;
     93   case MHD_HTTP_FORBIDDEN:
     94     sfr.hr.ec = TALER_JSON_get_error_code (json);
     95     sfr.hr.hint = TALER_JSON_get_error_hint (json);
     96     break;
     97   case MHD_HTTP_NOT_FOUND:
     98     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     if (NULL != json)
    102     {
    103       sfr.hr.ec = TALER_JSON_get_error_code (json);
    104       sfr.hr.hint = TALER_JSON_get_error_hint (json);
    105     }
    106     else
    107     {
    108       sfr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    109       sfr.hr.hint = TALER_ErrorCode_get_hint (sfr.hr.ec);
    110     }
    111     break;
    112   case MHD_HTTP_CONFLICT:
    113     sfr.hr.ec = TALER_JSON_get_error_code (json);
    114     sfr.hr.hint = TALER_JSON_get_error_hint (json);
    115     break;
    116   case MHD_HTTP_PRECONDITION_FAILED:
    117     sfr.hr.ec = TALER_JSON_get_error_code (json);
    118     sfr.hr.hint = TALER_JSON_get_error_hint (json);
    119     break;
    120   default:
    121     /* unexpected response code */
    122     GNUNET_break_op (0);
    123     sfr.hr.ec = TALER_JSON_get_error_code (json);
    124     sfr.hr.hint = TALER_JSON_get_error_hint (json);
    125     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     break;
    130   }
    131   if (NULL != sgfh->cb)
    132   {
    133     sgfh->cb (sgfh->cb_cls,
    134               &sfr);
    135     sgfh->cb = NULL;
    136   }
    137   TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
    138 }
    139 
    140 
    141 struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *
    142 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   sgfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle);
    160   sgfh->cb = cb;
    161   sgfh->cb_cls = cb_cls;
    162   sgfh->ctx = ctx;
    163   sgfh->url = TALER_url_join (exchange_base_url,
    164                               "management/global-fee",
    165                               NULL);
    166   if (NULL == sgfh->url)
    167   {
    168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    169                 "Could not construct request URL.\n");
    170     GNUNET_free (sgfh);
    171     return NULL;
    172   }
    173   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   eh = TALER_EXCHANGE_curl_easy_get_ (sgfh->url);
    193   if ( (NULL == eh) ||
    194        (GNUNET_OK !=
    195         TALER_curl_easy_post (&sgfh->post_ctx,
    196                               eh,
    197                               body)) )
    198   {
    199     GNUNET_break (0);
    200     if (NULL != eh)
    201       curl_easy_cleanup (eh);
    202     json_decref (body);
    203     GNUNET_free (sgfh->url);
    204     GNUNET_free (sgfh);
    205     return NULL;
    206   }
    207   json_decref (body);
    208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    209               "Requesting URL '%s'\n",
    210               sgfh->url);
    211   sgfh->job = GNUNET_CURL_job_add2 (ctx,
    212                                     eh,
    213                                     sgfh->post_ctx.headers,
    214                                     &handle_set_global_fee_finished,
    215                                     sgfh);
    216   if (NULL == sgfh->job)
    217   {
    218     TALER_EXCHANGE_management_set_global_fees_cancel (sgfh);
    219     return NULL;
    220   }
    221   return sgfh;
    222 }
    223 
    224 
    225 void
    226 TALER_EXCHANGE_management_set_global_fees_cancel (
    227   struct TALER_EXCHANGE_ManagementSetGlobalFeeHandle *sgfh)
    228 {
    229   if (NULL != sgfh->job)
    230   {
    231     GNUNET_CURL_job_cancel (sgfh->job);
    232     sgfh->job = NULL;
    233   }
    234   TALER_curl_easy_post_finished (&sgfh->post_ctx);
    235   GNUNET_free (sgfh->url);
    236   GNUNET_free (sgfh);
    237 }