LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 94 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 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_patch_instance.c
      21             :  * @brief Implementation of the PATCH /instances/$ID 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 <taler/taler_json_lib.h>
      33             : #include <taler/taler_curl_lib.h>
      34             : 
      35             : 
      36             : /**
      37             :  * Handle for a PATCH /instances/$ID operation.
      38             :  */
      39             : struct TALER_MERCHANT_InstancePatchHandle
      40             : {
      41             : 
      42             :   /**
      43             :    * The url for this request.
      44             :    */
      45             :   char *url;
      46             : 
      47             :   /**
      48             :    * Handle for the request.
      49             :    */
      50             :   struct GNUNET_CURL_Job *job;
      51             : 
      52             :   /**
      53             :    * Function to call with the result.
      54             :    */
      55             :   TALER_MERCHANT_InstancePatchCallback cb;
      56             : 
      57             :   /**
      58             :    * Closure for @a cb.
      59             :    */
      60             :   void *cb_cls;
      61             : 
      62             :   /**
      63             :    * Reference to the execution context.
      64             :    */
      65             :   struct GNUNET_CURL_Context *ctx;
      66             : 
      67             :   /**
      68             :    * Minor context that holds body and headers.
      69             :    */
      70             :   struct TALER_CURL_PostContext post_ctx;
      71             : 
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP PATCH /instances/$ID request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_InstancePatchHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param response response body, NULL if not in JSON
      82             :  */
      83             : static void
      84           0 : handle_patch_instance_finished (void *cls,
      85             :                                 long response_code,
      86             :                                 const void *response)
      87             : {
      88           0 :   struct TALER_MERCHANT_InstancePatchHandle *iph = cls;
      89           0 :   const json_t *json = response;
      90           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      91           0 :     .http_status = (unsigned int) response_code,
      92             :     .reply = json
      93             :   };
      94             : 
      95           0 :   iph->job = NULL;
      96           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      97             :               "PATCH /instances/$ID completed with response code %u\n",
      98             :               (unsigned int) response_code);
      99           0 :   switch (response_code)
     100             :   {
     101           0 :   case 0:
     102           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     103           0 :     break;
     104           0 :   case MHD_HTTP_NO_CONTENT:
     105           0 :     break;
     106           0 :   case MHD_HTTP_BAD_REQUEST:
     107           0 :     hr.ec = TALER_JSON_get_error_code (json);
     108           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     109           0 :     break;
     110           0 :   case MHD_HTTP_UNAUTHORIZED:
     111           0 :     hr.ec = TALER_JSON_get_error_code (json);
     112           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     113             :     /* Nothing really to verify, merchant says we need to authenticate. */
     114           0 :     break;
     115           0 :   case MHD_HTTP_FORBIDDEN:
     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 tried to abort the payment
     119             :      * after it was successful. We should pass the JSON reply to the
     120             :      * application */
     121           0 :     break;
     122           0 :   case MHD_HTTP_NOT_FOUND:
     123           0 :     hr.ec = TALER_JSON_get_error_code (json);
     124           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     125           0 :     break;
     126           0 :   case MHD_HTTP_CONFLICT:
     127           0 :     hr.ec = TALER_JSON_get_error_code (json);
     128           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     129           0 :     break;
     130           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     131           0 :     hr.ec = TALER_JSON_get_error_code (json);
     132           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     133             :     /* Server had an internal issue; we should retry,
     134             :        but this API leaves this to the application */
     135           0 :     break;
     136           0 :   default:
     137           0 :     TALER_MERCHANT_parse_error_details_ (json,
     138             :                                          response_code,
     139             :                                          &hr);
     140             :     /* unexpected response code */
     141           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     142             :                 "Unexpected response code %u/%d\n",
     143             :                 (unsigned int) response_code,
     144             :                 (int) hr.ec);
     145           0 :     GNUNET_break_op (0);
     146           0 :     break;
     147             :   }
     148           0 :   iph->cb (iph->cb_cls,
     149             :            &hr);
     150           0 :   TALER_MERCHANT_instance_patch_cancel (iph);
     151           0 : }
     152             : 
     153             : 
     154             : struct TALER_MERCHANT_InstancePatchHandle *
     155           0 : TALER_MERCHANT_instance_patch (
     156             :   struct GNUNET_CURL_Context *ctx,
     157             :   const char *backend_url,
     158             :   const char *instance_id,
     159             :   unsigned int accounts_length,
     160             :   const char *payto_uris[],
     161             :   const char *name,
     162             :   const json_t *address,
     163             :   const json_t *jurisdiction,
     164             :   const struct TALER_Amount *default_max_wire_fee,
     165             :   uint32_t default_wire_fee_amortization,
     166             :   const struct TALER_Amount *default_max_deposit_fee,
     167             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     168             :   struct GNUNET_TIME_Relative default_pay_delay,
     169             :   TALER_MERCHANT_InstancePatchCallback cb,
     170             :   void *cb_cls)
     171             : {
     172             :   struct TALER_MERCHANT_InstancePatchHandle *iph;
     173             :   json_t *jpayto_uris;
     174             :   json_t *req_obj;
     175             : 
     176           0 :   jpayto_uris = json_array ();
     177           0 :   if (NULL == jpayto_uris)
     178             :   {
     179           0 :     GNUNET_break (0);
     180           0 :     return NULL;
     181             :   }
     182           0 :   for (unsigned int i = 0; i<accounts_length; i++)
     183             :   {
     184           0 :     if (0 !=
     185           0 :         json_array_append_new (jpayto_uris,
     186           0 :                                json_string (payto_uris[i])))
     187             :     {
     188           0 :       GNUNET_break (0);
     189           0 :       json_decref (jpayto_uris);
     190           0 :       return NULL;
     191             :     }
     192             :   }
     193           0 :   req_obj = GNUNET_JSON_PACK (
     194             :     GNUNET_JSON_pack_array_steal ("payto_uris",
     195             :                                   jpayto_uris),
     196             :     GNUNET_JSON_pack_string ("name",
     197             :                              name),
     198             :     GNUNET_JSON_pack_object_incref ("address",
     199             :                                     (json_t *) address),
     200             :     GNUNET_JSON_pack_object_incref ("jurisdiction",
     201             :                                     (json_t *) jurisdiction),
     202             :     TALER_JSON_pack_amount ("default_max_wire_fee",
     203             :                             default_max_wire_fee),
     204             :     GNUNET_JSON_pack_uint64 ("default_wire_fee_amortization",
     205             :                              default_wire_fee_amortization),
     206             :     TALER_JSON_pack_amount ("default_max_deposit_fee",
     207             :                             default_max_deposit_fee),
     208             :     GNUNET_JSON_pack_time_rel ("default_wire_transfer_delay",
     209             :                                default_wire_transfer_delay),
     210             :     GNUNET_JSON_pack_time_rel ("default_pay_delay",
     211             :                                default_pay_delay));
     212           0 :   iph = GNUNET_new (struct TALER_MERCHANT_InstancePatchHandle);
     213           0 :   iph->ctx = ctx;
     214           0 :   iph->cb = cb;
     215           0 :   iph->cb_cls = cb_cls;
     216           0 :   if (NULL != instance_id)
     217             :   {
     218             :     char *path;
     219             : 
     220           0 :     GNUNET_asprintf (&path,
     221             :                      "management/instances/%s",
     222             :                      instance_id);
     223           0 :     iph->url = TALER_url_join (backend_url,
     224             :                                path,
     225             :                                NULL);
     226           0 :     GNUNET_free (path);
     227             :   }
     228             :   else
     229             :   {
     230             :     /* backend_url is already identifying the instance */
     231           0 :     iph->url = TALER_url_join (backend_url,
     232             :                                "private",
     233             :                                NULL);
     234             :   }
     235           0 :   if (NULL == iph->url)
     236             :   {
     237           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     238             :                 "Could not construct request URL.\n");
     239           0 :     json_decref (req_obj);
     240           0 :     GNUNET_free (iph);
     241           0 :     return NULL;
     242             :   }
     243             :   {
     244             :     CURL *eh;
     245             : 
     246           0 :     eh = TALER_MERCHANT_curl_easy_get_ (iph->url);
     247           0 :     if (GNUNET_OK !=
     248           0 :         TALER_curl_easy_post (&iph->post_ctx,
     249             :                               eh,
     250             :                               req_obj))
     251             :     {
     252           0 :       GNUNET_break (0);
     253           0 :       curl_easy_cleanup (eh);
     254           0 :       json_decref (req_obj);
     255           0 :       GNUNET_free (iph->url);
     256           0 :       GNUNET_free (iph);
     257           0 :       return NULL;
     258             :     }
     259           0 :     json_decref (req_obj);
     260           0 :     GNUNET_assert (CURLE_OK ==
     261             :                    curl_easy_setopt (eh,
     262             :                                      CURLOPT_CUSTOMREQUEST,
     263             :                                      MHD_HTTP_METHOD_PATCH));
     264           0 :     iph->job = GNUNET_CURL_job_add2 (ctx,
     265             :                                      eh,
     266           0 :                                      iph->post_ctx.headers,
     267             :                                      &handle_patch_instance_finished,
     268             :                                      iph);
     269             :   }
     270           0 :   return iph;
     271             : }
     272             : 
     273             : 
     274             : void
     275           0 : TALER_MERCHANT_instance_patch_cancel (
     276             :   struct TALER_MERCHANT_InstancePatchHandle *iph)
     277             : {
     278           0 :   if (NULL != iph->job)
     279             :   {
     280           0 :     GNUNET_CURL_job_cancel (iph->job);
     281           0 :     iph->job = NULL;
     282             :   }
     283           0 :   TALER_curl_easy_post_finished (&iph->post_ctx);
     284           0 :   GNUNET_free (iph->url);
     285           0 :   GNUNET_free (iph);
     286           0 : }
     287             : 
     288             : 
     289             : /* end of merchant_api_patch_instance.c */

Generated by: LCOV version 1.14