LCOV - code coverage report
Current view: top level - lib - merchant_api_post_instances.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 44.6 % 83 37
Test Date: 2025-10-21 13:10:50 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020-2023 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_instances.c
      21              :  * @brief Implementation of the POST /instances request
      22              :  *        of the merchant's HTTP API
      23              :  * @author Christian Grothoff
      24              :  */
      25              : #include "platform.h"
      26              : #include <curl/curl.h>
      27              : #include <jansson.h>
      28              : #include <microhttpd.h> /* just for HTTP status codes */
      29              : #include <gnunet/gnunet_util_lib.h>
      30              : #include "taler_merchant_service.h"
      31              : #include "merchant_api_curl_defaults.h"
      32              : #include "merchant_api_common.h"
      33              : #include <taler/taler_json_lib.h>
      34              : #include <taler/taler_curl_lib.h>
      35              : #include <taler/taler_kyclogic_lib.h>
      36              : 
      37              : 
      38              : /**
      39              :  * Handle for a POST /instances/$ID operation.
      40              :  */
      41              : struct TALER_MERCHANT_InstancesPostHandle
      42              : {
      43              : 
      44              :   /**
      45              :    * The url for this request.
      46              :    */
      47              :   char *url;
      48              : 
      49              :   /**
      50              :    * Handle for the request.
      51              :    */
      52              :   struct GNUNET_CURL_Job *job;
      53              : 
      54              :   /**
      55              :    * Function to call with the result.
      56              :    */
      57              :   TALER_MERCHANT_InstancesPostCallback cb;
      58              : 
      59              :   /**
      60              :    * Closure for @a cb.
      61              :    */
      62              :   void *cb_cls;
      63              : 
      64              :   /**
      65              :    * Reference to the execution context.
      66              :    */
      67              :   struct GNUNET_CURL_Context *ctx;
      68              : 
      69              :   /**
      70              :    * Minor context that holds body and headers.
      71              :    */
      72              :   struct TALER_CURL_PostContext post_ctx;
      73              : 
      74              : };
      75              : 
      76              : 
      77              : /**
      78              :  * Function called when we're done processing the
      79              :  * HTTP POST /instances request.
      80              :  *
      81              :  * @param cls the `struct TALER_MERCHANT_InstancesPostHandle`
      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           19 : handle_post_instances_finished (void *cls,
      87              :                                 long response_code,
      88              :                                 const void *response)
      89              : {
      90           19 :   struct TALER_MERCHANT_InstancesPostHandle *iph = cls;
      91           19 :   const json_t *json = response;
      92           19 :   struct TALER_MERCHANT_HttpResponse hr = {
      93           19 :     .http_status = (unsigned int) response_code,
      94              :     .reply = json
      95              :   };
      96              : 
      97           19 :   iph->job = NULL;
      98           19 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      99              :               "POST /management/instances completed with response code %u\n",
     100              :               (unsigned int) response_code);
     101           19 :   switch (response_code)
     102              :   {
     103            0 :   case 0:
     104            0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     105            0 :     break;
     106           19 :   case MHD_HTTP_NO_CONTENT:
     107           19 :     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           19 :   iph->cb (iph->cb_cls,
     157              :            &hr);
     158           19 :   TALER_MERCHANT_instances_post_cancel (iph);
     159           19 : }
     160              : 
     161              : 
     162              : struct TALER_MERCHANT_InstancesPostHandle *
     163           19 : TALER_MERCHANT_instances_post (
     164              :   struct GNUNET_CURL_Context *ctx,
     165              :   const char *backend_url,
     166              :   const char *instance_id,
     167              :   const char *name,
     168              :   const json_t *address,
     169              :   const json_t *jurisdiction,
     170              :   bool use_stefan,
     171              :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     172              :   struct GNUNET_TIME_Relative default_pay_delay,
     173              :   const char *auth_password,
     174              :   TALER_MERCHANT_InstancesPostCallback cb,
     175              :   void *cb_cls)
     176              : {
     177              :   struct TALER_MERCHANT_InstancesPostHandle *iph;
     178              :   json_t *req_obj;
     179              :   json_t *auth_obj;
     180              : 
     181           19 :   if (NULL != auth_password)
     182              :   {
     183            0 :     auth_obj = GNUNET_JSON_PACK (
     184              :       GNUNET_JSON_pack_string ("method",
     185              :                                "token"),
     186              :       GNUNET_JSON_pack_string ("password",
     187              :                                auth_password));
     188              :   }
     189              :   else
     190              :   {
     191           19 :     auth_obj = GNUNET_JSON_PACK (
     192              :       GNUNET_JSON_pack_string ("method",
     193              :                                "external"));
     194              :   }
     195           19 :   if (NULL == auth_obj)
     196              :   {
     197            0 :     GNUNET_break (0);
     198            0 :     return NULL;
     199              :   }
     200           19 :   req_obj = GNUNET_JSON_PACK (
     201              :     GNUNET_JSON_pack_string ("id",
     202              :                              instance_id),
     203              :     GNUNET_JSON_pack_string ("name",
     204              :                              name),
     205              :     GNUNET_JSON_pack_object_incref ("address",
     206              :                                     (json_t *) address),
     207              :     GNUNET_JSON_pack_object_incref ("jurisdiction",
     208              :                                     (json_t *) jurisdiction),
     209              :     GNUNET_JSON_pack_bool ("use_stefan",
     210              :                            use_stefan),
     211              :     GNUNET_JSON_pack_time_rel ("default_wire_transfer_delay",
     212              :                                default_wire_transfer_delay),
     213              :     GNUNET_JSON_pack_time_rel ("default_pay_delay",
     214              :                                default_pay_delay),
     215              :     GNUNET_JSON_pack_object_steal ("auth",
     216              :                                    auth_obj));
     217           19 :   iph = GNUNET_new (struct TALER_MERCHANT_InstancesPostHandle);
     218           19 :   iph->ctx = ctx;
     219           19 :   iph->cb = cb;
     220           19 :   iph->cb_cls = cb_cls;
     221           19 :   iph->url = TALER_url_join (backend_url,
     222              :                              "management/instances",
     223              :                              NULL);
     224           19 :   if (NULL == iph->url)
     225              :   {
     226            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     227              :                 "Could not construct request URL.\n");
     228            0 :     json_decref (req_obj);
     229            0 :     GNUNET_free (iph);
     230            0 :     return NULL;
     231              :   }
     232              :   {
     233              :     CURL *eh;
     234              : 
     235           19 :     eh = TALER_MERCHANT_curl_easy_get_ (iph->url);
     236           19 :     if (GNUNET_OK !=
     237           19 :         TALER_curl_easy_post (&iph->post_ctx,
     238              :                               eh,
     239              :                               req_obj))
     240              :     {
     241            0 :       GNUNET_break (0);
     242            0 :       curl_easy_cleanup (eh);
     243            0 :       json_decref (req_obj);
     244            0 :       GNUNET_free (iph);
     245            0 :       return NULL;
     246              :     }
     247           19 :     json_decref (req_obj);
     248           38 :     iph->job = GNUNET_CURL_job_add2 (ctx,
     249              :                                      eh,
     250           19 :                                      iph->post_ctx.headers,
     251              :                                      &handle_post_instances_finished,
     252              :                                      iph);
     253              :   }
     254           19 :   return iph;
     255              : }
     256              : 
     257              : 
     258              : void
     259           19 : TALER_MERCHANT_instances_post_cancel (
     260              :   struct TALER_MERCHANT_InstancesPostHandle *iph)
     261              : {
     262           19 :   if (NULL != iph->job)
     263              :   {
     264            0 :     GNUNET_CURL_job_cancel (iph->job);
     265            0 :     iph->job = NULL;
     266              :   }
     267           19 :   TALER_curl_easy_post_finished (&iph->post_ctx);
     268           19 :   GNUNET_free (iph->url);
     269           19 :   GNUNET_free (iph);
     270           19 : }
     271              : 
     272              : 
     273              : /* end of merchant_api_post_instances.c */
        

Generated by: LCOV version 2.0-1