LCOV - code coverage report
Current view: top level - lib - merchant_api_merchant_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 92 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 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_merchant_get_tip.c
      19             :  * @brief Implementation of the GET /private/tips/$TIP_ID request of the merchant's HTTP API
      20             :  * @author Jonathan Buchanan
      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             : struct TALER_MERCHANT_TipMerchantGetHandle
      34             : {
      35             :   /**
      36             :    * The url for this request.
      37             :    */
      38             :   char *url;
      39             : 
      40             :   /**
      41             :    * Handle for the request.
      42             :    */
      43             :   struct GNUNET_CURL_Job *job;
      44             : 
      45             :   /**
      46             :    * Function to call with the result.
      47             :    */
      48             :   TALER_MERCHANT_TipMerchantGetCallback cb;
      49             : 
      50             :   /**
      51             :    * Closure for @a cb.
      52             :    */
      53             :   void *cb_cls;
      54             : 
      55             :   /**
      56             :    * Reference to the execution context.
      57             :    */
      58             :   struct GNUNET_CURL_Context *ctx;
      59             : };
      60             : 
      61             : 
      62             : static int
      63           0 : parse_pickups (const json_t *pa,
      64             :                const struct TALER_Amount *total_authorized,
      65             :                const struct TALER_Amount *total_picked_up,
      66             :                const char *reason,
      67             :                struct GNUNET_TIME_Absolute expiration,
      68             :                const struct TALER_ReservePublicKeyP *reserve_pub,
      69             :                struct TALER_MERCHANT_TipMerchantGetHandle *tgh)
      70           0 : {
      71           0 :   unsigned int pa_len = json_array_size (pa);
      72           0 :   struct TALER_MERCHANT_PickupDetail pickups[pa_len];
      73             :   size_t index;
      74             :   json_t *value;
      75           0 :   int ret = GNUNET_OK;
      76             : 
      77           0 :   json_array_foreach (pa, index, value)
      78             :   {
      79           0 :     struct TALER_MERCHANT_PickupDetail *pickup = &pickups[index];
      80             :     struct GNUNET_JSON_Specification spec[] = {
      81           0 :       GNUNET_JSON_spec_fixed_auto ("pickup_id",
      82             :                                    &pickup->pickup_id),
      83           0 :       GNUNET_JSON_spec_uint64 ("num_planchets",
      84             :                                &pickup->num_planchets),
      85           0 :       TALER_JSON_spec_amount ("requested_amount",
      86             :                               &pickup->requested_amount),
      87           0 :       GNUNET_JSON_spec_end ()
      88             :     };
      89             : 
      90           0 :     if (GNUNET_OK !=
      91           0 :         GNUNET_JSON_parse (value,
      92             :                            spec,
      93             :                            NULL,
      94             :                            NULL))
      95             :     {
      96           0 :       GNUNET_break_op (0);
      97           0 :       ret = GNUNET_SYSERR;
      98           0 :       break;
      99             :     }
     100             :   }
     101           0 :   if (GNUNET_OK == ret)
     102             :   {
     103           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     104             :       .http_status = MHD_HTTP_OK
     105             :     };
     106             : 
     107           0 :     tgh->cb (tgh->cb_cls,
     108             :              &hr,
     109             :              total_authorized,
     110             :              total_picked_up,
     111             :              reason,
     112             :              expiration,
     113             :              reserve_pub,
     114             :              pa_len,
     115             :              pickups);
     116             :   }
     117           0 :   return ret;
     118             : }
     119             : 
     120             : 
     121             : /**
     122             :  * Function called when we're done processing the
     123             :  * GET /private/tips/$TIP_ID request.
     124             :  *
     125             :  * @param cls the `struct TALER_MERCHANT_TipMerchantGetHandle`
     126             :  * @param response_code HTTP response code, 0 on error
     127             :  * @param json response body, NULL if not in JSON
     128             :  */
     129             : static void
     130           0 : handle_merchant_tip_get_finished (void *cls,
     131             :                                   long response_code,
     132             :                                   const void *response)
     133             : {
     134           0 :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh = cls;
     135           0 :   const json_t *json = response;
     136           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     137           0 :     .http_status = (unsigned int) response_code,
     138             :     .reply = json
     139             :   };
     140             : 
     141           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     142             :               "Got /private/tips/$TIP_ID response with status code %u\n",
     143             :               (unsigned int) response_code);
     144           0 :   tgh->job = NULL;
     145           0 :   switch (response_code)
     146             :   {
     147           0 :   case MHD_HTTP_OK:
     148             :     {
     149             :       struct TALER_Amount total_authorized;
     150             :       struct TALER_Amount total_picked_up;
     151             :       const char *reason;
     152             :       struct GNUNET_TIME_Absolute expiration;
     153             :       struct TALER_ReservePublicKeyP reserve_pub;
     154             :       struct GNUNET_JSON_Specification spec[] = {
     155           0 :         TALER_JSON_spec_amount ("total_authorized",
     156             :                                 &total_authorized),
     157           0 :         TALER_JSON_spec_amount ("total_picked_up",
     158             :                                 &total_picked_up),
     159           0 :         GNUNET_JSON_spec_string ("reason",
     160             :                                  &reason),
     161           0 :         TALER_JSON_spec_absolute_time ("expiration",
     162             :                                        &expiration),
     163           0 :         GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     164             :                                      &reserve_pub),
     165           0 :         GNUNET_JSON_spec_end ()
     166             :       };
     167             : 
     168           0 :       if (GNUNET_OK !=
     169           0 :           GNUNET_JSON_parse (json,
     170             :                              spec,
     171             :                              NULL, NULL))
     172             :       {
     173           0 :         hr.http_status = 0;
     174           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     175             :       }
     176             :       else
     177             :       {
     178           0 :         json_t *pickups = json_object_get (json,
     179             :                                            "pickups");
     180           0 :         if (! json_is_array (pickups))
     181             :         {
     182           0 :           tgh->cb (tgh->cb_cls,
     183             :                    &hr,
     184             :                    &total_authorized,
     185             :                    &total_picked_up,
     186             :                    reason,
     187             :                    expiration,
     188             :                    &reserve_pub,
     189             :                    0,
     190             :                    NULL);
     191           0 :           TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     192           0 :           return;
     193             :         }
     194           0 :         else if (GNUNET_OK == parse_pickups (pickups,
     195             :                                              &total_authorized,
     196             :                                              &total_picked_up,
     197             :                                              reason,
     198             :                                              expiration,
     199             :                                              &reserve_pub,
     200             :                                              tgh))
     201             :         {
     202           0 :           GNUNET_JSON_parse_free (spec);
     203           0 :           TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     204           0 :           return;
     205             :         }
     206             :         else
     207             :         {
     208           0 :           hr.http_status = 0;
     209           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     210             :         }
     211             :       }
     212           0 :       GNUNET_JSON_parse_free (spec);
     213           0 :       break;
     214             :     }
     215           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     216             :     /* Server had an internal issue; we should retry, but this API
     217             :        leaves this to the application */
     218           0 :     hr.ec = TALER_JSON_get_error_code (json);
     219           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     220           0 :     break;
     221           0 :   case MHD_HTTP_NOT_FOUND:
     222             :     /* legal, can happen if instance or tip reserve is unknown */
     223           0 :     hr.ec = TALER_JSON_get_error_code (json);
     224           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     225           0 :     break;
     226           0 :   default:
     227             :     /* unexpected response code */
     228           0 :     GNUNET_break_op (0);
     229           0 :     TALER_MERCHANT_parse_error_details_ (json,
     230             :                                          response_code,
     231             :                                          &hr);
     232           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     233             :                 "Unexpected response code %u/%d\n",
     234             :                 (unsigned int) response_code,
     235             :                 (int) hr.ec);
     236           0 :     break;
     237             :   }
     238           0 :   tgh->cb (tgh->cb_cls,
     239             :            &hr,
     240             :            NULL,
     241             :            NULL,
     242             :            NULL,
     243             :            GNUNET_TIME_UNIT_ZERO_ABS,
     244             :            NULL,
     245             :            0,
     246             :            NULL);
     247           0 :   TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     248             : }
     249             : 
     250             : 
     251             : /**
     252             :  * Issue a GET /private/tips/$TIP_ID (private variant) request to the backend.
     253             :  * Returns information needed to pick up a tip.
     254             :  *
     255             :  * @param ctx execution context
     256             :  * @param backend_url base URL of the merchant backend
     257             :  * @param tip_id which tip should we query
     258             :  * @param cb function to call with the result
     259             :  * @param cb_cls closure for @a cb
     260             :  * @return handle for this operation, NULL upon errors
     261             :  */
     262             : struct TALER_MERCHANT_TipMerchantGetHandle *
     263           0 : TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx,
     264             :                                  const char *backend_url,
     265             :                                  const struct GNUNET_HashCode *tip_id,
     266             :                                  bool pickups,
     267             :                                  TALER_MERCHANT_TipMerchantGetCallback cb,
     268             :                                  void *cb_cls)
     269             : {
     270             :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh;
     271             :   CURL *eh;
     272             : 
     273           0 :   GNUNET_assert (NULL != backend_url);
     274           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipMerchantGetHandle);
     275           0 :   tgh->ctx = ctx;
     276           0 :   tgh->cb = cb;
     277           0 :   tgh->cb_cls = cb_cls;
     278             : 
     279             :   {
     280             :     char res_str[sizeof (*tip_id) * 2];
     281             :     char arg_str[sizeof (res_str) + 48];
     282             :     char *end;
     283             : 
     284           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     285             :                                          sizeof (*tip_id),
     286             :                                          res_str,
     287             :                                          sizeof (res_str));
     288           0 :     *end = '\0';
     289           0 :     GNUNET_snprintf (arg_str,
     290             :                      sizeof (arg_str),
     291             :                      "private/tips/%s",
     292             :                      res_str);
     293           0 :     tgh->url = TALER_url_join (backend_url,
     294             :                                arg_str,
     295             :                                "pickups", pickups ? "yes" : NULL,
     296             :                                NULL);
     297             :   }
     298           0 :   if (NULL == tgh->url)
     299             :   {
     300           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     301             :                 "Could not construct request URL.\n");
     302           0 :     GNUNET_free (tgh);
     303           0 :     return NULL;
     304             :   }
     305             : 
     306           0 :   eh = curl_easy_init ();
     307           0 :   GNUNET_assert (CURLE_OK ==
     308             :                  curl_easy_setopt (eh,
     309             :                                    CURLOPT_URL,
     310             :                                    tgh->url));
     311           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     312             :                                   eh,
     313             :                                   &handle_merchant_tip_get_finished,
     314             :                                   tgh);
     315           0 :   return tgh;
     316             : }
     317             : 
     318             : 
     319             : /**
     320             :  * Cancel a GET /private/tips/$TIP_ID request.
     321             :  *
     322             :  * @param tgh handle to the request to be canceled
     323             :  */
     324             : void
     325           0 : TALER_MERCHANT_merchant_tip_get_cancel (struct
     326             :                                         TALER_MERCHANT_TipMerchantGetHandle *tgh)
     327             : {
     328           0 :   if (NULL != tgh->job)
     329             :   {
     330           0 :     GNUNET_CURL_job_cancel (tgh->job);
     331           0 :     tgh->job = NULL;
     332             :   }
     333           0 :   GNUNET_free (tgh->url);
     334           0 :   GNUNET_free (tgh);
     335           0 : }
     336             : 
     337             : 
     338             : /* end of merchant_api_merchant_get_tip.c */

Generated by: LCOV version 1.14