LCOV - code coverage report
Current view: top level - lib - exchange_api_management_set_wire_fee.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 77 58.4 %
Date: 2025-06-05 21:03:14 Functions: 3 3 100.0 %

          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_wire_fee.c
      19             :  * @brief functions to set wire fees at an exchange
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "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_exchange_service.h"
      28             : #include "taler_signatures.h"
      29             : #include "taler_curl_lib.h"
      30             : #include "taler_json_lib.h"
      31             : 
      32             : 
      33             : struct TALER_EXCHANGE_ManagementSetWireFeeHandle
      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_ManagementSetWireFeeCallback 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/wire 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          29 : handle_set_wire_fee_finished (void *cls,
      78             :                               long response_code,
      79             :                               const void *response)
      80             : {
      81          29 :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh = cls;
      82          29 :   const json_t *json = response;
      83          29 :   struct TALER_EXCHANGE_ManagementSetWireFeeResponse swr = {
      84          29 :     .hr.http_status = (unsigned int) response_code,
      85             :     .hr.reply = json
      86             :   };
      87             : 
      88          29 :   swfh->job = NULL;
      89          29 :   switch (response_code)
      90             :   {
      91          25 :   case MHD_HTTP_NO_CONTENT:
      92          25 :     break;
      93           2 :   case MHD_HTTP_FORBIDDEN:
      94           2 :     swr.hr.ec = TALER_JSON_get_error_code (json);
      95           2 :     swr.hr.hint = TALER_JSON_get_error_hint (json);
      96           2 :     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             :                 swfh->url);
     101           0 :     if (NULL != json)
     102             :     {
     103           0 :       swr.hr.ec = TALER_JSON_get_error_code (json);
     104           0 :       swr.hr.hint = TALER_JSON_get_error_hint (json);
     105             :     }
     106             :     else
     107             :     {
     108           0 :       swr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     109           0 :       swr.hr.hint = TALER_ErrorCode_get_hint (swr.hr.ec);
     110             :     }
     111           0 :     break;
     112           2 :   case MHD_HTTP_CONFLICT:
     113           2 :     swr.hr.ec = TALER_JSON_get_error_code (json);
     114           2 :     swr.hr.hint = TALER_JSON_get_error_hint (json);
     115           2 :     break;
     116           0 :   case MHD_HTTP_PRECONDITION_FAILED:
     117           0 :     swr.hr.ec = TALER_JSON_get_error_code (json);
     118           0 :     swr.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 :     swr.hr.ec = TALER_JSON_get_error_code (json);
     124           0 :     swr.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 wire fee\n",
     127             :                 (unsigned int) response_code,
     128             :                 (int) swr.hr.ec);
     129           0 :     break;
     130             :   }
     131          29 :   if (NULL != swfh->cb)
     132             :   {
     133          29 :     swfh->cb (swfh->cb_cls,
     134             :               &swr);
     135          29 :     swfh->cb = NULL;
     136             :   }
     137          29 :   TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
     138          29 : }
     139             : 
     140             : 
     141             : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *
     142          29 : TALER_EXCHANGE_management_set_wire_fees (
     143             :   struct GNUNET_CURL_Context *ctx,
     144             :   const char *exchange_base_url,
     145             :   const char *wire_method,
     146             :   struct GNUNET_TIME_Timestamp validity_start,
     147             :   struct GNUNET_TIME_Timestamp validity_end,
     148             :   const struct TALER_WireFeeSet *fees,
     149             :   const struct TALER_MasterSignatureP *master_sig,
     150             :   TALER_EXCHANGE_ManagementSetWireFeeCallback cb,
     151             :   void *cb_cls)
     152             : {
     153             :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh;
     154             :   CURL *eh;
     155             :   json_t *body;
     156             : 
     157          29 :   swfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetWireFeeHandle);
     158          29 :   swfh->cb = cb;
     159          29 :   swfh->cb_cls = cb_cls;
     160          29 :   swfh->ctx = ctx;
     161          29 :   swfh->url = TALER_url_join (exchange_base_url,
     162             :                               "management/wire-fee",
     163             :                               NULL);
     164          29 :   if (NULL == swfh->url)
     165             :   {
     166           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                 "Could not construct request URL.\n");
     168           0 :     GNUNET_free (swfh);
     169           0 :     return NULL;
     170             :   }
     171          29 :   body = GNUNET_JSON_PACK (
     172             :     GNUNET_JSON_pack_string ("wire_method",
     173             :                              wire_method),
     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 ("closing_fee",
     181             :                             &fees->closing),
     182             :     TALER_JSON_pack_amount ("wire_fee",
     183             :                             &fees->wire));
     184          29 :   eh = TALER_EXCHANGE_curl_easy_get_ (swfh->url);
     185          58 :   if ( (NULL == eh) ||
     186             :        (GNUNET_OK !=
     187          29 :         TALER_curl_easy_post (&swfh->post_ctx,
     188             :                               eh,
     189             :                               body)) )
     190             :   {
     191           0 :     GNUNET_break (0);
     192           0 :     if (NULL != eh)
     193           0 :       curl_easy_cleanup (eh);
     194           0 :     json_decref (body);
     195           0 :     GNUNET_free (swfh->url);
     196           0 :     GNUNET_free (swfh);
     197           0 :     return NULL;
     198             :   }
     199          29 :   json_decref (body);
     200          29 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     201             :               "Requesting URL '%s'\n",
     202             :               swfh->url);
     203          58 :   swfh->job = GNUNET_CURL_job_add2 (ctx,
     204             :                                     eh,
     205          29 :                                     swfh->post_ctx.headers,
     206             :                                     &handle_set_wire_fee_finished,
     207             :                                     swfh);
     208          29 :   if (NULL == swfh->job)
     209             :   {
     210           0 :     TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
     211           0 :     return NULL;
     212             :   }
     213          29 :   return swfh;
     214             : }
     215             : 
     216             : 
     217             : void
     218          29 : TALER_EXCHANGE_management_set_wire_fees_cancel (
     219             :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh)
     220             : {
     221          29 :   if (NULL != swfh->job)
     222             :   {
     223           0 :     GNUNET_CURL_job_cancel (swfh->job);
     224           0 :     swfh->job = NULL;
     225             :   }
     226          29 :   TALER_curl_easy_post_finished (&swfh->post_ctx);
     227          29 :   GNUNET_free (swfh->url);
     228          29 :   GNUNET_free (swfh);
     229          29 : }

Generated by: LCOV version 1.16