LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instance.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 51 68 75.0 %
Date: 2025-06-23 16:22:09 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2023 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_kyclogic_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a GET /instances/$ID operation.
      37             :  */
      38             : struct TALER_MERCHANT_InstanceGetHandle
      39             : {
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_InstanceGetCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : 
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Function called when we're done processing the
      70             :  * HTTP GET /instances/$ID request.
      71             :  *
      72             :  * @param cls the `struct TALER_MERCHANT_InstanceGetHandle`
      73             :  * @param response_code HTTP response code, 0 on error
      74             :  * @param response response body, NULL if not in JSON
      75             :  */
      76             : static void
      77           9 : handle_get_instance_finished (void *cls,
      78             :                               long response_code,
      79             :                               const void *response)
      80             : {
      81           9 :   struct TALER_MERCHANT_InstanceGetHandle *igh = cls;
      82           9 :   const json_t *json = response;
      83           9 :   struct TALER_MERCHANT_InstanceGetResponse igr = {
      84           9 :     .hr.http_status = (unsigned int) response_code,
      85             :     .hr.reply = json
      86             :   };
      87             : 
      88           9 :   igh->job = NULL;
      89           9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      90             :               "Got /instances/$ID response with status code %u\n",
      91             :               (unsigned int) response_code);
      92           9 :   switch (response_code)
      93             :   {
      94           5 :   case MHD_HTTP_OK:
      95             :     {
      96             :       const char *uts;
      97             :       const json_t *address;
      98             :       const json_t *jurisdiction;
      99             :       struct GNUNET_JSON_Specification spec[] = {
     100           5 :         GNUNET_JSON_spec_string (
     101             :           "name",
     102             :           &igr.details.ok.details.name),
     103           5 :         GNUNET_JSON_spec_string (
     104             :           "user_type",
     105             :           &uts),
     106           5 :         GNUNET_JSON_spec_fixed_auto (
     107             :           "merchant_pub",
     108             :           &igr.details.ok.details.merchant_pub),
     109           5 :         GNUNET_JSON_spec_object_const (
     110             :           "address",
     111             :           &address),
     112           5 :         GNUNET_JSON_spec_object_const (
     113             :           "jurisdiction",
     114             :           &jurisdiction),
     115           5 :         GNUNET_JSON_spec_bool (
     116             :           "use_stefan",
     117             :           &igr.details.ok.details.use_stefan),
     118           5 :         GNUNET_JSON_spec_relative_time (
     119             :           "default_wire_transfer_delay",
     120             :           &igr.details.ok.details.default_wire_transfer_delay),
     121           5 :         GNUNET_JSON_spec_relative_time (
     122             :           "default_pay_delay",
     123             :           &igr.details.ok.details.default_pay_delay),
     124           5 :         GNUNET_JSON_spec_end ()
     125             :       };
     126             : 
     127           5 :       if (GNUNET_OK !=
     128           5 :           GNUNET_JSON_parse (json,
     129             :                              spec,
     130             :                              NULL, NULL))
     131             :       {
     132           0 :         GNUNET_break_op (0);
     133           0 :         igr.hr.http_status = 0;
     134           0 :         igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     135           0 :         break;
     136             :       }
     137           5 :       igr.details.ok.details.address = address;
     138           5 :       igr.details.ok.details.jurisdiction = jurisdiction;
     139           5 :       igh->cb (igh->cb_cls,
     140             :                &igr);
     141           5 :       TALER_MERCHANT_instance_get_cancel (igh);
     142           5 :       return;
     143             :     }
     144           0 :   case MHD_HTTP_UNAUTHORIZED:
     145           0 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     146           0 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     147             :     /* Nothing really to verify, merchant says we need to authenticate. */
     148           0 :     break;
     149           4 :   case MHD_HTTP_NOT_FOUND:
     150             :     /* instance does not exist */
     151           4 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     152           4 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     153           4 :     break;
     154           0 :   default:
     155             :     /* unexpected response code */
     156           0 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     157           0 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     158           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     159             :                 "Unexpected response code %u/%d\n",
     160             :                 (unsigned int) response_code,
     161             :                 (int) igr.hr.ec);
     162           0 :     break;
     163             :   }
     164           4 :   igh->cb (igh->cb_cls,
     165             :            &igr);
     166           4 :   TALER_MERCHANT_instance_get_cancel (igh);
     167             : }
     168             : 
     169             : 
     170             : struct TALER_MERCHANT_InstanceGetHandle *
     171           9 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
     172             :                              const char *backend_url,
     173             :                              const char *instance_id,
     174             :                              TALER_MERCHANT_InstanceGetCallback cb,
     175             :                              void *cb_cls)
     176             : {
     177             :   struct TALER_MERCHANT_InstanceGetHandle *igh;
     178             :   CURL *eh;
     179             : 
     180           9 :   igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
     181           9 :   igh->ctx = ctx;
     182           9 :   igh->cb = cb;
     183           9 :   igh->cb_cls = cb_cls;
     184           9 :   if (NULL != instance_id)
     185             :   {
     186             :     char *path;
     187             : 
     188           8 :     GNUNET_asprintf (&path,
     189             :                      "instances/%s/private",
     190             :                      instance_id);
     191           8 :     igh->url = TALER_url_join (backend_url,
     192             :                                path,
     193             :                                NULL);
     194           8 :     GNUNET_free (path);
     195             :   }
     196             :   else
     197             :   {
     198           1 :     igh->url = TALER_url_join (backend_url,
     199             :                                "private",
     200             :                                NULL);
     201             :   }
     202           9 :   if (NULL == igh->url)
     203             :   {
     204           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     205             :                 "Could not construct request URL.\n");
     206           0 :     GNUNET_free (igh);
     207           0 :     return NULL;
     208             :   }
     209           9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     210             :               "Requesting URL '%s'\n",
     211             :               igh->url);
     212           9 :   eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
     213           9 :   igh->job = GNUNET_CURL_job_add (ctx,
     214             :                                   eh,
     215             :                                   &handle_get_instance_finished,
     216             :                                   igh);
     217           9 :   return igh;
     218             : }
     219             : 
     220             : 
     221             : void
     222           9 : TALER_MERCHANT_instance_get_cancel (
     223             :   struct TALER_MERCHANT_InstanceGetHandle *igh)
     224             : {
     225           9 :   if (NULL != igh->job)
     226           0 :     GNUNET_CURL_job_cancel (igh->job);
     227           9 :   GNUNET_free (igh->url);
     228           9 :   GNUNET_free (igh);
     229           9 : }

Generated by: LCOV version 1.16