LCOV - code coverage report
Current view: top level - lib - exchange_api_management_set_wire_fee.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 47 69 68.1 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020-2021 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 "taler_exchange_service.h"
      26             : #include "taler_signatures.h"
      27             : #include "taler_curl_lib.h"
      28             : #include "taler_json_lib.h"
      29             : 
      30             : 
      31             : struct TALER_EXCHANGE_ManagementSetWireFeeHandle
      32             : {
      33             : 
      34             :   /**
      35             :    * The url for this request.
      36             :    */
      37             :   char *url;
      38             : 
      39             :   /**
      40             :    * Minor context that holds body and headers.
      41             :    */
      42             :   struct TALER_CURL_PostContext post_ctx;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_EXCHANGE_ManagementSetWireFeeCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP /management/wire request.
      69             :  *
      70             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param response response body, NULL if not in JSON
      73             :  */
      74             : static void
      75          10 : handle_set_wire_fee_finished (void *cls,
      76             :                               long response_code,
      77             :                               const void *response)
      78             : {
      79          10 :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh = cls;
      80          10 :   const json_t *json = response;
      81          10 :   struct TALER_EXCHANGE_HttpResponse hr = {
      82          10 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86          10 :   swfh->job = NULL;
      87          10 :   switch (response_code)
      88             :   {
      89           8 :   case MHD_HTTP_NO_CONTENT:
      90           8 :     break;
      91           1 :   case MHD_HTTP_FORBIDDEN:
      92           1 :     hr.ec = TALER_JSON_get_error_code (json);
      93           1 :     hr.hint = TALER_JSON_get_error_hint (json);
      94           1 :     break;
      95           1 :   case MHD_HTTP_CONFLICT:
      96           1 :     hr.ec = TALER_JSON_get_error_code (json);
      97           1 :     hr.hint = TALER_JSON_get_error_hint (json);
      98           1 :     break;
      99           0 :   case MHD_HTTP_PRECONDITION_FAILED:
     100           0 :     hr.ec = TALER_JSON_get_error_code (json);
     101           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     102           0 :     break;
     103           0 :   default:
     104             :     /* unexpected response code */
     105           0 :     GNUNET_break_op (0);
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     109             :                 "Unexpected response code %u/%d for exchange management set wire fee\n",
     110             :                 (unsigned int) response_code,
     111             :                 (int) hr.ec);
     112           0 :     break;
     113             :   }
     114          10 :   if (NULL != swfh->cb)
     115             :   {
     116          10 :     swfh->cb (swfh->cb_cls,
     117             :               &hr);
     118          10 :     swfh->cb = NULL;
     119             :   }
     120          10 :   TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
     121          10 : }
     122             : 
     123             : 
     124             : struct TALER_EXCHANGE_ManagementSetWireFeeHandle *
     125          10 : TALER_EXCHANGE_management_set_wire_fees (
     126             :   struct GNUNET_CURL_Context *ctx,
     127             :   const char *exchange_base_url,
     128             :   const char *wire_method,
     129             :   struct GNUNET_TIME_Absolute validity_start,
     130             :   struct GNUNET_TIME_Absolute validity_end,
     131             :   const struct TALER_Amount *wire_fee,
     132             :   const struct TALER_Amount *closing_fee,
     133             :   const struct TALER_MasterSignatureP *master_sig,
     134             :   TALER_EXCHANGE_ManagementWireEnableCallback cb,
     135             :   void *cb_cls)
     136             : {
     137             :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh;
     138             :   CURL *eh;
     139             :   json_t *body;
     140             : 
     141          10 :   swfh = GNUNET_new (struct TALER_EXCHANGE_ManagementSetWireFeeHandle);
     142          10 :   swfh->cb = cb;
     143          10 :   swfh->cb_cls = cb_cls;
     144          10 :   swfh->ctx = ctx;
     145          10 :   swfh->url = TALER_url_join (exchange_base_url,
     146             :                               "management/wire-fee",
     147             :                               NULL);
     148          10 :   if (NULL == swfh->url)
     149             :   {
     150           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     151             :                 "Could not construct request URL.\n");
     152           0 :     GNUNET_free (swfh);
     153           0 :     return NULL;
     154             :   }
     155          10 :   body = GNUNET_JSON_PACK (
     156             :     GNUNET_JSON_pack_string ("wire_method",
     157             :                              wire_method),
     158             :     GNUNET_JSON_pack_data_auto ("master_sig",
     159             :                                 master_sig),
     160             :     GNUNET_JSON_pack_time_abs ("fee_start",
     161             :                                validity_start),
     162             :     GNUNET_JSON_pack_time_abs ("fee_end",
     163             :                                validity_end),
     164             :     TALER_JSON_pack_amount ("closing_fee",
     165             :                             closing_fee),
     166             :     TALER_JSON_pack_amount ("wire_fee",
     167             :                             wire_fee));
     168          10 :   eh = curl_easy_init ();
     169          10 :   GNUNET_assert (NULL != eh);
     170          10 :   if (GNUNET_OK !=
     171          10 :       TALER_curl_easy_post (&swfh->post_ctx,
     172             :                             eh,
     173             :                             body))
     174             :   {
     175           0 :     GNUNET_break (0);
     176           0 :     json_decref (body);
     177           0 :     GNUNET_free (swfh->url);
     178           0 :     GNUNET_free (eh);
     179           0 :     return NULL;
     180             :   }
     181          10 :   json_decref (body);
     182          10 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     183             :               "Requesting URL '%s'\n",
     184             :               swfh->url);
     185          10 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     186             :                                                CURLOPT_URL,
     187             :                                                swfh->url));
     188          20 :   swfh->job = GNUNET_CURL_job_add2 (ctx,
     189             :                                     eh,
     190          10 :                                     swfh->post_ctx.headers,
     191             :                                     &handle_set_wire_fee_finished,
     192             :                                     swfh);
     193          10 :   if (NULL == swfh->job)
     194             :   {
     195           0 :     TALER_EXCHANGE_management_set_wire_fees_cancel (swfh);
     196           0 :     return NULL;
     197             :   }
     198          10 :   return swfh;
     199             : }
     200             : 
     201             : 
     202             : void
     203          10 : TALER_EXCHANGE_management_set_wire_fees_cancel (
     204             :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *swfh)
     205             : {
     206          10 :   if (NULL != swfh->job)
     207             :   {
     208           0 :     GNUNET_CURL_job_cancel (swfh->job);
     209           0 :     swfh->job = NULL;
     210             :   }
     211          10 :   TALER_curl_easy_post_finished (&swfh->post_ctx);
     212          10 :   GNUNET_free (swfh->url);
     213          10 :   GNUNET_free (swfh);
     214          10 : }

Generated by: LCOV version 1.14