LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_query.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 32 45 71.1 %
Date: 2018-07-14 06:17:23 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 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_tip_query.c
      19             :  * @brief Implementation of the /tip-query request of the merchant's HTTP API
      20             :  * @author Florian Dold
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include <taler/taler_json_lib.h>
      30             : #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * @brief A handle for tracking /tip-pickup operations
      35             :  */
      36             : struct TALER_MERCHANT_TipQueryOperation
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * JSON encoding of the request to POST.
      45             :    */
      46             :   char *json_enc;
      47             : 
      48             :   /**
      49             :    * Handle for the request.
      50             :    */
      51             :   struct GNUNET_CURL_Job *job;
      52             : 
      53             :   /**
      54             :    * Function to call with the result.
      55             :    */
      56             :   TALER_MERCHANT_TipQueryCallback cb;
      57             : 
      58             :   /**
      59             :    * Closure for @a cb.
      60             :    */
      61             :   void *cb_cls;
      62             : 
      63             :   /**
      64             :    * Reference to the execution context.
      65             :    */
      66             :   struct GNUNET_CURL_Context *ctx;
      67             : 
      68             :   /**
      69             :    * Expected number of planchets.
      70             :    */
      71             :   unsigned int num_planchets;
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * We got a 200 response back from the exchange (or the merchant).
      77             :  * Now we need to parse the response and if it is well-formed,
      78             :  * call the callback (and set it to NULL afterwards).
      79             :  *
      80             :  * @param tqo handle of the original operation
      81             :  * @param json cryptographic proof returned by the exchange/merchant
      82             :  * @return #GNUNET_OK if response is valid
      83             :  */
      84             : static int
      85           8 : check_ok (struct TALER_MERCHANT_TipQueryOperation *tqo,
      86             :           const json_t *json)
      87             : {
      88             :   struct GNUNET_TIME_Absolute reserve_expiration;
      89             :   struct TALER_Amount amount_authorized;
      90             :   struct TALER_Amount amount_available;
      91             :   struct TALER_Amount amount_picked_up;
      92             :   struct TALER_ReservePublicKeyP reserve_pub;
      93           8 :   struct GNUNET_JSON_Specification spec[] = {
      94             :     GNUNET_JSON_spec_fixed_auto ("reserve_pub", &reserve_pub),
      95             :     GNUNET_JSON_spec_absolute_time ("reserve_expiration", &reserve_expiration),
      96             :     TALER_JSON_spec_amount ("amount_authorized", &amount_authorized),
      97             :     TALER_JSON_spec_amount ("amount_available", &amount_available),
      98             :     TALER_JSON_spec_amount ("amount_picked_up", &amount_picked_up),
      99             :     GNUNET_JSON_spec_end()
     100             :   };
     101             : 
     102           8 :   if (GNUNET_OK !=
     103           8 :       GNUNET_JSON_parse (json,
     104             :                          spec,
     105             :                          NULL, NULL))
     106             :   {
     107           0 :     GNUNET_break_op (0);
     108           0 :     return GNUNET_SYSERR;
     109             :   }
     110           8 :   tqo->cb (tqo->cb_cls,
     111             :            MHD_HTTP_OK,
     112             :            TALER_JSON_get_error_code (json),
     113             :            json,
     114             :            reserve_expiration,
     115             :            &reserve_pub,
     116             :            &amount_authorized,
     117             :            &amount_available,
     118             :            &amount_picked_up);
     119           8 :   return GNUNET_OK;
     120             : }
     121             : 
     122             : 
     123             : /**
     124             :  * Function called when we're done processing the
     125             :  * HTTP /track/transaction request.
     126             :  *
     127             :  * @param cls the `struct TALER_MERCHANT_TipQueryOperation`
     128             :  * @param response_code HTTP response code, 0 on error
     129             :  * @param json response body, NULL if not in JSON
     130             :  */
     131             : static void
     132           8 : handle_tip_query_finished (void *cls,
     133             :                             long response_code,
     134             :                             const json_t *json)
     135             : {
     136           8 :   struct TALER_MERCHANT_TipQueryOperation *tqo = cls;
     137             : 
     138           8 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     139             :               "Got /tip-query response with status code %u\n",
     140             :               (unsigned int) response_code);
     141             : 
     142           8 :   tqo->job = NULL;
     143           8 :   switch (response_code)
     144             :   {
     145             :   case MHD_HTTP_OK:
     146           8 :     if (GNUNET_OK != check_ok (tqo,
     147             :                                json))
     148             :     {
     149           0 :       GNUNET_break_op (0);
     150           0 :       response_code = 0;
     151             :     }
     152           8 :     break;
     153             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     154             :     /* Server had an internal issue; we should retry, but this API
     155             :        leaves this to the application */
     156           0 :     break;
     157             :   case MHD_HTTP_NOT_FOUND:
     158             :     /* legal, can happen if instance or tip reserve is unknown */
     159           0 :     break;
     160             :   default:
     161             :     /* unexpected response code */
     162           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     163             :                 "Unexpected response code %u\n",
     164             :                 (unsigned int) response_code);
     165           0 :     GNUNET_break (0);
     166           0 :     response_code = 0;
     167           0 :     break;
     168             :   }
     169           8 :   if (MHD_HTTP_OK != response_code)
     170           0 :     tqo->cb (tqo->cb_cls,
     171             :              response_code,
     172             :              TALER_JSON_get_error_code (json),
     173             :              json,
     174             :              GNUNET_TIME_UNIT_ZERO_ABS,
     175             :              NULL,
     176             :              NULL,
     177             :              NULL,
     178             :              NULL);
     179           8 :   TALER_MERCHANT_tip_query_cancel (tqo);
     180           8 : }
     181             : 
     182             : 
     183             : /**
     184             :  * Issue a /tip-query request to the backend.  Informs the backend
     185             :  * that a customer wants to pick up a tip.
     186             :  *
     187             :  * @param ctx execution context
     188             :  * @param backend_url base URL of the merchant backend
     189             :  * @param instance instance to query
     190             :  * @return handle for this operation, NULL upon errors
     191             :  */
     192             : struct TALER_MERCHANT_TipQueryOperation *
     193           8 : TALER_MERCHANT_tip_query (struct GNUNET_CURL_Context *ctx,
     194             :                           const char *backend_url,
     195             :                           const char *instance,
     196             :                           TALER_MERCHANT_TipQueryCallback query_cb,
     197             :                           void *query_cb_cls)
     198             : {
     199             :   struct TALER_MERCHANT_TipQueryOperation *tqo;
     200             :   CURL *eh;
     201             : 
     202           8 :   tqo = GNUNET_new (struct TALER_MERCHANT_TipQueryOperation);
     203           8 :   tqo->ctx = ctx;
     204           8 :   tqo->cb = query_cb;
     205           8 :   tqo->cb_cls = query_cb_cls;
     206           8 :   tqo->url = TALER_url_join (backend_url, "/tip-query",
     207             :                              "instance", instance,
     208             :                              NULL);
     209           8 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     210             :               "Requesting URL '%s'\n",
     211             :               tqo->url);
     212           8 :   eh = curl_easy_init ();
     213           8 :   GNUNET_assert (CURLE_OK ==
     214             :                  curl_easy_setopt (eh,
     215             :                                    CURLOPT_URL,
     216             :                                    tqo->url));
     217           8 :   tqo->job = GNUNET_CURL_job_add (ctx,
     218             :                                   eh,
     219             :                                   GNUNET_YES,
     220             :                                   &handle_tip_query_finished,
     221             :                                   tqo);
     222           8 :   return tqo;
     223             : }
     224             : 
     225             : 
     226             : /**
     227             :  * Cancel a /tip-query request.  This function cannot be used
     228             :  * on a request handle if a response is already served for it.
     229             :  *
     230             :  * @param tqo handle to the operation being cancelled
     231             :  */
     232             : void
     233           8 : TALER_MERCHANT_tip_query_cancel (struct TALER_MERCHANT_TipQueryOperation *tqo)
     234             : {
     235           8 :   if (NULL != tqo->job)
     236             :   {
     237           0 :     GNUNET_CURL_job_cancel (tqo->job);
     238           0 :     tqo->job = NULL;
     239             :   }
     240           8 :   GNUNET_free (tqo->url);
     241           8 :   GNUNET_free (tqo);
     242           8 : }
     243             : 
     244             : /* end of merchant_api_tip_query.c */

Generated by: LCOV version 1.13