LCOV - code coverage report
Current view: top level - lib - merchant_api_get_reserve.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 96 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-2017, 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_get_reserve.c
      19             :  * @brief Implementation of the GET /reserve request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A Handle for tracking wire reserve.
      36             :  */
      37             : struct TALER_MERCHANT_ReserveGetHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_MERCHANT_ReserveGetCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP GET /reserve request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_ReserveGetHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param json response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_reserve_get_finished (void *cls,
      77             :                              long response_code,
      78             :                              const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_ReserveGetHandle *rgh = 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             :   bool active;
      87             : 
      88           0 :   rgh->job = NULL;
      89           0 :   switch (response_code)
      90             :   {
      91           0 :   case 0:
      92           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      93           0 :     break;
      94           0 :   case MHD_HTTP_OK:
      95             :     {
      96             :       struct TALER_MERCHANT_ReserveSummary rs;
      97             :       const json_t *tips;
      98             :       struct GNUNET_JSON_Specification spec[] = {
      99           0 :         TALER_JSON_spec_absolute_time ("creation_time",
     100             :                                        &rs.creation_time),
     101           0 :         TALER_JSON_spec_absolute_time ("expiration_time",
     102             :                                        &rs.expiration_time),
     103           0 :         GNUNET_JSON_spec_bool ("active",
     104             :                                &active),
     105           0 :         TALER_JSON_spec_amount ("merchant_initial_amount",
     106             :                                 &rs.merchant_initial_amount),
     107           0 :         TALER_JSON_spec_amount ("exchange_initial_amount",
     108             :                                 &rs.exchange_initial_amount),
     109           0 :         TALER_JSON_spec_amount ("pickup_amount",
     110             :                                 &rs.pickup_amount),
     111           0 :         TALER_JSON_spec_amount ("committed_amount",
     112             :                                 &rs.committed_amount),
     113           0 :         GNUNET_JSON_spec_end ()
     114             :       };
     115             : 
     116           0 :       if (GNUNET_OK !=
     117           0 :           GNUNET_JSON_parse (json,
     118             :                              spec,
     119             :                              NULL, NULL))
     120             :       {
     121           0 :         GNUNET_break_op (0);
     122           0 :         hr.http_status = 0;
     123           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     124           0 :         break;
     125             :       }
     126             : 
     127           0 :       tips = json_object_get (json,
     128             :                               "tips");
     129           0 :       if ((NULL == tips) ||
     130           0 :           json_is_null (tips))
     131             :       {
     132           0 :         rgh->cb (rgh->cb_cls,
     133             :                  &hr,
     134             :                  &rs,
     135             :                  false,
     136             :                  0,
     137             :                  NULL);
     138           0 :         TALER_MERCHANT_reserve_get_cancel (rgh);
     139           0 :         return;
     140             :       }
     141           0 :       if (! json_is_array (tips))
     142             :       {
     143           0 :         GNUNET_break_op (0);
     144           0 :         hr.http_status = 0;
     145           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     146           0 :         break;
     147             :       }
     148             :       {
     149             :         size_t tds_length;
     150             :         json_t *tip;
     151             :         struct TALER_MERCHANT_TipDetails *tds;
     152             :         unsigned int i;
     153             :         bool ok;
     154             : 
     155           0 :         tds_length = json_array_size (tips);
     156           0 :         tds = GNUNET_new_array (tds_length,
     157             :                                 struct TALER_MERCHANT_TipDetails);
     158           0 :         ok = true;
     159           0 :         json_array_foreach (tips, i, tip) {
     160           0 :           struct TALER_MERCHANT_TipDetails *td = &tds[i];
     161             :           struct GNUNET_JSON_Specification ispec[] = {
     162           0 :             GNUNET_JSON_spec_fixed_auto ("tip_id",
     163             :                                          &td->tip_id),
     164           0 :             TALER_JSON_spec_amount ("total_amount",
     165             :                                     &td->amount),
     166           0 :             GNUNET_JSON_spec_string ("reason",
     167             :                                      &td->reason),
     168           0 :             GNUNET_JSON_spec_end ()
     169             :           };
     170             : 
     171           0 :           if (GNUNET_OK !=
     172           0 :               GNUNET_JSON_parse (tip,
     173             :                                  ispec,
     174             :                                  NULL, NULL))
     175             :           {
     176           0 :             GNUNET_break_op (0);
     177           0 :             ok = false;
     178           0 :             break;
     179             :           }
     180             :         }
     181             : 
     182           0 :         if (! ok)
     183             :         {
     184           0 :           GNUNET_break_op (0);
     185           0 :           GNUNET_free (tds);
     186           0 :           hr.http_status = 0;
     187           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     188           0 :           break;
     189             :         }
     190           0 :         rgh->cb (rgh->cb_cls,
     191             :                  &hr,
     192             :                  &rs,
     193             :                  active,
     194             :                  tds_length,
     195             :                  tds);
     196           0 :         GNUNET_free (tds);
     197           0 :         TALER_MERCHANT_reserve_get_cancel (rgh);
     198           0 :         return;
     199             :       }
     200             :     }
     201           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     202             :     /* Server had an internal issue; we should retry, but this API
     203             :        leaves this to the application */
     204           0 :     hr.ec = TALER_JSON_get_error_code (json);
     205           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     206           0 :     break;
     207           0 :   default:
     208             :     /* unexpected response code */
     209           0 :     GNUNET_break_op (0);
     210           0 :     TALER_MERCHANT_parse_error_details_ (json,
     211             :                                          response_code,
     212             :                                          &hr);
     213           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     214             :                 "Unexpected response code %u/%d\n",
     215             :                 (unsigned int) response_code,
     216             :                 (int) hr.ec);
     217           0 :     response_code = 0;
     218           0 :     break;
     219             :   }
     220           0 :   rgh->cb (rgh->cb_cls,
     221             :            &hr,
     222             :            NULL,
     223             :            false,
     224             :            0,
     225             :            NULL);
     226           0 :   TALER_MERCHANT_reserve_get_cancel (rgh);
     227             : }
     228             : 
     229             : 
     230             : /**
     231             :  * Issue a GET /reserve/$RESERVE_ID request to the backend.  Queries the backend
     232             :  * about the status of a reserve.
     233             :  *
     234             :  * @param ctx execution context
     235             :  * @param backend_url base URL of the merchant backend
     236             :  * @param reserve_pub which reserve should be queried
     237             :  * @param tips should we return details about the tips issued from the reserve
     238             :  * @param cb function to call with the result(s)
     239             :  * @param cb_cls closure for @a cb
     240             :  * @return handle for this operation, NULL upon errors
     241             :  */
     242             : struct TALER_MERCHANT_ReserveGetHandle *
     243           0 : TALER_MERCHANT_reserve_get (struct GNUNET_CURL_Context *ctx,
     244             :                             const char *backend_url,
     245             :                             const struct TALER_ReservePublicKeyP *reserve_pub,
     246             :                             bool fetch_tips,
     247             :                             TALER_MERCHANT_ReserveGetCallback cb,
     248             :                             void *cb_cls)
     249             : {
     250             :   struct TALER_MERCHANT_ReserveGetHandle *rgh;
     251             :   CURL *eh;
     252             : 
     253           0 :   rgh = GNUNET_new (struct TALER_MERCHANT_ReserveGetHandle);
     254           0 :   rgh->ctx = ctx;
     255           0 :   rgh->cb = cb;
     256           0 :   rgh->cb_cls = cb_cls;
     257             :   {
     258             :     char res_str[sizeof (*reserve_pub) * 2];
     259             :     char arg_str[sizeof (res_str) + 32];
     260             :     char *end;
     261             : 
     262           0 :     end = GNUNET_STRINGS_data_to_string (reserve_pub,
     263             :                                          sizeof (*reserve_pub),
     264             :                                          res_str,
     265             :                                          sizeof (res_str));
     266           0 :     *end = '\0';
     267           0 :     GNUNET_snprintf (arg_str,
     268             :                      sizeof (arg_str),
     269             :                      "private/reserves/%s",
     270             :                      res_str);
     271           0 :     rgh->url = TALER_url_join (backend_url,
     272             :                                arg_str,
     273             :                                "tips",
     274             :                                fetch_tips ? "yes" : "no",
     275             :                                NULL);
     276             :   }
     277           0 :   if (NULL == rgh->url)
     278             :   {
     279           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     280             :                 "Could not construct request URL.\n");
     281           0 :     GNUNET_free (rgh);
     282           0 :     return NULL;
     283             :   }
     284           0 :   eh = curl_easy_init ();
     285           0 :   GNUNET_assert (CURLE_OK ==
     286             :                  curl_easy_setopt (eh,
     287             :                                    CURLOPT_URL,
     288             :                                    rgh->url));
     289           0 :   rgh->job = GNUNET_CURL_job_add (ctx,
     290             :                                   eh,
     291             :                                   &handle_reserve_get_finished,
     292             :                                   rgh);
     293           0 :   return rgh;
     294             : }
     295             : 
     296             : 
     297             : /**
     298             :  * Cancel a GET /reserves/$RESERVE_ID request.
     299             :  *
     300             :  * @param rgh handle to the request to be canceled
     301             :  */
     302             : void
     303           0 : TALER_MERCHANT_reserve_get_cancel (
     304             :   struct TALER_MERCHANT_ReserveGetHandle *rgh)
     305             : {
     306           0 :   if (NULL != rgh->job)
     307             :   {
     308           0 :     GNUNET_CURL_job_cancel (rgh->job);
     309           0 :     rgh->job = NULL;
     310             :   }
     311           0 :   GNUNET_free (rgh->url);
     312           0 :   GNUNET_free (rgh);
     313           0 : }
     314             : 
     315             : 
     316             : /* end of merchant_api_get_reserve.c */

Generated by: LCOV version 1.14