LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 91 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 <taler/taler_json_lib.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a PATCH /instances/$ID operation.
      37             :  */
      38             : struct TALER_MERCHANT_InstancePatchHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_InstancePatchCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : 
      66             :   /**
      67             :    * Minor context that holds body and headers.
      68             :    */
      69             :   struct TALER_CURL_PostContext post_ctx;
      70             : 
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * HTTP PATCH /instances/$ID request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_InstancePatchHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param json response body, NULL if not in JSON
      81             :  */
      82             : static void
      83           0 : handle_patch_instance_finished (void *cls,
      84             :                                 long response_code,
      85             :                                 const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_InstancePatchHandle *iph = cls;
      88           0 :   const json_t *json = response;
      89           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      90           0 :     .http_status = (unsigned int) response_code,
      91             :     .reply = json
      92             :   };
      93             : 
      94           0 :   iph->job = NULL;
      95           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      96             :               "PATCH /instances/$ID completed with response code %u\n",
      97             :               (unsigned int) response_code);
      98           0 :   switch (response_code)
      99             :   {
     100           0 :   case 0:
     101           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     102           0 :     break;
     103           0 :   case MHD_HTTP_NO_CONTENT:
     104           0 :     break;
     105           0 :   case MHD_HTTP_BAD_REQUEST:
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108           0 :     GNUNET_break_op (0);
     109             :     /* This should never happen, either us
     110             :      * or the merchant is buggy (or API version conflict);
     111             :      * just pass JSON reply to the application */
     112           0 :     break;
     113           0 :   case MHD_HTTP_FORBIDDEN:
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116             :     /* Nothing really to verify, merchant says we tried to abort the payment
     117             :      * after it was successful. We should pass the JSON reply to the
     118             :      * application */
     119           0 :     break;
     120           0 :   case MHD_HTTP_NOT_FOUND:
     121           0 :     hr.ec = TALER_JSON_get_error_code (json);
     122           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     123           0 :     break;
     124           0 :   case MHD_HTTP_CONFLICT:
     125           0 :     hr.ec = TALER_JSON_get_error_code (json);
     126           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     127           0 :     break;
     128           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     129           0 :     hr.ec = TALER_JSON_get_error_code (json);
     130           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     131             :     /* Server had an internal issue; we should retry,
     132             :        but this API leaves this to the application */
     133           0 :     break;
     134           0 :   default:
     135           0 :     TALER_MERCHANT_parse_error_details_ (json,
     136             :                                          response_code,
     137             :                                          &hr);
     138             :     /* unexpected response code */
     139           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     140             :                 "Unexpected response code %u/%d\n",
     141             :                 (unsigned int) response_code,
     142             :                 (int) hr.ec);
     143           0 :     GNUNET_break_op (0);
     144           0 :     break;
     145             :   }
     146           0 :   iph->cb (iph->cb_cls,
     147             :            &hr);
     148           0 :   TALER_MERCHANT_instance_patch_cancel (iph);
     149           0 : }
     150             : 
     151             : 
     152             : /**
     153             :  * Update instance configuration.
     154             :  *
     155             :  * @param ctx the context
     156             :  * @param backend_url HTTP base URL for the backend
     157             :  * @param instance_id identity of the instance to get information about
     158             :  * @param payto_uris_length length of the @a accounts array
     159             :  * @param payto_uris URIs of the bank accounts of the merchant instance
     160             :  * @param name name of the merchant instance
     161             :  * @param address physical address of the merchant instance
     162             :  * @param jurisdiction jurisdiction of the merchant instance
     163             :  * @param default_max_wire_fee default maximum wire fee merchant is willing to fully pay
     164             :  * @param default_wire_fee_amortization default amortization factor for excess wire fees
     165             :  * @param default_max_deposit_fee default maximum deposit fee merchant is willing to pay
     166             :  * @param default_wire_transfer_delay default wire transfer delay merchant will ask for
     167             :  * @param default_pay_delay default validity period for offers merchant makes
     168             :  * @param cb function to call with the
     169             :  *        backend's instances information
     170             :  * @param cb_cls closure for @a config_cb
     171             :  * @return the instances handle; NULL upon error
     172             :  */
     173             : struct TALER_MERCHANT_InstancePatchHandle *
     174           0 : TALER_MERCHANT_instance_patch (
     175             :   struct GNUNET_CURL_Context *ctx,
     176             :   const char *backend_url,
     177             :   const char *instance_id,
     178             :   unsigned int accounts_length,
     179             :   const char *payto_uris[],
     180             :   const char *name,
     181             :   const json_t *address,
     182             :   const json_t *jurisdiction,
     183             :   const struct TALER_Amount *default_max_wire_fee,
     184             :   uint32_t default_wire_fee_amortization,
     185             :   const struct TALER_Amount *default_max_deposit_fee,
     186             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     187             :   struct GNUNET_TIME_Relative default_pay_delay,
     188             :   TALER_MERCHANT_InstancePatchCallback cb,
     189             :   void *cb_cls)
     190             : {
     191             :   struct TALER_MERCHANT_InstancePatchHandle *iph;
     192             :   json_t *jpayto_uris;
     193             :   json_t *req_obj;
     194             : 
     195           0 :   jpayto_uris = json_array ();
     196           0 :   if (NULL == jpayto_uris)
     197             :   {
     198           0 :     GNUNET_break (0);
     199           0 :     return NULL;
     200             :   }
     201           0 :   for (unsigned int i = 0; i<accounts_length; i++)
     202             :   {
     203           0 :     if (0 !=
     204           0 :         json_array_append_new (jpayto_uris,
     205           0 :                                json_string (payto_uris[i])))
     206             :     {
     207           0 :       GNUNET_break (0);
     208           0 :       json_decref (jpayto_uris);
     209           0 :       return NULL;
     210             :     }
     211             :   }
     212           0 :   req_obj = json_pack ("{s:o, s:s, s:O, s:O"
     213             :                        " s:o, s:I: s:o, s:o, s:o}",
     214             :                        "payto_uris",
     215             :                        jpayto_uris,
     216             :                        "name",
     217             :                        name,
     218             :                        "address",
     219             :                        address,
     220             :                        "jurisdiction",
     221             :                        jurisdiction,
     222             :                        /* end of group of 4 */
     223             :                        "default_max_wire_fee",
     224             :                        TALER_JSON_from_amount (default_max_wire_fee),
     225             :                        "default_wire_fee_amortization",
     226             :                        (json_int_t) default_wire_fee_amortization,
     227             :                        "default_max_deposit_fee",
     228             :                        TALER_JSON_from_amount (default_max_deposit_fee),
     229             :                        "default_wire_transfer_delay",
     230             :                        GNUNET_JSON_from_time_rel (default_wire_transfer_delay),
     231             :                        "default_pay_delay",
     232             :                        GNUNET_JSON_from_time_rel (default_pay_delay));
     233           0 :   if (NULL == req_obj)
     234             :   {
     235           0 :     GNUNET_break (0);
     236           0 :     return NULL;
     237             :   }
     238           0 :   iph = GNUNET_new (struct TALER_MERCHANT_InstancePatchHandle);
     239           0 :   iph->ctx = ctx;
     240           0 :   iph->cb = cb;
     241           0 :   iph->cb_cls = cb_cls;
     242             :   {
     243             :     char *path;
     244             : 
     245           0 :     GNUNET_asprintf (&path,
     246             :                      "private/instances/%s",
     247             :                      instance_id);
     248           0 :     iph->url = TALER_url_join (backend_url,
     249             :                                path,
     250             :                                NULL);
     251           0 :     GNUNET_free (path);
     252             :   }
     253           0 :   if (NULL == iph->url)
     254             :   {
     255           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     256             :                 "Could not construct request URL.\n");
     257           0 :     json_decref (req_obj);
     258           0 :     GNUNET_free (iph);
     259           0 :     return NULL;
     260             :   }
     261             :   {
     262             :     CURL *eh;
     263             : 
     264           0 :     eh = curl_easy_init ();
     265           0 :     if (GNUNET_OK !=
     266           0 :         TALER_curl_easy_post (&iph->post_ctx,
     267             :                               eh,
     268             :                               req_obj))
     269             :     {
     270           0 :       GNUNET_break (0);
     271           0 :       json_decref (req_obj);
     272           0 :       GNUNET_free (iph);
     273           0 :       return NULL;
     274             :     }
     275             : 
     276           0 :     json_decref (req_obj);
     277           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     278             :                                                  CURLOPT_URL,
     279             :                                                  iph->url));
     280           0 :     GNUNET_assert (CURLE_OK ==
     281             :                    curl_easy_setopt (eh,
     282             :                                      CURLOPT_CUSTOMREQUEST,
     283             :                                      MHD_HTTP_METHOD_PATCH));
     284           0 :     iph->job = GNUNET_CURL_job_add2 (ctx,
     285             :                                      eh,
     286           0 :                                      iph->post_ctx.headers,
     287             :                                      &handle_patch_instance_finished,
     288             :                                      iph);
     289             :   }
     290           0 :   return iph;
     291             : }
     292             : 
     293             : 
     294             : /**
     295             :  * Cancel PATCH /instances/$ID request.  Must not be called by clients after
     296             :  * the callback was invoked.
     297             :  *
     298             :  * @param iph request to cancel.
     299             :  */
     300             : void
     301           0 : TALER_MERCHANT_instance_patch_cancel (
     302             :   struct TALER_MERCHANT_InstancePatchHandle *iph)
     303             : {
     304           0 :   if (NULL != iph->job)
     305             :   {
     306           0 :     GNUNET_CURL_job_cancel (iph->job);
     307           0 :     iph->job = NULL;
     308             :   }
     309           0 :   TALER_curl_easy_post_finished (&iph->post_ctx);
     310           0 :   GNUNET_free (iph->url);
     311           0 :   GNUNET_free (iph);
     312           0 : }
     313             : 
     314             : 
     315             : /* end of merchant_api_patch_instance.c */

Generated by: LCOV version 1.14