LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 87 0.0 %
Date: 2021-04-12 06:04:58 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018, 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_get_instance.c
      19             :  * @brief Implementation of the GET /instance/$ID request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include <taler/taler_json_lib.h>
      30             : #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Handle for a GET /instances/$ID operation.
      35             :  */
      36             : struct TALER_MERCHANT_InstanceGetHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_InstanceGetCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP GET /instances/$ID request.
      69             :  *
      70             :  * @param cls the `struct TALER_MERCHANT_InstanceGetHandle`
      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           0 : handle_get_instance_finished (void *cls,
      76             :                               long response_code,
      77             :                               const void *response)
      78             : {
      79           0 :   struct TALER_MERCHANT_InstanceGetHandle *igh = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   igh->job = NULL;
      87           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      88             :               "Got /instances/$ID response with status code %u\n",
      89             :               (unsigned int) response_code);
      90           0 :   switch (response_code)
      91             :   {
      92           0 :   case MHD_HTTP_OK:
      93             :     {
      94             :       json_t *accounts;
      95             :       const char *name;
      96             :       struct TALER_MerchantPublicKeyP merchant_pub;
      97             :       json_t *address;
      98             :       json_t *jurisdiction;
      99             :       struct TALER_Amount default_max_wire_fee;
     100             :       uint32_t default_wire_fee_amortization;
     101             :       struct TALER_Amount default_max_deposit_fee;
     102             :       struct GNUNET_TIME_Relative default_wire_transfer_delay;
     103             :       struct GNUNET_TIME_Relative default_pay_delay;
     104             :       struct GNUNET_JSON_Specification spec[] = {
     105           0 :         GNUNET_JSON_spec_json ("accounts",
     106             :                                &accounts),
     107           0 :         GNUNET_JSON_spec_string ("name",
     108             :                                  &name),
     109           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     110             :                                      &merchant_pub),
     111           0 :         GNUNET_JSON_spec_json ("address",
     112             :                                &address),
     113           0 :         GNUNET_JSON_spec_json ("jurisdiction",
     114             :                                &jurisdiction),
     115           0 :         TALER_JSON_spec_amount ("default_max_wire_fee",
     116             :                                 &default_max_wire_fee),
     117           0 :         GNUNET_JSON_spec_uint32 ("default_wire_fee_amortization",
     118             :                                  &default_wire_fee_amortization),
     119           0 :         TALER_JSON_spec_amount ("default_max_deposit_fee",
     120             :                                 &default_max_deposit_fee),
     121           0 :         TALER_JSON_spec_relative_time ("default_wire_transfer_delay",
     122             :                                        &default_wire_transfer_delay),
     123           0 :         TALER_JSON_spec_relative_time ("default_pay_delay",
     124             :                                        &default_pay_delay),
     125           0 :         GNUNET_JSON_spec_end ()
     126             :       };
     127             : 
     128           0 :       if ( (GNUNET_OK ==
     129           0 :             GNUNET_JSON_parse (json,
     130             :                                spec,
     131           0 :                                NULL, NULL)) &&
     132           0 :            (json_is_array (accounts)) )
     133           0 :       {
     134           0 :         unsigned int accounts_length = json_array_size (accounts);
     135           0 :         struct TALER_MERCHANT_Account aa[accounts_length];
     136           0 :         const char *payto_uris[accounts_length];
     137             :         size_t index;
     138             :         json_t *value;
     139           0 :         int ret = GNUNET_OK;
     140             : 
     141           0 :         memset (payto_uris, 0, sizeof (payto_uris));
     142           0 :         json_array_foreach (accounts, index, value)
     143             :         {
     144             :           struct GNUNET_JSON_Specification spec[] = {
     145           0 :             GNUNET_JSON_spec_fixed_auto ("salt",
     146             :                                          &aa[index].salt),
     147           0 :             GNUNET_JSON_spec_string ("payto_uri",
     148             :                                      &payto_uris[index]),
     149           0 :             GNUNET_JSON_spec_fixed_auto ("h_wire",
     150             :                                          &aa[index].h_wire),
     151           0 :             GNUNET_JSON_spec_bool ("active",
     152             :                                    &aa[index].active),
     153           0 :             GNUNET_JSON_spec_end ()
     154             :           };
     155             : 
     156           0 :           if (GNUNET_OK !=
     157           0 :               GNUNET_JSON_parse (value,
     158             :                                  spec,
     159             :                                  NULL, NULL))
     160             :           {
     161           0 :             GNUNET_break_op (0);
     162           0 :             ret = GNUNET_SYSERR;
     163           0 :             break;
     164             :           }
     165           0 :           aa[index].payto_uri = payto_uris[index];
     166             :         }
     167             : 
     168           0 :         if (GNUNET_OK == ret)
     169             :         {
     170           0 :           struct TALER_MERCHANT_InstanceDetails details = {
     171             :             .name = name,
     172             :             .merchant_pub = &merchant_pub,
     173             :             .address = address,
     174             :             .jurisdiction = jurisdiction,
     175             :             .default_max_wire_fee = &default_max_wire_fee,
     176             :             .default_wire_fee_amortization = default_wire_fee_amortization,
     177             :             .default_max_deposit_fee = &default_max_deposit_fee,
     178             :             .default_wire_transfer_delay = default_wire_transfer_delay,
     179             :             .default_pay_delay = default_pay_delay
     180             :           };
     181             : 
     182           0 :           igh->cb (igh->cb_cls,
     183             :                    &hr,
     184             :                    accounts_length,
     185             :                    aa,
     186             :                    &details);
     187           0 :           GNUNET_JSON_parse_free (spec);
     188           0 :           TALER_MERCHANT_instance_get_cancel (igh);
     189           0 :           return;
     190             :         }
     191             :       }
     192           0 :       GNUNET_break_op (0);
     193           0 :       hr.http_status = 0;
     194           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     195           0 :       GNUNET_JSON_parse_free (spec);
     196           0 :       break;
     197             :     }
     198           0 :   case MHD_HTTP_NOT_FOUND:
     199             :     /* instance does not exist */
     200           0 :     hr.ec = TALER_JSON_get_error_code (json);
     201           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     202           0 :     break;
     203           0 :   default:
     204             :     /* unexpected response code */
     205           0 :     hr.ec = TALER_JSON_get_error_code (json);
     206           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     207           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     208             :                 "Unexpected response code %u/%d\n",
     209             :                 (unsigned int) response_code,
     210             :                 (int) hr.ec);
     211           0 :     break;
     212             :   }
     213           0 :   igh->cb (igh->cb_cls,
     214             :            &hr,
     215             :            0,
     216             :            NULL,
     217             :            NULL);
     218           0 :   TALER_MERCHANT_instance_get_cancel (igh);
     219             : }
     220             : 
     221             : 
     222             : struct TALER_MERCHANT_InstanceGetHandle *
     223           0 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
     224             :                              const char *backend_url,
     225             :                              const char *instance_id,
     226             :                              TALER_MERCHANT_InstanceGetCallback cb,
     227             :                              void *cb_cls)
     228             : {
     229             :   struct TALER_MERCHANT_InstanceGetHandle *igh;
     230             :   CURL *eh;
     231             : 
     232           0 :   igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
     233           0 :   igh->ctx = ctx;
     234           0 :   igh->cb = cb;
     235           0 :   igh->cb_cls = cb_cls;
     236             :   {
     237             :     char *path;
     238             : 
     239           0 :     GNUNET_asprintf (&path,
     240             :                      "private/instances/%s",
     241             :                      instance_id);
     242           0 :     igh->url = TALER_url_join (backend_url,
     243             :                                path,
     244             :                                NULL);
     245           0 :     GNUNET_free (path);
     246             :   }
     247           0 :   if (NULL == igh->url)
     248             :   {
     249           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     250             :                 "Could not construct request URL.\n");
     251           0 :     GNUNET_free (igh);
     252           0 :     return NULL;
     253             :   }
     254           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     255             :               "Requesting URL '%s'\n",
     256             :               igh->url);
     257           0 :   eh = curl_easy_init ();
     258           0 :   GNUNET_assert (CURLE_OK ==
     259             :                  curl_easy_setopt (eh,
     260             :                                    CURLOPT_URL,
     261             :                                    igh->url));
     262           0 :   igh->job = GNUNET_CURL_job_add (ctx,
     263             :                                   eh,
     264             :                                   &handle_get_instance_finished,
     265             :                                   igh);
     266           0 :   return igh;
     267             : }
     268             : 
     269             : 
     270             : void
     271           0 : TALER_MERCHANT_instance_get_cancel (
     272             :   struct TALER_MERCHANT_InstanceGetHandle *igh)
     273             : {
     274           0 :   if (NULL != igh->job)
     275           0 :     GNUNET_CURL_job_cancel (igh->job);
     276           0 :   GNUNET_free (igh->url);
     277           0 :   GNUNET_free (igh);
     278           0 : }

Generated by: LCOV version 1.14