LCOV - code coverage report
Current view: top level - lib - merchant_api_get_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 59 72.9 %
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) 2022 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_template.c
      19             :  * @brief Implementation of the GET /templates/$ID request of the merchant's HTTP API
      20             :  * @author Priscilla HUANG
      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 /templates/$ID operation.
      36             :  */
      37             : struct TALER_MERCHANT_TemplateGetHandle
      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_TemplateGetCallback 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 /templates/$ID request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_TemplateGetHandle`
      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           6 : handle_get_template_finished (void *cls,
      77             :                               long response_code,
      78             :                               const void *response)
      79             : {
      80           6 :   struct TALER_MERCHANT_TemplateGetHandle *tgh = cls;
      81           6 :   const json_t *json = response;
      82           6 :   struct TALER_MERCHANT_TemplateGetResponse tgr = {
      83           6 :     .hr.http_status = (unsigned int) response_code,
      84             :     .hr.reply = json
      85             :   };
      86             : 
      87           6 :   tgh->job = NULL;
      88           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      89             :               "Got /templates/$ID response with status code %u\n",
      90             :               (unsigned int) response_code);
      91           6 :   switch (response_code)
      92             :   {
      93           4 :   case MHD_HTTP_OK:
      94             :     {
      95             :       const json_t *contract;
      96             :       struct GNUNET_JSON_Specification spec[] = {
      97           4 :         GNUNET_JSON_spec_string ("template_description",
      98             :                                  &tgr.details.ok.template_description),
      99           4 :         GNUNET_JSON_spec_mark_optional (
     100             :           GNUNET_JSON_spec_string ("otp_id",
     101             :                                    &tgr.details.ok.otp_id),
     102             :           NULL),
     103           4 :         GNUNET_JSON_spec_object_const ("template_contract",
     104             :                                        &contract),
     105           4 :         GNUNET_JSON_spec_end ()
     106             :       };
     107             : 
     108           4 :       if (GNUNET_OK ==
     109           4 :           GNUNET_JSON_parse (json,
     110             :                              spec,
     111             :                              NULL, NULL))
     112             :       {
     113           4 :         tgr.details.ok.template_contract = contract;
     114           4 :         tgh->cb (tgh->cb_cls,
     115             :                  &tgr);
     116           4 :         TALER_MERCHANT_template_get_cancel (tgh);
     117           4 :         return;
     118             :       }
     119           0 :       tgr.hr.http_status = 0;
     120           0 :       tgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     121           0 :       break;
     122             :     }
     123           0 :   case MHD_HTTP_UNAUTHORIZED:
     124           0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     125           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     126             :     /* Nothing really to verify, merchant says we need to authenticate. */
     127           0 :     break;
     128           2 :   case MHD_HTTP_NOT_FOUND:
     129           2 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     130           2 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     131           2 :     break;
     132           0 :   default:
     133             :     /* unexpected response code */
     134           0 :     tgr.hr.ec = TALER_JSON_get_error_code (json);
     135           0 :     tgr.hr.hint = TALER_JSON_get_error_hint (json);
     136           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     137             :                 "Unexpected response code %u/%d\n",
     138             :                 (unsigned int) response_code,
     139             :                 (int) tgr.hr.ec);
     140           0 :     break;
     141             :   }
     142           2 :   tgh->cb (tgh->cb_cls,
     143             :            &tgr);
     144           2 :   TALER_MERCHANT_template_get_cancel (tgh);
     145             : }
     146             : 
     147             : 
     148             : struct TALER_MERCHANT_TemplateGetHandle *
     149           6 : TALER_MERCHANT_template_get (
     150             :   struct GNUNET_CURL_Context *ctx,
     151             :   const char *backend_url,
     152             :   const char *template_id,
     153             :   TALER_MERCHANT_TemplateGetCallback cb,
     154             :   void *cb_cls)
     155             : {
     156             :   struct TALER_MERCHANT_TemplateGetHandle *tgh;
     157             :   CURL *eh;
     158             : 
     159           6 :   tgh = GNUNET_new (struct TALER_MERCHANT_TemplateGetHandle);
     160           6 :   tgh->ctx = ctx;
     161           6 :   tgh->cb = cb;
     162           6 :   tgh->cb_cls = cb_cls;
     163             :   {
     164             :     char *path;
     165             : 
     166           6 :     GNUNET_asprintf (&path,
     167             :                      "private/templates/%s",
     168             :                      template_id);
     169           6 :     tgh->url = TALER_url_join (backend_url,
     170             :                                path,
     171             :                                NULL);
     172           6 :     GNUNET_free (path);
     173             :   }
     174           6 :   if (NULL == tgh->url)
     175             :   {
     176           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     177             :                 "Could not construct request URL.\n");
     178           0 :     GNUNET_free (tgh);
     179           0 :     return NULL;
     180             :   }
     181           6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     182             :               "Requesting URL '%s'\n",
     183             :               tgh->url);
     184           6 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     185           6 :   tgh->job = GNUNET_CURL_job_add (ctx,
     186             :                                   eh,
     187             :                                   &handle_get_template_finished,
     188             :                                   tgh);
     189           6 :   return tgh;
     190             : }
     191             : 
     192             : 
     193             : void
     194           6 : TALER_MERCHANT_template_get_cancel (
     195             :   struct TALER_MERCHANT_TemplateGetHandle *tgh)
     196             : {
     197           6 :   if (NULL != tgh->job)
     198           0 :     GNUNET_CURL_job_cancel (tgh->job);
     199           6 :   GNUNET_free (tgh->url);
     200           6 :   GNUNET_free (tgh);
     201           6 : }

Generated by: LCOV version 1.16