LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 90 0.0 %
Date: 2022-08-25 06:17:04 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a GET /instances/$ID operation.
      36             :  */
      37             : struct TALER_MERCHANT_InstanceGetHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      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_MERCHANT_InstanceGetCallback 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             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP GET /instances/$ID request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_InstanceGetHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param response response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_get_instance_finished (void *cls,
      77             :                               long response_code,
      78             :                               const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_InstanceGetHandle *igh = cls;
      81           0 :   const json_t *json = response;
      82           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      83           0 :     .http_status = (unsigned int) response_code,
      84             :     .reply = json
      85             :   };
      86             : 
      87           0 :   igh->job = NULL;
      88           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89             :               "Got /instances/$ID response with status code %u\n",
      90             :               (unsigned int) response_code);
      91           0 :   switch (response_code)
      92             :   {
      93           0 :   case MHD_HTTP_OK:
      94             :     {
      95             :       json_t *accounts;
      96             :       const char *name;
      97             :       struct TALER_MerchantPublicKeyP merchant_pub;
      98             :       json_t *address;
      99             :       json_t *jurisdiction;
     100             :       struct TALER_Amount default_max_wire_fee;
     101             :       uint32_t default_wire_fee_amortization;
     102             :       struct TALER_Amount default_max_deposit_fee;
     103             :       struct GNUNET_TIME_Relative default_wire_transfer_delay;
     104             :       struct GNUNET_TIME_Relative default_pay_delay;
     105             :       struct GNUNET_JSON_Specification spec[] = {
     106           0 :         GNUNET_JSON_spec_json ("accounts",
     107             :                                &accounts),
     108           0 :         GNUNET_JSON_spec_string ("name",
     109             :                                  &name),
     110           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     111             :                                      &merchant_pub),
     112           0 :         GNUNET_JSON_spec_json ("address",
     113             :                                &address),
     114           0 :         GNUNET_JSON_spec_json ("jurisdiction",
     115             :                                &jurisdiction),
     116           0 :         TALER_JSON_spec_amount_any ("default_max_wire_fee",
     117             :                                     &default_max_wire_fee),
     118           0 :         GNUNET_JSON_spec_uint32 ("default_wire_fee_amortization",
     119             :                                  &default_wire_fee_amortization),
     120           0 :         TALER_JSON_spec_amount_any ("default_max_deposit_fee",
     121             :                                     &default_max_deposit_fee),
     122           0 :         GNUNET_JSON_spec_relative_time ("default_wire_transfer_delay",
     123             :                                         &default_wire_transfer_delay),
     124           0 :         GNUNET_JSON_spec_relative_time ("default_pay_delay",
     125             :                                         &default_pay_delay),
     126           0 :         GNUNET_JSON_spec_end ()
     127             :       };
     128             : 
     129           0 :       if ( (GNUNET_OK ==
     130           0 :             GNUNET_JSON_parse (json,
     131             :                                spec,
     132           0 :                                NULL, NULL)) &&
     133           0 :            (json_is_array (accounts)) )
     134           0 :       {
     135           0 :         unsigned int accounts_length = json_array_size (accounts);
     136           0 :         struct TALER_MERCHANT_Account aa[accounts_length];
     137           0 :         const char *payto_uris[accounts_length];
     138             :         size_t index;
     139             :         json_t *value;
     140           0 :         int ret = GNUNET_OK;
     141             : 
     142           0 :         memset (payto_uris, 0, sizeof (payto_uris));
     143           0 :         json_array_foreach (accounts, index, value)
     144             :         {
     145             :           struct GNUNET_JSON_Specification spec[] = {
     146           0 :             GNUNET_JSON_spec_fixed_auto ("salt",
     147             :                                          &aa[index].salt),
     148           0 :             GNUNET_JSON_spec_string ("payto_uri",
     149             :                                      &payto_uris[index]),
     150           0 :             GNUNET_JSON_spec_fixed_auto ("h_wire",
     151             :                                          &aa[index].h_wire),
     152           0 :             GNUNET_JSON_spec_bool ("active",
     153             :                                    &aa[index].active),
     154           0 :             GNUNET_JSON_spec_end ()
     155             :           };
     156             : 
     157           0 :           if (GNUNET_OK !=
     158           0 :               GNUNET_JSON_parse (value,
     159             :                                  spec,
     160             :                                  NULL, NULL))
     161             :           {
     162           0 :             GNUNET_break_op (0);
     163           0 :             ret = GNUNET_SYSERR;
     164           0 :             break;
     165             :           }
     166           0 :           aa[index].payto_uri = payto_uris[index];
     167             :         }
     168             : 
     169           0 :         if (GNUNET_OK == ret)
     170             :         {
     171           0 :           struct TALER_MERCHANT_InstanceDetails details = {
     172             :             .name = name,
     173             :             .merchant_pub = &merchant_pub,
     174             :             .address = address,
     175             :             .jurisdiction = jurisdiction,
     176             :             .default_max_wire_fee = &default_max_wire_fee,
     177             :             .default_wire_fee_amortization = default_wire_fee_amortization,
     178             :             .default_max_deposit_fee = &default_max_deposit_fee,
     179             :             .default_wire_transfer_delay = default_wire_transfer_delay,
     180             :             .default_pay_delay = default_pay_delay
     181             :           };
     182             : 
     183           0 :           igh->cb (igh->cb_cls,
     184             :                    &hr,
     185             :                    accounts_length,
     186             :                    aa,
     187             :                    &details);
     188           0 :           GNUNET_JSON_parse_free (spec);
     189           0 :           TALER_MERCHANT_instance_get_cancel (igh);
     190           0 :           return;
     191             :         }
     192             :       }
     193           0 :       GNUNET_break_op (0);
     194           0 :       hr.http_status = 0;
     195           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     196           0 :       GNUNET_JSON_parse_free (spec);
     197           0 :       break;
     198             :     }
     199           0 :   case MHD_HTTP_UNAUTHORIZED:
     200           0 :     hr.ec = TALER_JSON_get_error_code (json);
     201           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     202             :     /* Nothing really to verify, merchant says we need to authenticate. */
     203           0 :     break;
     204           0 :   case MHD_HTTP_NOT_FOUND:
     205             :     /* instance does not exist */
     206           0 :     hr.ec = TALER_JSON_get_error_code (json);
     207           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     208           0 :     break;
     209           0 :   default:
     210             :     /* unexpected response code */
     211           0 :     hr.ec = TALER_JSON_get_error_code (json);
     212           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     213           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     214             :                 "Unexpected response code %u/%d\n",
     215             :                 (unsigned int) response_code,
     216             :                 (int) hr.ec);
     217           0 :     break;
     218             :   }
     219           0 :   igh->cb (igh->cb_cls,
     220             :            &hr,
     221             :            0,
     222             :            NULL,
     223             :            NULL);
     224           0 :   TALER_MERCHANT_instance_get_cancel (igh);
     225             : }
     226             : 
     227             : 
     228             : struct TALER_MERCHANT_InstanceGetHandle *
     229           0 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
     230             :                              const char *backend_url,
     231             :                              const char *instance_id,
     232             :                              TALER_MERCHANT_InstanceGetCallback cb,
     233             :                              void *cb_cls)
     234             : {
     235             :   struct TALER_MERCHANT_InstanceGetHandle *igh;
     236             :   CURL *eh;
     237             : 
     238           0 :   igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
     239           0 :   igh->ctx = ctx;
     240           0 :   igh->cb = cb;
     241           0 :   igh->cb_cls = cb_cls;
     242             :   {
     243             :     char *path;
     244             : 
     245           0 :     GNUNET_asprintf (&path,
     246             :                      "instances/%s/private",
     247             :                      instance_id);
     248           0 :     igh->url = TALER_url_join (backend_url,
     249             :                                path,
     250             :                                NULL);
     251           0 :     GNUNET_free (path);
     252             :   }
     253           0 :   if (NULL == igh->url)
     254             :   {
     255           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     256             :                 "Could not construct request URL.\n");
     257           0 :     GNUNET_free (igh);
     258           0 :     return NULL;
     259             :   }
     260           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     261             :               "Requesting URL '%s'\n",
     262             :               igh->url);
     263           0 :   eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
     264           0 :   igh->job = GNUNET_CURL_job_add (ctx,
     265             :                                   eh,
     266             :                                   &handle_get_instance_finished,
     267             :                                   igh);
     268           0 :   return igh;
     269             : }
     270             : 
     271             : 
     272             : void
     273           0 : TALER_MERCHANT_instance_get_cancel (
     274             :   struct TALER_MERCHANT_InstanceGetHandle *igh)
     275             : {
     276           0 :   if (NULL != igh->job)
     277           0 :     GNUNET_CURL_job_cancel (igh->job);
     278           0 :   GNUNET_free (igh->url);
     279           0 :   GNUNET_free (igh);
     280           0 : }

Generated by: LCOV version 1.14