LCOV - code coverage report
Current view: top level - lib - merchant_api_wallet_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 62 0.0 %
Date: 2022-08-25 06:17:04 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 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A handle for tracking /tip-get operations
      36             :  */
      37             : struct TALER_MERCHANT_TipWalletGetHandle
      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_TipWalletGetCallback 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             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP /track/transaction request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_TipGetHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param response response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_wallet_tip_get_finished (void *cls,
      77             :                                 long response_code,
      78             :                                 const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_TipWalletGetHandle *tgh = 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 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      88             :               "Got /tip/$TIP_ID response with status code %u\n",
      89             :               (unsigned int) response_code);
      90             : 
      91           0 :   tgh->job = NULL;
      92           0 :   switch (response_code)
      93             :   {
      94           0 :   case MHD_HTTP_OK:
      95             :     {
      96             :       const char *exchange_url;
      97             :       struct TALER_Amount amount_remaining;
      98             :       struct GNUNET_TIME_Timestamp expiration;
      99             :       struct GNUNET_JSON_Specification spec[] = {
     100           0 :         GNUNET_JSON_spec_timestamp ("expiration",
     101             :                                     &expiration),
     102           0 :         GNUNET_JSON_spec_string ("exchange_url",
     103             :                                  &exchange_url),
     104           0 :         TALER_JSON_spec_amount_any ("tip_amount",
     105             :                                     &amount_remaining),
     106           0 :         GNUNET_JSON_spec_end ()
     107             :       };
     108             : 
     109           0 :       if (GNUNET_OK !=
     110           0 :           GNUNET_JSON_parse (json,
     111             :                              spec,
     112             :                              NULL, NULL))
     113             :       {
     114           0 :         GNUNET_break_op (0);
     115           0 :         hr.http_status = 0;
     116           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     117           0 :         break;
     118             :       }
     119           0 :       tgh->cb (tgh->cb_cls,
     120             :                &hr,
     121             :                expiration,
     122             :                exchange_url,
     123             :                &amount_remaining);
     124           0 :       TALER_MERCHANT_wallet_tip_get_cancel (tgh);
     125           0 :       return;
     126             :     }
     127           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     128             :     /* Server had an internal issue; we should retry, but this API
     129             :        leaves this to the application */
     130           0 :     hr.ec = TALER_JSON_get_error_code (json);
     131           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     132           0 :     break;
     133           0 :   case MHD_HTTP_NOT_FOUND:
     134             :     /* legal, can happen if instance or tip reserve is unknown */
     135           0 :     hr.ec = TALER_JSON_get_error_code (json);
     136           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     137           0 :     break;
     138           0 :   default:
     139             :     /* unexpected response code */
     140           0 :     GNUNET_break_op (0);
     141           0 :     TALER_MERCHANT_parse_error_details_ (json,
     142             :                                          response_code,
     143             :                                          &hr);
     144           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     145             :                 "Unexpected response code %u/%d\n",
     146             :                 (unsigned int) response_code,
     147             :                 (int) hr.ec);
     148           0 :     break;
     149             :   }
     150           0 :   tgh->cb (tgh->cb_cls,
     151             :            &hr,
     152           0 :            GNUNET_TIME_UNIT_ZERO_TS,
     153             :            NULL,
     154             :            NULL);
     155           0 :   TALER_MERCHANT_wallet_tip_get_cancel (tgh);
     156             : }
     157             : 
     158             : 
     159             : struct TALER_MERCHANT_TipWalletGetHandle *
     160           0 : TALER_MERCHANT_wallet_tip_get (struct GNUNET_CURL_Context *ctx,
     161             :                                const char *backend_url,
     162             :                                const struct TALER_TipIdentifierP *tip_id,
     163             :                                TALER_MERCHANT_TipWalletGetCallback cb,
     164             :                                void *cb_cls)
     165             : {
     166             :   struct TALER_MERCHANT_TipWalletGetHandle *tgh;
     167             :   CURL *eh;
     168             : 
     169           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipWalletGetHandle);
     170           0 :   tgh->ctx = ctx;
     171           0 :   tgh->cb = cb;
     172           0 :   tgh->cb_cls = cb_cls;
     173             :   {
     174             :     char res_str[sizeof (*tip_id) * 2];
     175             :     char arg_str[sizeof (res_str) + 48];
     176             :     char *end;
     177             : 
     178           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     179             :                                          sizeof (*tip_id),
     180             :                                          res_str,
     181             :                                          sizeof (res_str));
     182           0 :     *end = '\0';
     183           0 :     GNUNET_snprintf (arg_str,
     184             :                      sizeof (arg_str),
     185             :                      "tips/%s",
     186             :                      res_str);
     187           0 :     tgh->url = TALER_url_join (backend_url,
     188             :                                arg_str,
     189             :                                NULL);
     190             :   }
     191             : 
     192           0 :   if (NULL == tgh->url)
     193             :   {
     194           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     195             :                 "Could not construct request URL.\n");
     196           0 :     GNUNET_free (tgh);
     197           0 :     return NULL;
     198             :   }
     199             : 
     200           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     201             :               "Requesting URL '%s'\n",
     202             :               tgh->url);
     203           0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     204           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     205             :                                   eh,
     206             :                                   &handle_wallet_tip_get_finished,
     207             :                                   tgh);
     208           0 :   return tgh;
     209             : }
     210             : 
     211             : 
     212             : void
     213           0 : TALER_MERCHANT_wallet_tip_get_cancel (
     214             :   struct TALER_MERCHANT_TipWalletGetHandle *tgh)
     215             : {
     216           0 :   if (NULL != tgh->job)
     217             :   {
     218           0 :     GNUNET_CURL_job_cancel (tgh->job);
     219           0 :     tgh->job = NULL;
     220             :   }
     221           0 :   GNUNET_free (tgh->url);
     222           0 :   GNUNET_free (tgh);
     223           0 : }
     224             : 
     225             : 
     226             : /* end of merchant_api_wallet_get_tip.c */

Generated by: LCOV version 1.14