LCOV - code coverage report
Current view: top level - lib - merchant_api_post_tokenfamilies.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 72 47.2 %
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) 2020-2024 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Lesser General Public License as
       7             :   published by the Free Software Foundation; either version 2.1,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful,
      11             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU Lesser General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU Lesser General
      16             :   Public License along with TALER; see the file COPYING.LGPL.
      17             :   If not, see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file merchant_api_post_tokenfamilies.c
      21             :  * @brief Implementation of the POST /tokenfamilies request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Christian Blättler
      24             :  */
      25             : #include "platform.h"
      26             : #include <curl/curl.h>
      27             : #include <gnunet/gnunet_json_lib.h>
      28             : #include <gnunet/gnunet_time_lib.h>
      29             : #include <jansson.h>
      30             : #include <microhttpd.h> /* just for HTTP status codes */
      31             : #include <gnunet/gnunet_util_lib.h>
      32             : #include "taler_merchant_service.h"
      33             : #include "merchant_api_curl_defaults.h"
      34             : #include "merchant_api_common.h"
      35             : #include <taler/taler_json_lib.h>
      36             : #include <taler/taler_curl_lib.h>
      37             : 
      38             : 
      39             : /**
      40             :  * Handle for a POST /tokenfamilies operation.
      41             :  */
      42             : struct TALER_MERCHANT_TokenFamiliesPostHandle
      43             : {
      44             : 
      45             :   /**
      46             :    * The url for this request.
      47             :    */
      48             :   char *url;
      49             : 
      50             :   /**
      51             :    * Handle for the request.
      52             :    */
      53             :   struct GNUNET_CURL_Job *job;
      54             : 
      55             :   /**
      56             :    * Function to call with the result.
      57             :    */
      58             :   TALER_MERCHANT_TokenFamiliesPostCallback cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a cb.
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             :   /**
      66             :    * Reference to the execution context.
      67             :    */
      68             :   struct GNUNET_CURL_Context *ctx;
      69             : 
      70             :   /**
      71             :    * Minor context that holds body and headers.
      72             :    */
      73             :   struct TALER_CURL_PostContext post_ctx;
      74             : 
      75             : };
      76             : 
      77             : /**
      78             :  * Function called when we're done processing the
      79             :  * HTTP POST /tokenfamilies request.
      80             :  *
      81             :  * @param cls the `struct TALER_MERCHANT_TokenFamiliesPostHandle`
      82             :  * @param response_code HTTP response code, 0 on error
      83             :  * @param response response body, NULL if not in JSON
      84             :  */
      85             : static void
      86           4 : handle_post_token_families_finished (void *cls,
      87             :                                      long response_code,
      88             :                                      const void *response)
      89             : {
      90           4 :   struct TALER_MERCHANT_TokenFamiliesPostHandle *handle = cls;
      91           4 :   const json_t *json = response;
      92           4 :   struct TALER_MERCHANT_HttpResponse hr = {
      93           4 :     .http_status = (unsigned int) response_code,
      94             :     .reply = json
      95             :   };
      96             : 
      97           4 :   handle->job = NULL;
      98           4 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      99             :               "POST /tokenfamilies completed with response code %u\n",
     100             :               (unsigned int) response_code);
     101           4 :   switch (response_code)
     102             :   {
     103           0 :   case 0:
     104           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     105           0 :     break;
     106           4 :   case MHD_HTTP_NO_CONTENT:
     107           4 :     break;
     108           0 :   case MHD_HTTP_BAD_REQUEST:
     109           0 :     hr.ec = TALER_JSON_get_error_code (json);
     110           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     111             :     /* This should never happen, either us
     112             :      * or the merchant is buggy (or API version conflict);
     113             :      * just pass JSON reply to the application */
     114           0 :     break;
     115           0 :   case MHD_HTTP_UNAUTHORIZED:
     116           0 :     hr.ec = TALER_JSON_get_error_code (json);
     117           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     118             :     /* Nothing really to verify, merchant says we need to authenticate. */
     119           0 :     break;
     120           0 :   case MHD_HTTP_FORBIDDEN:
     121           0 :     hr.ec = TALER_JSON_get_error_code (json);
     122           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123             :     /* Nothing really to verify, merchant says we tried to abort the payment
     124             :      * after it was successful. We should pass the JSON reply to the
     125             :      * application */
     126           0 :     break;
     127           0 :   case MHD_HTTP_NOT_FOUND:
     128           0 :     hr.ec = TALER_JSON_get_error_code (json);
     129           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     130             :     /* Nothing really to verify, this should never
     131             :        happen, we should pass the JSON reply to the
     132             :        application */
     133           0 :     break;
     134           0 :   case MHD_HTTP_CONFLICT:
     135           0 :     hr.ec = TALER_JSON_get_error_code (json);
     136           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     137           0 :     break;
     138           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     139           0 :     hr.ec = TALER_JSON_get_error_code (json);
     140           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     141             :     /* Server had an internal issue; we should retry,
     142             :        but this API leaves this to the application */
     143           0 :     break;
     144           0 :   default:
     145           0 :     TALER_MERCHANT_parse_error_details_ (json,
     146             :                                          response_code,
     147             :                                          &hr);
     148             :     /* unexpected response code */
     149           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     150             :                 "Unexpected response code %u/%d\n",
     151             :                 (unsigned int) response_code,
     152             :                 (int) hr.ec);
     153           0 :     GNUNET_break_op (0);
     154           0 :     break;
     155             :   }
     156           4 :   handle->cb (handle->cb_cls,
     157             :               &hr);
     158           4 :   TALER_MERCHANT_token_families_post_cancel (handle);
     159           4 : }
     160             : 
     161             : 
     162             : struct TALER_MERCHANT_TokenFamiliesPostHandle *
     163           4 : TALER_MERCHANT_token_families_post (
     164             :   struct GNUNET_CURL_Context *ctx,
     165             :   const char *backend_url,
     166             :   const char *slug,
     167             :   const char *name,
     168             :   const char *description,
     169             :   const json_t *description_i18n,
     170             :   const json_t *extra_data,
     171             :   struct GNUNET_TIME_Timestamp valid_after,
     172             :   struct GNUNET_TIME_Timestamp valid_before,
     173             :   struct GNUNET_TIME_Relative duration,
     174             :   struct GNUNET_TIME_Relative validity_granularity,
     175             :   struct GNUNET_TIME_Relative start_offset,
     176             :   const char *kind,
     177             :   TALER_MERCHANT_TokenFamiliesPostCallback cb,
     178             :   void *cb_cls)
     179             : {
     180             :   struct TALER_MERCHANT_TokenFamiliesPostHandle *handle;
     181             :   json_t *req_obj;
     182             : 
     183           4 :   req_obj = GNUNET_JSON_PACK (
     184             :     GNUNET_JSON_pack_string ("slug",
     185             :                              slug),
     186             :     GNUNET_JSON_pack_string ("name",
     187             :                              name),
     188             :     GNUNET_JSON_pack_string ("description",
     189             :                              description),
     190             :     GNUNET_JSON_pack_allow_null (
     191             :       GNUNET_JSON_pack_object_incref ("description_i18n",
     192             :                                       (json_t *) description_i18n)),
     193             :     GNUNET_JSON_pack_allow_null (
     194             :       GNUNET_JSON_pack_object_incref ("extra_data",
     195             :                                       (json_t *) extra_data)),
     196             :     GNUNET_JSON_pack_allow_null (
     197             :       GNUNET_JSON_pack_timestamp ("valid_after",
     198             :                                   valid_after)),
     199             :     GNUNET_JSON_pack_timestamp ("valid_before",
     200             :                                 valid_before),
     201             :     GNUNET_JSON_pack_time_rel ("duration",
     202             :                                duration),
     203             :     GNUNET_JSON_pack_time_rel ("validity_granularity",
     204             :                                validity_granularity),
     205             :     GNUNET_JSON_pack_time_rel ("start_offset",
     206             :                                start_offset),
     207             :     GNUNET_JSON_pack_string ("kind",
     208             :                              kind));
     209           4 :   handle = GNUNET_new (struct TALER_MERCHANT_TokenFamiliesPostHandle);
     210           4 :   handle->ctx = ctx;
     211           4 :   handle->cb = cb;
     212           4 :   handle->cb_cls = cb_cls;
     213           4 :   handle->url = TALER_url_join (backend_url,
     214             :                                 "private/tokenfamilies",
     215             :                                 NULL);
     216           4 :   if (NULL == handle->url)
     217             :   {
     218             : 
     219           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     220             :                 "Could not construct request URL.\n");
     221           0 :     json_decref (req_obj);
     222           0 :     GNUNET_free (handle);
     223           0 :     return NULL;
     224             :   }
     225             :   {
     226             :     CURL *eh;
     227             : 
     228           4 :     eh = TALER_MERCHANT_curl_easy_get_ (handle->url);
     229           4 :     GNUNET_assert (GNUNET_OK ==
     230             :                    TALER_curl_easy_post (&handle->post_ctx,
     231             :                                          eh,
     232             :                                          req_obj));
     233           4 :     json_decref (req_obj);
     234           8 :     handle->job = GNUNET_CURL_job_add2 (ctx,
     235             :                                         eh,
     236           4 :                                         handle->post_ctx.headers,
     237             :                                         &handle_post_token_families_finished,
     238             :                                         handle);
     239           4 :     GNUNET_assert (NULL != handle->job);
     240             :   }
     241           4 :   return handle;
     242             : }
     243             : 
     244             : 
     245             : void
     246           4 : TALER_MERCHANT_token_families_post_cancel (
     247             :   struct TALER_MERCHANT_TokenFamiliesPostHandle *pph)
     248             : {
     249           4 :   if (NULL != pph->job)
     250             :   {
     251           0 :     GNUNET_CURL_job_cancel (pph->job);
     252           0 :     pph->job = NULL;
     253             :   }
     254           4 :   TALER_curl_easy_post_finished (&pph->post_ctx);
     255           4 :   GNUNET_free (pph->url);
     256           4 :   GNUNET_free (pph);
     257           4 : }

Generated by: LCOV version 1.16