LCOV - code coverage report
Current view: top level - lib - merchant_api_get_reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 96 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-2017, 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_get_reserves.c
      19             :  * @brief Implementation of the GET /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 <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A Handle for tracking wire reserves.
      36             :  */
      37             : struct TALER_MERCHANT_ReservesGetHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_ReservesGetCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP GET /reserves request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_ReservesGetHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param json response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_reserves_get_finished (void *cls,
      77             :                               long response_code,
      78             :                               const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_ReservesGetHandle *rgh = cls;
      81           0 :   const json_t *json = response;
      82           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      83           0 :     .http_status = (unsigned int) response_code,
      84             :     .reply = json
      85             :   };
      86             : 
      87           0 :   rgh->job = NULL;
      88           0 :   switch (response_code)
      89             :   {
      90           0 :   case 0:
      91           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      92           0 :     break;
      93           0 :   case MHD_HTTP_OK:
      94             :     {
      95             :       json_t *reserves;
      96             :       struct GNUNET_JSON_Specification spec[] = {
      97           0 :         GNUNET_JSON_spec_json ("reserves",
      98             :                                &reserves),
      99           0 :         GNUNET_JSON_spec_end ()
     100             :       };
     101             : 
     102           0 :       if (GNUNET_OK !=
     103           0 :           GNUNET_JSON_parse (json,
     104             :                              spec,
     105             :                              NULL, NULL))
     106             :       {
     107           0 :         GNUNET_break_op (0);
     108           0 :         hr.http_status = 0;
     109           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     110           0 :         break;
     111             :       }
     112             :       else
     113             :       {
     114             :         size_t rds_length;
     115             :         struct TALER_MERCHANT_ReserveSummary *rds;
     116             :         json_t *reserve;
     117             :         unsigned int i;
     118             :         bool ok;
     119             : 
     120           0 :         if (! json_is_array (reserves))
     121             :         {
     122           0 :           GNUNET_break_op (0);
     123           0 :           GNUNET_JSON_parse_free (spec);
     124           0 :           hr.http_status = 0;
     125           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     126           0 :           break;
     127             :         }
     128           0 :         rds_length = json_array_size (reserves);
     129           0 :         rds = GNUNET_new_array (rds_length,
     130             :                                 struct TALER_MERCHANT_ReserveSummary);
     131           0 :         ok = true;
     132           0 :         json_array_foreach (reserves, i, reserve) {
     133           0 :           struct TALER_MERCHANT_ReserveSummary *rd = &rds[i];
     134             :           struct GNUNET_JSON_Specification ispec[] = {
     135           0 :             GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     136             :                                          &rd->reserve_pub),
     137           0 :             TALER_JSON_spec_absolute_time ("creation_time",
     138             :                                            &rd->creation_time),
     139           0 :             TALER_JSON_spec_absolute_time ("expiration_time",
     140             :                                            &rd->expiration_time),
     141           0 :             TALER_JSON_spec_amount ("merchant_initial_amount",
     142             :                                     &rd->merchant_initial_amount),
     143           0 :             TALER_JSON_spec_amount ("exchange_initial_amount",
     144             :                                     &rd->exchange_initial_amount),
     145           0 :             TALER_JSON_spec_amount ("pickup_amount",
     146             :                                     &rd->pickup_amount),
     147           0 :             TALER_JSON_spec_amount ("committed_amount",
     148             :                                     &rd->committed_amount),
     149           0 :             GNUNET_JSON_spec_bool ("active",
     150             :                                    &rd->active),
     151           0 :             GNUNET_JSON_spec_end ()
     152             :           };
     153             : 
     154           0 :           if (GNUNET_OK !=
     155           0 :               GNUNET_JSON_parse (reserve,
     156             :                                  ispec,
     157             :                                  NULL, NULL))
     158             :           {
     159           0 :             GNUNET_break_op (0);
     160           0 :             ok = false;
     161           0 :             break;
     162             :           }
     163             :         }
     164             : 
     165           0 :         if (! ok)
     166             :         {
     167           0 :           GNUNET_break_op (0);
     168           0 :           GNUNET_free (rds);
     169           0 :           GNUNET_JSON_parse_free (spec);
     170           0 :           hr.http_status = 0;
     171           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     172           0 :           break;
     173             :         }
     174           0 :         rgh->cb (rgh->cb_cls,
     175             :                  &hr,
     176             :                  rds_length,
     177             :                  rds);
     178           0 :         GNUNET_free (rds);
     179           0 :         GNUNET_JSON_parse_free (spec);
     180           0 :         TALER_MERCHANT_reserves_get_cancel (rgh);
     181           0 :         return;
     182             :       }
     183             :     }
     184           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     185             :     /* Server had an internal issue; we should retry, but this API
     186             :        leaves this to the application */
     187           0 :     hr.ec = TALER_JSON_get_error_code (json);
     188           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     189           0 :     break;
     190           0 :   default:
     191             :     /* unexpected response code */
     192           0 :     GNUNET_break_op (0);
     193           0 :     TALER_MERCHANT_parse_error_details_ (json,
     194             :                                          response_code,
     195             :                                          &hr);
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     197             :                 "Unexpected response code %u/%d\n",
     198             :                 (unsigned int) response_code,
     199             :                 (int) hr.ec);
     200           0 :     response_code = 0;
     201           0 :     break;
     202             :   }
     203           0 :   rgh->cb (rgh->cb_cls,
     204             :            &hr,
     205             :            0,
     206             :            NULL);
     207           0 :   TALER_MERCHANT_reserves_get_cancel (rgh);
     208             : }
     209             : 
     210             : 
     211             : /**
     212             :  * Issue a GET /reserves request to the backend.  Informs the backend
     213             :  * that a customer wants to pick up a reserves.
     214             :  *
     215             :  * @param ctx execution context
     216             :  * @param backend_url base URL of the merchant backend
     217             :  * @param after filter for reserves created after this date, use 0 for no filtering
     218             :  * @param active filter for reserves that are active
     219             :  * @param failures filter for reserves where we disagree about the balance with
     220             :  *        the exchange
     221             :  * @param cb function to call with the result(s)
     222             :  * @param cb_cls closure for @a cb
     223             :  * @return handle for this operation, NULL upon errors
     224             :  */
     225             : struct TALER_MERCHANT_ReservesGetHandle *
     226           0 : TALER_MERCHANT_reserves_get (struct GNUNET_CURL_Context *ctx,
     227             :                              const char *backend_url,
     228             :                              struct GNUNET_TIME_Absolute after,
     229             :                              enum TALER_EXCHANGE_YesNoAll active,
     230             :                              enum TALER_EXCHANGE_YesNoAll failures,
     231             :                              TALER_MERCHANT_ReservesGetCallback cb,
     232             :                              void *cb_cls)
     233             : {
     234             :   struct TALER_MERCHANT_ReservesGetHandle *rgh;
     235             :   CURL *eh;
     236           0 :   const char *active_s = NULL;
     237           0 :   const char *failures_s = NULL;
     238             :   char *after_s;
     239             : 
     240           0 :   rgh = GNUNET_new (struct TALER_MERCHANT_ReservesGetHandle);
     241           0 :   rgh->ctx = ctx;
     242           0 :   rgh->cb = cb;
     243           0 :   rgh->cb_cls = cb_cls;
     244           0 :   active_s = TALER_yna_to_string (active);
     245           0 :   failures_s = TALER_yna_to_string (failures);
     246           0 :   after_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (
     247             :                              after));
     248           0 :   rgh->url = TALER_url_join (backend_url,
     249             :                              "private/reserves",
     250             :                              "active",
     251             :                              active_s,
     252             :                              "failures",
     253             :                              failures_s,
     254             :                              "after",
     255           0 :                              after.abs_value_us != 0
     256             :                              ? after_s
     257             :                              : NULL,
     258             :                              NULL);
     259           0 :   GNUNET_free (after_s);
     260           0 :   if (NULL == rgh->url)
     261             :   {
     262           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     263             :                 "Could not construct request URL.\n");
     264           0 :     GNUNET_free (rgh);
     265           0 :     return NULL;
     266             :   }
     267           0 :   eh = curl_easy_init ();
     268           0 :   GNUNET_assert (CURLE_OK ==
     269             :                  curl_easy_setopt (eh,
     270             :                                    CURLOPT_URL,
     271             :                                    rgh->url));
     272           0 :   rgh->job = GNUNET_CURL_job_add (ctx,
     273             :                                   eh,
     274             :                                   &handle_reserves_get_finished,
     275             :                                   rgh);
     276           0 :   return rgh;
     277             : }
     278             : 
     279             : 
     280             : /**
     281             :  * Cancel a GET /reserves request.
     282             :  *
     283             :  * @param rgh handle to the request to be canceled
     284             :  */
     285             : void
     286           0 : TALER_MERCHANT_reserves_get_cancel (
     287             :   struct TALER_MERCHANT_ReservesGetHandle *rgh)
     288             : {
     289           0 :   if (NULL != rgh->job)
     290             :   {
     291           0 :     GNUNET_CURL_job_cancel (rgh->job);
     292           0 :     rgh->job = NULL;
     293             :   }
     294           0 :   GNUNET_free (rgh->url);
     295           0 :   GNUNET_free (rgh);
     296           0 : }
     297             : 
     298             : 
     299             : /* end of merchant_api_get_reserves.c */

Generated by: LCOV version 1.14