LCOV - code coverage report
Current view: top level - lib - exchange_api_management_wire_disable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 47 73 64.4 %
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) 2015-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_wire_disable.c
      19             :  * @brief functions to disable an exchange wire method / bank account
      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_ManagementWireDisableHandle
      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_ManagementWireDisableCallback 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/disable request.
      69             :  *
      70             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorDisableHandle *`
      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           3 : handle_auditor_disable_finished (void *cls,
      76             :                                  long response_code,
      77             :                                  const void *response)
      78             : {
      79           3 :   struct TALER_EXCHANGE_ManagementWireDisableHandle *wh = cls;
      80           3 :   const json_t *json = response;
      81           3 :   struct TALER_EXCHANGE_HttpResponse hr = {
      82           3 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           3 :   wh->job = NULL;
      87           3 :   switch (response_code)
      88             :   {
      89           0 :   case 0:
      90             :     /* no reply */
      91           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      92           0 :     hr.hint = "server offline?";
      93           0 :     break;
      94           1 :   case MHD_HTTP_NO_CONTENT:
      95           1 :     break;
      96           1 :   case MHD_HTTP_FORBIDDEN:
      97           1 :     hr.ec = TALER_JSON_get_error_code (json);
      98           1 :     hr.hint = TALER_JSON_get_error_hint (json);
      99           1 :     break;
     100           1 :   case MHD_HTTP_NOT_FOUND:
     101           1 :     hr.ec = TALER_JSON_get_error_code (json);
     102           1 :     hr.hint = TALER_JSON_get_error_hint (json);
     103           1 :     break;
     104           0 :   case MHD_HTTP_CONFLICT:
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107           0 :     break;
     108           0 :   default:
     109             :     /* unexpected response code */
     110           0 :     GNUNET_break_op (0);
     111           0 :     hr.ec = TALER_JSON_get_error_code (json);
     112           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     113           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     114             :                 "Unexpected response code %u/%d exchange management disable wire\n",
     115             :                 (unsigned int) response_code,
     116             :                 (int) hr.ec);
     117           0 :     break;
     118             :   }
     119           3 :   if (NULL != wh->cb)
     120             :   {
     121           3 :     wh->cb (wh->cb_cls,
     122             :             &hr);
     123           3 :     wh->cb = NULL;
     124             :   }
     125           3 :   TALER_EXCHANGE_management_disable_wire_cancel (wh);
     126           3 : }
     127             : 
     128             : 
     129             : struct TALER_EXCHANGE_ManagementWireDisableHandle *
     130           3 : TALER_EXCHANGE_management_disable_wire (
     131             :   struct GNUNET_CURL_Context *ctx,
     132             :   const char *url,
     133             :   const char *payto_uri,
     134             :   struct GNUNET_TIME_Absolute validity_end,
     135             :   const struct TALER_MasterSignatureP *master_sig,
     136             :   TALER_EXCHANGE_ManagementWireDisableCallback cb,
     137             :   void *cb_cls)
     138             : {
     139             :   struct TALER_EXCHANGE_ManagementWireDisableHandle *wh;
     140             :   CURL *eh;
     141             :   json_t *body;
     142             : 
     143           3 :   wh = GNUNET_new (struct TALER_EXCHANGE_ManagementWireDisableHandle);
     144           3 :   wh->cb = cb;
     145           3 :   wh->cb_cls = cb_cls;
     146           3 :   wh->ctx = ctx;
     147           3 :   wh->url = TALER_url_join (url,
     148             :                             "management/wire/disable",
     149             :                             NULL);
     150           3 :   if (NULL == wh->url)
     151             :   {
     152           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     153             :                 "Could not construct request URL.\n");
     154           0 :     GNUNET_free (wh);
     155           0 :     return NULL;
     156             :   }
     157           3 :   body = GNUNET_JSON_PACK (
     158             :     GNUNET_JSON_pack_string ("payto_uri",
     159             :                              payto_uri),
     160             :     GNUNET_JSON_pack_data_auto ("master_sig_del",
     161             :                                 master_sig),
     162             :     GNUNET_JSON_pack_time_abs ("validity_end",
     163             :                                validity_end));
     164           3 :   eh = curl_easy_init ();
     165           3 :   GNUNET_assert (NULL != eh);
     166           3 :   if (GNUNET_OK !=
     167           3 :       TALER_curl_easy_post (&wh->post_ctx,
     168             :                             eh,
     169             :                             body))
     170             :   {
     171           0 :     GNUNET_break (0);
     172           0 :     json_decref (body);
     173           0 :     GNUNET_free (wh->url);
     174           0 :     GNUNET_free (eh);
     175           0 :     return NULL;
     176             :   }
     177           3 :   json_decref (body);
     178           3 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     179             :               "Requesting URL '%s'\n",
     180             :               wh->url);
     181           3 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     182             :                                                CURLOPT_URL,
     183             :                                                wh->url));
     184           6 :   wh->job = GNUNET_CURL_job_add2 (ctx,
     185             :                                   eh,
     186           3 :                                   wh->post_ctx.headers,
     187             :                                   &handle_auditor_disable_finished,
     188             :                                   wh);
     189           3 :   if (NULL == wh->job)
     190             :   {
     191           0 :     TALER_EXCHANGE_management_disable_wire_cancel (wh);
     192           0 :     return NULL;
     193             :   }
     194           3 :   return wh;
     195             : }
     196             : 
     197             : 
     198             : void
     199           3 : TALER_EXCHANGE_management_disable_wire_cancel (
     200             :   struct TALER_EXCHANGE_ManagementWireDisableHandle *wh)
     201             : {
     202           3 :   if (NULL != wh->job)
     203             :   {
     204           0 :     GNUNET_CURL_job_cancel (wh->job);
     205           0 :     wh->job = NULL;
     206             :   }
     207           3 :   TALER_curl_easy_post_finished (&wh->post_ctx);
     208           3 :   GNUNET_free (wh->url);
     209           3 :   GNUNET_free (wh);
     210           3 : }

Generated by: LCOV version 1.14