LCOV - code coverage report
Current view: top level - lib - merchant_api_merchant_get_tip.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 99 0.0 %
Date: 2022-08-25 06:17:04 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 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : struct TALER_MERCHANT_TipMerchantGetHandle
      35             : {
      36             :   /**
      37             :    * The url for this request.
      38             :    */
      39             :   char *url;
      40             : 
      41             :   /**
      42             :    * Handle for the request.
      43             :    */
      44             :   struct GNUNET_CURL_Job *job;
      45             : 
      46             :   /**
      47             :    * Function to call with the result.
      48             :    */
      49             :   TALER_MERCHANT_TipMerchantGetCallback cb;
      50             : 
      51             :   /**
      52             :    * Closure for @a cb.
      53             :    */
      54             :   void *cb_cls;
      55             : 
      56             :   /**
      57             :    * Reference to the execution context.
      58             :    */
      59             :   struct GNUNET_CURL_Context *ctx;
      60             : };
      61             : 
      62             : 
      63             : static enum GNUNET_GenericReturnValue
      64           0 : parse_pickups (const json_t *pa,
      65             :                struct TALER_MERCHANT_TipStatusResponse *tsr,
      66             :                struct TALER_MERCHANT_TipMerchantGetHandle *tgh)
      67           0 : {
      68           0 :   unsigned int pa_len = json_array_size (pa);
      69           0 :   struct TALER_MERCHANT_PickupDetail pickups[pa_len];
      70             :   size_t index;
      71             :   json_t *value;
      72             : 
      73           0 :   json_array_foreach (pa, index, value)
      74             :   {
      75           0 :     struct TALER_MERCHANT_PickupDetail *pickup = &pickups[index];
      76             :     struct GNUNET_JSON_Specification spec[] = {
      77           0 :       GNUNET_JSON_spec_fixed_auto ("pickup_id",
      78             :                                    &pickup->pickup_id),
      79           0 :       GNUNET_JSON_spec_uint64 ("num_planchets",
      80             :                                &pickup->num_planchets),
      81           0 :       TALER_JSON_spec_amount_any ("requested_amount",
      82             :                                   &pickup->requested_amount),
      83           0 :       GNUNET_JSON_spec_end ()
      84             :     };
      85             : 
      86           0 :     if (GNUNET_OK !=
      87           0 :         GNUNET_JSON_parse (value,
      88             :                            spec,
      89             :                            NULL,
      90             :                            NULL))
      91             :     {
      92           0 :       GNUNET_break_op (0);
      93           0 :       return GNUNET_SYSERR;
      94             :     }
      95             :   }
      96           0 :   tsr->details.success.pickups_length = pa_len;
      97           0 :   tsr->details.success.pickups = pickups;
      98           0 :   tgh->cb (tgh->cb_cls,
      99             :            tsr);
     100           0 :   return GNUNET_OK;
     101             : }
     102             : 
     103             : 
     104             : /**
     105             :  * Function called when we're done processing the
     106             :  * GET /private/tips/$TIP_ID request.
     107             :  *
     108             :  * @param cls the `struct TALER_MERCHANT_TipMerchantGetHandle`
     109             :  * @param response_code HTTP response code, 0 on error
     110             :  * @param response response body, NULL if not in JSON
     111             :  */
     112             : static void
     113           0 : handle_merchant_tip_get_finished (void *cls,
     114             :                                   long response_code,
     115             :                                   const void *response)
     116             : {
     117           0 :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh = cls;
     118           0 :   const json_t *json = response;
     119           0 :   struct TALER_MERCHANT_TipStatusResponse tsr = {
     120           0 :     .hr.http_status = (unsigned int) response_code,
     121             :     .hr.reply = json
     122             :   };
     123             : 
     124           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     125             :               "Got /private/tips/$TIP_ID response with status code %u\n",
     126             :               (unsigned int) response_code);
     127           0 :   tgh->job = NULL;
     128           0 :   switch (response_code)
     129             :   {
     130           0 :   case MHD_HTTP_OK:
     131             :     {
     132             :       struct GNUNET_JSON_Specification spec[] = {
     133           0 :         TALER_JSON_spec_amount_any ("total_authorized",
     134             :                                     &tsr.details.success.total_authorized),
     135           0 :         TALER_JSON_spec_amount_any ("total_picked_up",
     136             :                                     &tsr.details.success.total_picked_up),
     137           0 :         GNUNET_JSON_spec_string ("reason",
     138             :                                  &tsr.details.success.reason),
     139           0 :         GNUNET_JSON_spec_timestamp ("expiration",
     140             :                                     &tsr.details.success.expiration),
     141           0 :         GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     142             :                                      &tsr.details.success.reserve_pub),
     143           0 :         GNUNET_JSON_spec_end ()
     144             :       };
     145             : 
     146           0 :       if (GNUNET_OK !=
     147           0 :           GNUNET_JSON_parse (json,
     148             :                              spec,
     149             :                              NULL, NULL))
     150             :       {
     151           0 :         tsr.hr.http_status = 0;
     152           0 :         tsr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     153             :       }
     154             :       else
     155             :       {
     156           0 :         json_t *pickups = json_object_get (json,
     157             :                                            "pickups");
     158           0 :         if (! json_is_array (pickups))
     159             :         {
     160           0 :           tgh->cb (tgh->cb_cls,
     161             :                    &tsr);
     162           0 :           TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     163           0 :           return;
     164             :         }
     165           0 :         if (GNUNET_OK ==
     166           0 :             parse_pickups (pickups,
     167             :                            &tsr,
     168             :                            tgh))
     169             :         {
     170           0 :           GNUNET_JSON_parse_free (spec);
     171           0 :           TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     172           0 :           return;
     173             :         }
     174           0 :         tsr.hr.http_status = 0;
     175           0 :         tsr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     176             :       }
     177           0 :       GNUNET_JSON_parse_free (spec);
     178           0 :       break;
     179             :     }
     180           0 :   case MHD_HTTP_UNAUTHORIZED:
     181           0 :     tsr.hr.ec = TALER_JSON_get_error_code (json);
     182           0 :     tsr.hr.hint = TALER_JSON_get_error_hint (json);
     183             :     /* Nothing really to verify, merchant says we need to authenticate. */
     184           0 :     break;
     185           0 :   case MHD_HTTP_NOT_FOUND:
     186             :     /* legal, can happen if instance or tip reserve is unknown */
     187           0 :     tsr.hr.ec = TALER_JSON_get_error_code (json);
     188           0 :     tsr.hr.hint = TALER_JSON_get_error_hint (json);
     189           0 :     break;
     190           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     191             :     /* Server had an internal issue; we should retry, but this API
     192             :        leaves this to the application */
     193           0 :     tsr.hr.ec = TALER_JSON_get_error_code (json);
     194           0 :     tsr.hr.hint = TALER_JSON_get_error_hint (json);
     195           0 :     break;
     196           0 :   default:
     197             :     /* unexpected response code */
     198           0 :     GNUNET_break_op (0);
     199           0 :     TALER_MERCHANT_parse_error_details_ (json,
     200             :                                          response_code,
     201             :                                          &tsr.hr);
     202           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     203             :                 "Unexpected response code %u/%d\n",
     204             :                 (unsigned int) response_code,
     205             :                 (int) tsr.hr.ec);
     206           0 :     break;
     207             :   }
     208           0 :   tgh->cb (tgh->cb_cls,
     209             :            &tsr);
     210           0 :   TALER_MERCHANT_merchant_tip_get_cancel (tgh);
     211             : }
     212             : 
     213             : 
     214             : struct TALER_MERCHANT_TipMerchantGetHandle *
     215           0 : TALER_MERCHANT_merchant_tip_get (struct GNUNET_CURL_Context *ctx,
     216             :                                  const char *backend_url,
     217             :                                  const struct TALER_TipIdentifierP *tip_id,
     218             :                                  const struct TALER_Amount *min_pick_up,
     219             :                                  struct GNUNET_TIME_Relative lp_timeout,
     220             :                                  bool pickups,
     221             :                                  TALER_MERCHANT_TipMerchantGetCallback cb,
     222             :                                  void *cb_cls)
     223             : {
     224             :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh;
     225             :   CURL *eh;
     226             : 
     227           0 :   GNUNET_assert (NULL != backend_url);
     228           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipMerchantGetHandle);
     229           0 :   tgh->ctx = ctx;
     230           0 :   tgh->cb = cb;
     231           0 :   tgh->cb_cls = cb_cls;
     232             : 
     233             :   {
     234             :     char res_str[sizeof (*tip_id) * 2];
     235             :     char arg_str[sizeof (res_str) + 48];
     236             :     char timeout_str[32];
     237             :     char *end;
     238             : 
     239           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     240             :                                          sizeof (*tip_id),
     241             :                                          res_str,
     242             :                                          sizeof (res_str));
     243           0 :     *end = '\0';
     244           0 :     GNUNET_snprintf (arg_str,
     245             :                      sizeof (arg_str),
     246             :                      "private/tips/%s",
     247             :                      res_str);
     248           0 :     GNUNET_snprintf (timeout_str,
     249             :                      sizeof (timeout_str),
     250             :                      "%llu",
     251             :                      ((unsigned long long)
     252           0 :                       lp_timeout.rel_value_us /
     253           0 :                       GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us));
     254           0 :     tgh->url = TALER_url_join (backend_url,
     255             :                                arg_str,
     256             :                                "pickups",
     257             :                                pickups
     258             :                                ? "yes"
     259             :                                : NULL,
     260             :                                "min_amount",
     261             :                                min_pick_up
     262           0 :                                ? TALER_amount2s (min_pick_up)
     263             :                                : NULL,
     264             :                                "timeout_ms",
     265           0 :                                GNUNET_TIME_relative_is_zero (lp_timeout)
     266             :                                ? NULL
     267             :                                : timeout_str,
     268             :                                NULL);
     269             :   }
     270           0 :   if (NULL == tgh->url)
     271             :   {
     272           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     273             :                 "Could not construct request URL.\n");
     274           0 :     GNUNET_free (tgh);
     275           0 :     return NULL;
     276             :   }
     277             : 
     278           0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     279           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     280             :                                   eh,
     281             :                                   &handle_merchant_tip_get_finished,
     282             :                                   tgh);
     283           0 :   return tgh;
     284             : }
     285             : 
     286             : 
     287             : void
     288           0 : TALER_MERCHANT_merchant_tip_get_cancel (
     289             :   struct TALER_MERCHANT_TipMerchantGetHandle *tgh)
     290             : {
     291           0 :   if (NULL != tgh->job)
     292             :   {
     293           0 :     GNUNET_CURL_job_cancel (tgh->job);
     294           0 :     tgh->job = NULL;
     295             :   }
     296           0 :   GNUNET_free (tgh->url);
     297           0 :   GNUNET_free (tgh);
     298           0 : }
     299             : 
     300             : 
     301             : /* end of merchant_api_merchant_get_tip.c */

Generated by: LCOV version 1.14