LCOV - code coverage report
Current view: top level - lib - merchant_api_post_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 74 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) 2014, 2015, 2016, 2020 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 lib/merchant_api_post_reserves.c
      19             :  * @brief Implementation of the POST /reserves request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include <taler/taler_curl_lib.h>
      30             : #include <taler/taler_json_lib.h>
      31             : 
      32             : 
      33             : /**
      34             :  * @brief A handle for POSTing reserve data.
      35             :  */
      36             : struct TALER_MERCHANT_PostReservesHandle
      37             : {
      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_PostReservesCallback 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             :    * Minor context that holds body and headers.
      66             :    */
      67             :   struct TALER_CURL_PostContext post_ctx;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Function called when we're done processing the
      74             :  * HTTP POST /reserves request.
      75             :  *
      76             :  * @param cls the `struct TALER_MERCHANT_PostReservesHandle`
      77             :  * @param response_code HTTP response code, 0 on error
      78             :  * @param json response body, NULL if not in JSON
      79             :  */
      80             : static void
      81           0 : handle_post_reserves_finished (void *cls,
      82             :                                long response_code,
      83             :                                const void *response)
      84             : {
      85           0 :   struct TALER_MERCHANT_PostReservesHandle *prh = cls;
      86           0 :   const json_t *json = response;
      87           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      88           0 :     .http_status = (unsigned int) response_code,
      89             :     .reply = json
      90             :   };
      91             : 
      92           0 :   prh->job = NULL;
      93           0 :   switch (response_code)
      94             :   {
      95           0 :   case 0:
      96           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      97           0 :     break;
      98           0 :   case MHD_HTTP_OK:
      99             :     {
     100             :       const char *payto_uri;
     101             :       struct TALER_ReservePublicKeyP reserve_pub;
     102             :       struct GNUNET_JSON_Specification spec[] = {
     103           0 :         GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     104             :                                      &reserve_pub),
     105           0 :         GNUNET_JSON_spec_string ("payto_uri",
     106             :                                  &payto_uri),
     107           0 :         GNUNET_JSON_spec_end ()
     108             :       };
     109             : 
     110           0 :       if (GNUNET_OK !=
     111           0 :           GNUNET_JSON_parse (json,
     112             :                              spec,
     113             :                              NULL, NULL))
     114             :       {
     115           0 :         GNUNET_break_op (0);
     116           0 :         hr.http_status = 0;
     117           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     118           0 :         break;
     119             :       }
     120             :       else
     121             :       {
     122           0 :         prh->cb (prh->cb_cls,
     123             :                  &hr,
     124             :                  &reserve_pub,
     125             :                  payto_uri);
     126           0 :         GNUNET_JSON_parse_free (spec);
     127           0 :         TALER_MERCHANT_reserves_post_cancel (prh);
     128           0 :         return;
     129             :       }
     130             :     }
     131           0 :   case MHD_HTTP_ACCEPTED:
     132           0 :     break;
     133           0 :   case MHD_HTTP_NOT_FOUND:
     134             :     /* Nothing really to verify, this should never
     135             :        happen, we should pass the JSON reply to the application */
     136           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     137             :                 "Did not find any data\n");
     138           0 :     hr.ec = TALER_JSON_get_error_code (json);
     139           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     140           0 :     break;
     141           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     142             :     /* Server had an internal issue; we should retry, but this API
     143             :        leaves this to the application */
     144           0 :     hr.ec = TALER_JSON_get_error_code (json);
     145           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     146           0 :     break;
     147           0 :   default:
     148             :     /* unexpected response code */
     149           0 :     GNUNET_break_op (0);
     150           0 :     TALER_MERCHANT_parse_error_details_ (json,
     151             :                                          response_code,
     152             :                                          &hr);
     153           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     154             :                 "Unexpected response code %u/%d\n",
     155             :                 (unsigned int) response_code,
     156             :                 (int) hr.ec);
     157           0 :     break;
     158             :   }
     159           0 :   prh->cb (prh->cb_cls,
     160             :            &hr,
     161             :            NULL,
     162             :            NULL);
     163           0 :   TALER_MERCHANT_reserves_post_cancel (prh);
     164             : }
     165             : 
     166             : 
     167             : /**
     168             :  * Request backend to create a reserve.
     169             :  *
     170             :  * @param ctx execution context
     171             :  * @param backend_url base URL of the backend
     172             :  * @param initial_balance desired initial balance for the reserve
     173             :  * @param exchange_url what is the URL of the exchange where the reserve should be set up
     174             :  * @param wire_method desired wire method, for example "iban" or "x-taler-bank"
     175             :  * @param cb the callback to call when a reply for this request is available
     176             :  * @param cb_cls closure for @a cb
     177             :  * @return a handle for this request
     178             :  */
     179             : struct TALER_MERCHANT_PostReservesHandle *
     180           0 : TALER_MERCHANT_reserves_post (
     181             :   struct GNUNET_CURL_Context *ctx,
     182             :   const char *backend_url,
     183             :   const struct TALER_Amount *initial_balance,
     184             :   const char *exchange_url,
     185             :   const char *wire_method,
     186             :   TALER_MERCHANT_PostReservesCallback cb,
     187             :   void *cb_cls)
     188             : {
     189             :   struct TALER_MERCHANT_PostReservesHandle *prh;
     190             :   CURL *eh;
     191             :   json_t *req;
     192             : 
     193           0 :   prh = GNUNET_new (struct TALER_MERCHANT_PostReservesHandle);
     194           0 :   prh->ctx = ctx;
     195           0 :   prh->cb = cb;
     196           0 :   prh->cb_cls = cb_cls;
     197           0 :   prh->url = TALER_url_join (backend_url,
     198             :                              "private/reserves",
     199             :                              NULL);
     200           0 :   if (NULL == prh->url)
     201             :   {
     202           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     203             :                 "Could not construct request URL.\n");
     204           0 :     GNUNET_free (prh);
     205           0 :     return NULL;
     206             :   }
     207           0 :   req = json_pack ("{s:o, s:s, s:s}",
     208             :                    "initial_balance", TALER_JSON_from_amount (initial_balance),
     209             :                    "wire_method", wire_method,
     210             :                    "exchange_url", exchange_url);
     211           0 :   GNUNET_assert (NULL != req);
     212           0 :   eh = curl_easy_init ();
     213           0 :   GNUNET_assert (NULL != eh);
     214           0 :   if (GNUNET_OK != TALER_curl_easy_post (&prh->post_ctx,
     215             :                                          eh,
     216             :                                          req))
     217             :   {
     218           0 :     GNUNET_break (0);
     219           0 :     json_decref (req);
     220           0 :     GNUNET_free (prh);
     221           0 :     return NULL;
     222             :   }
     223           0 :   json_decref (req);
     224           0 :   GNUNET_assert (CURLE_OK ==
     225             :                  curl_easy_setopt (eh,
     226             :                                    CURLOPT_URL,
     227             :                                    prh->url));
     228           0 :   prh->job = GNUNET_CURL_job_add2 (ctx,
     229             :                                    eh,
     230           0 :                                    prh->post_ctx.headers,
     231             :                                    &handle_post_reserves_finished,
     232             :                                    prh);
     233           0 :   return prh;
     234             : }
     235             : 
     236             : 
     237             : /**
     238             :  * Cancel a POST /reserves request.  This function cannot be used
     239             :  * on a request handle if a response is already served for it.
     240             :  *
     241             :  * @param prh handle to the tracking operation being cancelled
     242             :  */
     243             : void
     244           0 : TALER_MERCHANT_reserves_post_cancel (
     245             :   struct TALER_MERCHANT_PostReservesHandle *prh)
     246             : {
     247           0 :   if (NULL != prh->job)
     248             :   {
     249           0 :     GNUNET_CURL_job_cancel (prh->job);
     250           0 :     prh->job = NULL;
     251             :   }
     252           0 :   GNUNET_free (prh->url);
     253           0 :   TALER_curl_easy_post_finished (&prh->post_ctx);
     254           0 :   GNUNET_free (prh);
     255           0 : }
     256             : 
     257             : 
     258             : /* end of merchant_api_post_reserves.c */

Generated by: LCOV version 1.14