LCOV - code coverage report
Current view: top level - lib - merchant_api_get_instance.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.4 % 69 52
Test Date: 2025-11-06 19:31:41 Functions: 100.0 % 3 3

            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 json_t *address;
      97              :       const json_t *jurisdiction;
      98              :       struct GNUNET_JSON_Specification spec[] = {
      99            5 :         GNUNET_JSON_spec_string (
     100              :           "name",
     101              :           &igr.details.ok.details.name),
     102            5 :         GNUNET_JSON_spec_fixed_auto (
     103              :           "merchant_pub",
     104              :           &igr.details.ok.details.merchant_pub),
     105            5 :         GNUNET_JSON_spec_object_const (
     106              :           "address",
     107              :           &address),
     108            5 :         GNUNET_JSON_spec_object_const (
     109              :           "jurisdiction",
     110              :           &jurisdiction),
     111            5 :         GNUNET_JSON_spec_bool (
     112              :           "use_stefan",
     113              :           &igr.details.ok.details.use_stefan),
     114            5 :         GNUNET_JSON_spec_relative_time (
     115              :           "default_wire_transfer_delay",
     116              :           &igr.details.ok.details.default_wire_transfer_delay),
     117            5 :         GNUNET_JSON_spec_relative_time (
     118              :           "default_pay_delay",
     119              :           &igr.details.ok.details.default_pay_delay),
     120            5 :         GNUNET_JSON_spec_mark_optional (
     121              :           GNUNET_JSON_spec_relative_time (
     122              :             "default_refund_delay",
     123              :             &igr.details.ok.details.default_refund_delay),
     124              :           NULL),
     125            5 :         GNUNET_JSON_spec_mark_optional (
     126              :           GNUNET_JSON_spec_time_rounder_interval (
     127              :             "default_wire_transfer_rounding_interval",
     128              :             &igr.details.ok.details.default_wire_transfer_rounding_interval),
     129              :           NULL),
     130            5 :         GNUNET_JSON_spec_end ()
     131              :       };
     132              : 
     133            5 :       if (GNUNET_OK !=
     134            5 :           GNUNET_JSON_parse (json,
     135              :                              spec,
     136              :                              NULL, NULL))
     137              :       {
     138            0 :         GNUNET_break_op (0);
     139            0 :         igr.hr.http_status = 0;
     140            0 :         igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     141            0 :         break;
     142              :       }
     143            5 :       igr.details.ok.details.address = address;
     144            5 :       igr.details.ok.details.jurisdiction = jurisdiction;
     145            5 :       igh->cb (igh->cb_cls,
     146              :                &igr);
     147            5 :       TALER_MERCHANT_instance_get_cancel (igh);
     148            5 :       return;
     149              :     }
     150            0 :   case MHD_HTTP_UNAUTHORIZED:
     151            0 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     152            0 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     153              :     /* Nothing really to verify, merchant says we need to authenticate. */
     154            0 :     break;
     155            4 :   case MHD_HTTP_NOT_FOUND:
     156              :     /* instance does not exist */
     157            4 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     158            4 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     159            4 :     break;
     160            0 :   default:
     161              :     /* unexpected response code */
     162            0 :     igr.hr.ec = TALER_JSON_get_error_code (json);
     163            0 :     igr.hr.hint = TALER_JSON_get_error_hint (json);
     164            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     165              :                 "Unexpected response code %u/%d\n",
     166              :                 (unsigned int) response_code,
     167              :                 (int) igr.hr.ec);
     168            0 :     break;
     169              :   }
     170            4 :   igh->cb (igh->cb_cls,
     171              :            &igr);
     172            4 :   TALER_MERCHANT_instance_get_cancel (igh);
     173              : }
     174              : 
     175              : 
     176              : struct TALER_MERCHANT_InstanceGetHandle *
     177            9 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
     178              :                              const char *backend_url,
     179              :                              const char *instance_id,
     180              :                              TALER_MERCHANT_InstanceGetCallback cb,
     181              :                              void *cb_cls)
     182              : {
     183              :   struct TALER_MERCHANT_InstanceGetHandle *igh;
     184              :   CURL *eh;
     185              : 
     186            9 :   igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
     187            9 :   igh->ctx = ctx;
     188            9 :   igh->cb = cb;
     189            9 :   igh->cb_cls = cb_cls;
     190            9 :   if (NULL != instance_id)
     191              :   {
     192              :     char *path;
     193              : 
     194            8 :     GNUNET_asprintf (&path,
     195              :                      "instances/%s/private",
     196              :                      instance_id);
     197            8 :     igh->url = TALER_url_join (backend_url,
     198              :                                path,
     199              :                                NULL);
     200            8 :     GNUNET_free (path);
     201              :   }
     202              :   else
     203              :   {
     204            1 :     igh->url = TALER_url_join (backend_url,
     205              :                                "private",
     206              :                                NULL);
     207              :   }
     208            9 :   if (NULL == igh->url)
     209              :   {
     210            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     211              :                 "Could not construct request URL.\n");
     212            0 :     GNUNET_free (igh);
     213            0 :     return NULL;
     214              :   }
     215            9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     216              :               "Requesting URL '%s'\n",
     217              :               igh->url);
     218            9 :   eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
     219            9 :   igh->job = GNUNET_CURL_job_add (ctx,
     220              :                                   eh,
     221              :                                   &handle_get_instance_finished,
     222              :                                   igh);
     223            9 :   return igh;
     224              : }
     225              : 
     226              : 
     227              : void
     228            9 : TALER_MERCHANT_instance_get_cancel (
     229              :   struct TALER_MERCHANT_InstanceGetHandle *igh)
     230              : {
     231            9 :   if (NULL != igh->job)
     232            0 :     GNUNET_CURL_job_cancel (igh->job);
     233            9 :   GNUNET_free (igh->url);
     234            9 :   GNUNET_free (igh);
     235            9 : }
        

Generated by: LCOV version 2.0-1