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 83 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 json 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_INVALID_RESPONSE;
     195           0 :       GNUNET_JSON_parse_free (spec);
     196           0 :       break;
     197             :     }
     198           0 :   default:
     199             :     /* unexpected response code */
     200           0 :     hr.ec = TALER_JSON_get_error_code (json);
     201           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     202           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     203             :                 "Unexpected response code %u/%d\n",
     204             :                 (unsigned int) response_code,
     205             :                 (int) hr.ec);
     206           0 :     break;
     207             :   }
     208           0 :   igh->cb (igh->cb_cls,
     209             :            &hr,
     210             :            0,
     211             :            NULL,
     212             :            NULL);
     213           0 :   TALER_MERCHANT_instance_get_cancel (igh);
     214             : }
     215             : 
     216             : 
     217             : /**
     218             :  * Get the instance data of a backend. Will connect to the merchant backend
     219             :  * and obtain information about the instances.  The respective information will
     220             :  * be passed to the @a cb once available.
     221             :  *
     222             :  * @param ctx the context
     223             :  * @param backend_url HTTP base URL for the backend
     224             :  * @param instance_id identity of the instance to get information about
     225             :  * @param cb function to call with the
     226             :  *        backend's instances information
     227             :  * @param cb_cls closure for @a cb
     228             :  * @return the instances handle; NULL upon error
     229             :  */
     230             : struct TALER_MERCHANT_InstanceGetHandle *
     231           0 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
     232             :                              const char *backend_url,
     233             :                              const char *instance_id,
     234             :                              TALER_MERCHANT_InstanceGetCallback cb,
     235             :                              void *cb_cls)
     236             : {
     237             :   struct TALER_MERCHANT_InstanceGetHandle *igh;
     238             :   CURL *eh;
     239             : 
     240           0 :   igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
     241           0 :   igh->ctx = ctx;
     242           0 :   igh->cb = cb;
     243           0 :   igh->cb_cls = cb_cls;
     244             :   {
     245             :     char *path;
     246             : 
     247           0 :     GNUNET_asprintf (&path,
     248             :                      "private/instances/%s",
     249             :                      instance_id);
     250           0 :     igh->url = TALER_url_join (backend_url,
     251             :                                path,
     252             :                                NULL);
     253           0 :     GNUNET_free (path);
     254             :   }
     255           0 :   if (NULL == igh->url)
     256             :   {
     257           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     258             :                 "Could not construct request URL.\n");
     259           0 :     GNUNET_free (igh);
     260           0 :     return NULL;
     261             :   }
     262           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     263             :               "Requesting URL '%s'\n",
     264             :               igh->url);
     265           0 :   eh = curl_easy_init ();
     266           0 :   GNUNET_assert (CURLE_OK ==
     267             :                  curl_easy_setopt (eh,
     268             :                                    CURLOPT_URL,
     269             :                                    igh->url));
     270           0 :   igh->job = GNUNET_CURL_job_add (ctx,
     271             :                                   eh,
     272             :                                   &handle_get_instance_finished,
     273             :                                   igh);
     274           0 :   return igh;
     275             : }
     276             : 
     277             : 
     278             : /**
     279             :  * Cancel GET /instance/$ID request.  Must not be called by clients after
     280             :  * the callback was invoked.
     281             :  *
     282             :  * @param igh request to cancel.
     283             :  */
     284             : void
     285           0 : TALER_MERCHANT_instance_get_cancel (
     286             :   struct TALER_MERCHANT_InstanceGetHandle *igh)
     287             : {
     288           0 :   if (NULL != igh->job)
     289           0 :     GNUNET_CURL_job_cancel (igh->job);
     290           0 :   GNUNET_free (igh->url);
     291           0 :   GNUNET_free (igh);
     292           0 : }

Generated by: LCOV version 1.14