LCOV - code coverage report
Current view: top level - lib - merchant_api_wallet_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 62 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-2018, 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_wallet_get_tip.c
      19             :  * @brief Implementation of the GET /tips/$TIP_ID 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-get operations
      35             :  */
      36             : struct TALER_MERCHANT_TipWalletGetHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_TipWalletGetCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Function called when we're done processing the
      68             :  * HTTP /track/transaction request.
      69             :  *
      70             :  * @param cls the `struct TALER_MERCHANT_TipGetHandle`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param json response body, NULL if not in JSON
      73             :  */
      74             : static void
      75           0 : handle_wallet_tip_get_finished (void *cls,
      76             :                                 long response_code,
      77             :                                 const void *response)
      78             : {
      79           0 :   struct TALER_MERCHANT_TipWalletGetHandle *tgh = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      87             :               "Got /tip/$TIP_ID response with status code %u\n",
      88             :               (unsigned int) response_code);
      89             : 
      90           0 :   tgh->job = NULL;
      91           0 :   switch (response_code)
      92             :   {
      93           0 :   case MHD_HTTP_OK:
      94             :     {
      95             :       const char *exchange_url;
      96             :       struct TALER_Amount amount_remaining;
      97             :       struct GNUNET_TIME_Absolute expiration;
      98             :       struct GNUNET_JSON_Specification spec[] = {
      99           0 :         TALER_JSON_spec_absolute_time ("expiration",
     100             :                                        &expiration),
     101           0 :         GNUNET_JSON_spec_string ("exchange_url",
     102             :                                  &exchange_url),
     103           0 :         TALER_JSON_spec_amount ("tip_amount",
     104             :                                 &amount_remaining),
     105           0 :         GNUNET_JSON_spec_end ()
     106             :       };
     107             : 
     108           0 :       if (GNUNET_OK !=
     109           0 :           GNUNET_JSON_parse (json,
     110             :                              spec,
     111             :                              NULL, NULL))
     112             :       {
     113           0 :         GNUNET_break_op (0);
     114           0 :         hr.http_status = 0;
     115           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     116           0 :         break;
     117             :       }
     118           0 :       tgh->cb (tgh->cb_cls,
     119             :                &hr,
     120             :                expiration,
     121             :                exchange_url,
     122             :                &amount_remaining);
     123           0 :       TALER_MERCHANT_wallet_tip_get_cancel (tgh);
     124           0 :       return;
     125             :     }
     126           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     127             :     /* Server had an internal issue; we should retry, but this API
     128             :        leaves this to the application */
     129           0 :     hr.ec = TALER_JSON_get_error_code (json);
     130           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     131           0 :     break;
     132           0 :   case MHD_HTTP_NOT_FOUND:
     133             :     /* legal, can happen if instance or tip reserve is unknown */
     134           0 :     hr.ec = TALER_JSON_get_error_code (json);
     135           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     136           0 :     break;
     137           0 :   default:
     138             :     /* unexpected response code */
     139           0 :     GNUNET_break_op (0);
     140           0 :     TALER_MERCHANT_parse_error_details_ (json,
     141             :                                          response_code,
     142             :                                          &hr);
     143           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     144             :                 "Unexpected response code %u/%d\n",
     145             :                 (unsigned int) response_code,
     146             :                 (int) hr.ec);
     147           0 :     break;
     148             :   }
     149           0 :   tgh->cb (tgh->cb_cls,
     150             :            &hr,
     151             :            GNUNET_TIME_UNIT_ZERO_ABS,
     152             :            NULL,
     153             :            NULL);
     154           0 :   TALER_MERCHANT_wallet_tip_get_cancel (tgh);
     155             : }
     156             : 
     157             : 
     158             : /**
     159             :  * Issue a GET /tips/$TIP_ID request to the backend.  Informs the backend
     160             :  * that a customer wants to pick up a tip.
     161             :  *
     162             :  * @param ctx execution context
     163             :  * @param backend_url base URL of the merchant backend
     164             :  * @param tip_id which tip should we query
     165             :  * @param cb function to call with the result
     166             :  * @param cb_cls closure for @a cb
     167             :  * @return handle for this operation, NULL upon errors
     168             :  */
     169             : struct TALER_MERCHANT_TipWalletGetHandle *
     170           0 : TALER_MERCHANT_wallet_tip_get (struct GNUNET_CURL_Context *ctx,
     171             :                                const char *backend_url,
     172             :                                const struct GNUNET_HashCode *tip_id,
     173             :                                TALER_MERCHANT_TipWalletGetCallback cb,
     174             :                                void *cb_cls)
     175             : {
     176             :   struct TALER_MERCHANT_TipWalletGetHandle *tgh;
     177             :   CURL *eh;
     178             : 
     179           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipWalletGetHandle);
     180           0 :   tgh->ctx = ctx;
     181           0 :   tgh->cb = cb;
     182           0 :   tgh->cb_cls = cb_cls;
     183             :   {
     184             :     char res_str[sizeof (*tip_id) * 2];
     185             :     char arg_str[sizeof (res_str) + 48];
     186             :     char *end;
     187             : 
     188           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     189             :                                          sizeof (*tip_id),
     190             :                                          res_str,
     191             :                                          sizeof (res_str));
     192           0 :     *end = '\0';
     193           0 :     GNUNET_snprintf (arg_str,
     194             :                      sizeof (arg_str),
     195             :                      "tips/%s",
     196             :                      res_str);
     197           0 :     tgh->url = TALER_url_join (backend_url,
     198             :                                arg_str,
     199             :                                NULL);
     200             :   }
     201             : 
     202           0 :   if (NULL == tgh->url)
     203             :   {
     204           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     205             :                 "Could not construct request URL.\n");
     206           0 :     GNUNET_free (tgh);
     207           0 :     return NULL;
     208             :   }
     209             : 
     210           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     211             :               "Requesting URL '%s'\n",
     212             :               tgh->url);
     213             : 
     214           0 :   eh = curl_easy_init ();
     215           0 :   GNUNET_assert (CURLE_OK ==
     216             :                  curl_easy_setopt (eh,
     217             :                                    CURLOPT_URL,
     218             :                                    tgh->url));
     219             : 
     220           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     221             :                                   eh,
     222             :                                   &handle_wallet_tip_get_finished,
     223             :                                   tgh);
     224           0 :   return tgh;
     225             : }
     226             : 
     227             : 
     228             : /**
     229             :  * Cancel a GET /tips/$TIP_ID request.  This function cannot be used
     230             :  * on a request handle if a response is already served for it.
     231             :  *
     232             :  * @param tqo handle to the operation being cancelled
     233             :  */
     234             : void
     235           0 : TALER_MERCHANT_wallet_tip_get_cancel (
     236             :   struct TALER_MERCHANT_TipWalletGetHandle *tgh)
     237             : {
     238           0 :   if (NULL != tgh->job)
     239             :   {
     240           0 :     GNUNET_CURL_job_cancel (tgh->job);
     241           0 :     tgh->job = NULL;
     242             :   }
     243           0 :   GNUNET_free (tgh->url);
     244           0 :   GNUNET_free (tgh);
     245           0 : }
     246             : 
     247             : 
     248             : /* end of merchant_api_wallet_get_tip.c */

Generated by: LCOV version 1.14