LCOV - code coverage report
Current view: top level - lib - merchant_api_get_transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 105 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_transfers.c
      19             :  * @brief Implementation of the GET /transfers 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 transfers.
      36             :  */
      37             : struct TALER_MERCHANT_GetTransfersHandle
      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_GetTransfersCallback 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 /transfers request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_GetTransfersHandle`
      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_transfers_get_finished (void *cls,
      77             :                                long response_code,
      78             :                                const void *response)
      79             : {
      80           0 :   struct TALER_MERCHANT_GetTransfersHandle *gth = 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 :   gth->job = NULL;
      88           0 :   switch (response_code)
      89             :   {
      90           0 :   case 0:
      91           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      92           0 :     break;
      93           0 :   case MHD_HTTP_OK:
      94             :     {
      95             :       json_t *transfers;
      96             :       struct GNUNET_JSON_Specification spec[] = {
      97           0 :         GNUNET_JSON_spec_json ("transfers",
      98             :                                &transfers),
      99           0 :         GNUNET_JSON_spec_end ()
     100             :       };
     101             : 
     102           0 :       if (GNUNET_OK !=
     103           0 :           GNUNET_JSON_parse (json,
     104             :                              spec,
     105             :                              NULL, NULL))
     106             :       {
     107           0 :         GNUNET_break_op (0);
     108           0 :         hr.http_status = 0;
     109           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     110           0 :         break;
     111             :       }
     112             :       else
     113             :       {
     114             :         size_t tds_length;
     115             :         struct TALER_MERCHANT_TransferData *tds;
     116             :         json_t *transfer;
     117             :         unsigned int i;
     118             :         bool ok;
     119             : 
     120           0 :         if (! json_is_array (transfers))
     121             :         {
     122           0 :           GNUNET_break_op (0);
     123           0 :           GNUNET_JSON_parse_free (spec);
     124           0 :           hr.http_status = 0;
     125           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     126           0 :           break;
     127             :         }
     128           0 :         tds_length = json_array_size (transfers);
     129           0 :         tds = GNUNET_new_array (tds_length,
     130             :                                 struct TALER_MERCHANT_TransferData);
     131           0 :         ok = true;
     132           0 :         json_array_foreach (transfers, i, transfer) {
     133             :           /* FIXME: handle 'execution_time', 'verified', and/or 'confirmed'
     134             :                     not present in the response. */
     135           0 :           struct TALER_MERCHANT_TransferData *td = &tds[i];
     136             :           struct GNUNET_JSON_Specification ispec[] = {
     137           0 :             TALER_JSON_spec_amount ("credit_amount",
     138             :                                     &td->credit_amount),
     139           0 :             GNUNET_JSON_spec_fixed_auto ("wtid",
     140             :                                          &td->wtid),
     141           0 :             GNUNET_JSON_spec_string ("payto_uri",
     142             :                                      &td->payto_uri),
     143           0 :             GNUNET_JSON_spec_string ("exchange_url",
     144             :                                      &td->exchange_url),
     145           0 :             GNUNET_JSON_spec_uint64 ("transfer_serial_id",
     146             :                                      &td->credit_serial),
     147           0 :             TALER_JSON_spec_absolute_time ("execution_time",
     148             :                                            &td->execution_time),
     149           0 :             GNUNET_JSON_spec_bool ("verified",
     150             :                                    &td->verified),
     151           0 :             GNUNET_JSON_spec_bool ("confirmed",
     152             :                                    &td->confirmed),
     153           0 :             GNUNET_JSON_spec_end ()
     154             :           };
     155             : 
     156           0 :           if (GNUNET_OK !=
     157           0 :               GNUNET_JSON_parse (transfer,
     158             :                                  ispec,
     159             :                                  NULL, NULL))
     160             :           {
     161           0 :             GNUNET_break_op (0);
     162           0 :             ok = false;
     163           0 :             break;
     164             :           }
     165             :         }
     166             : 
     167           0 :         if (! ok)
     168             :         {
     169           0 :           GNUNET_break_op (0);
     170           0 :           GNUNET_free (tds);
     171           0 :           GNUNET_JSON_parse_free (spec);
     172           0 :           hr.http_status = 0;
     173           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     174           0 :           break;
     175             :         }
     176           0 :         gth->cb (gth->cb_cls,
     177             :                  &hr,
     178             :                  tds_length,
     179             :                  tds);
     180           0 :         GNUNET_free (tds);
     181           0 :         GNUNET_JSON_parse_free (spec);
     182           0 :         TALER_MERCHANT_transfers_get_cancel (gth);
     183           0 :         return;
     184             :       }
     185             :     }
     186           0 :   case MHD_HTTP_NOT_FOUND:
     187             :     /* Nothing really to verify, this should never
     188             :        happen, we should pass the JSON reply to the application */
     189           0 :     hr.ec = TALER_JSON_get_error_code (json);
     190           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     191           0 :     break;
     192           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     193             :     /* Server had an internal issue; we should retry, but this API
     194             :        leaves this to the application */
     195           0 :     hr.ec = TALER_JSON_get_error_code (json);
     196           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     197           0 :     break;
     198           0 :   default:
     199             :     /* unexpected response code */
     200           0 :     GNUNET_break_op (0);
     201           0 :     TALER_MERCHANT_parse_error_details_ (json,
     202             :                                          response_code,
     203             :                                          &hr);
     204           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     205             :                 "Unexpected response code %u/%d\n",
     206             :                 (unsigned int) response_code,
     207             :                 (int) hr.ec);
     208           0 :     response_code = 0;
     209           0 :     break;
     210             :   }
     211           0 :   gth->cb (gth->cb_cls,
     212             :            &hr,
     213             :            0,
     214             :            NULL);
     215           0 :   TALER_MERCHANT_transfers_get_cancel (gth);
     216             : }
     217             : 
     218             : 
     219             : /**
     220             :  * Request backend to return list of all wire transfers that
     221             :  * we received (or that the exchange claims we should have received).
     222             :  *
     223             :  * Note that when filtering by timestamp (using “before” and/or “after”), we
     224             :  * use the time reported by the exchange and thus will ONLY return results for
     225             :  * which we already have a response from the exchange. This should be
     226             :  * virtually all transfers, however it is conceivable that for some transfer
     227             :  * the exchange responded with a temporary error (i.e. HTTP status 500+) and
     228             :  * then we do not yet have an execution time to filter by. Thus, IF timestamp
     229             :  * filters are given, transfers for which we have no response from the
     230             :  * exchange yet are automatically excluded.
     231             :  *
     232             :  * @param ctx execution context
     233             :  * @param backend_url base URL of the backend
     234             :  * @param payto_uri filter by this credit account of the merchant
     235             :  * @param before limit to transactions before this timestamp, use
     236             :  *                 #GNUNET_TIME_UNIT_FOREVER_ABS to not filter by @a before
     237             :  * @param after limit to transactions after this timestamp, use
     238             :  *                 #GNUNET_TIME_UNIT_ZERO_ABS to not filter by @a after
     239             :  * @param limit return at most this number of results; negative to descend in execution time
     240             :  * @param offset start at this "credit serial" number (exclusive)
     241             :  * @param verified filter results by verification status
     242             :  * @param cb the callback to call when a reply for this request is available
     243             :  * @param cb_cls closure for @a cb
     244             :  * @return a handle for this request
     245             :  */
     246             : struct TALER_MERCHANT_GetTransfersHandle *
     247           0 : TALER_MERCHANT_transfers_get (
     248             :   struct GNUNET_CURL_Context *ctx,
     249             :   const char *backend_url,
     250             :   const char *payto_uri,
     251             :   const struct GNUNET_TIME_Absolute before,
     252             :   const struct GNUNET_TIME_Absolute after,
     253             :   int64_t limit,
     254             :   uint64_t offset,
     255             :   enum TALER_EXCHANGE_YesNoAll verified,
     256             :   TALER_MERCHANT_GetTransfersCallback cb,
     257             :   void *cb_cls)
     258             : {
     259             :   struct TALER_MERCHANT_GetTransfersHandle *gth;
     260             :   CURL *eh;
     261           0 :   const char *verified_s = NULL;
     262             :   char limit_s[30];
     263             :   char offset_s[30];
     264             :   char *before_s;
     265             :   char *after_s;
     266             : 
     267           0 :   gth = GNUNET_new (struct TALER_MERCHANT_GetTransfersHandle);
     268           0 :   gth->ctx = ctx;
     269           0 :   gth->cb = cb;
     270           0 :   gth->cb_cls = cb_cls;
     271           0 :   verified_s = TALER_yna_to_string (verified);
     272           0 :   GNUNET_snprintf (limit_s,
     273             :                    sizeof (limit_s),
     274             :                    "%lld",
     275             :                    (long long) limit);
     276           0 :   GNUNET_snprintf (offset_s,
     277             :                    sizeof (offset_s),
     278             :                    "%lld",
     279             :                    (unsigned long long) offset);
     280           0 :   before_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (before));
     281           0 :   after_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (after));
     282           0 :   gth->url = TALER_url_join (backend_url,
     283             :                              "private/transfers",
     284             :                              "payto_uri",
     285             :                              payto_uri,
     286             :                              "verified",
     287             :                              (TALER_EXCHANGE_YNA_ALL != verified)
     288             :                              ? verified_s
     289             :                              : NULL,
     290             :                              "limit",
     291             :                              0 != limit
     292             :                              ? limit_s
     293             :                              : NULL,
     294             :                              "offset",
     295           0 :                              ((0 != offset) && (UINT64_MAX != offset))
     296             :                              ? offset_s
     297             :                              : NULL,
     298             :                              "before",
     299           0 :                              before.abs_value_us !=
     300           0 :                              GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us
     301             :                              ? before_s
     302             :                              : NULL,
     303             :                              "after",
     304           0 :                              after.abs_value_us != 0
     305             :                              ? after_s
     306             :                              : NULL,
     307             :                              NULL);
     308           0 :   GNUNET_free (before_s);
     309           0 :   GNUNET_free (after_s);
     310           0 :   if (NULL == gth->url)
     311             :   {
     312           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     313             :                 "Could not construct request URL.\n");
     314           0 :     GNUNET_free (gth);
     315           0 :     return NULL;
     316             :   }
     317           0 :   eh = curl_easy_init ();
     318           0 :   GNUNET_assert (CURLE_OK ==
     319             :                  curl_easy_setopt (eh,
     320             :                                    CURLOPT_URL,
     321             :                                    gth->url));
     322           0 :   gth->job = GNUNET_CURL_job_add (ctx,
     323             :                                   eh,
     324             :                                   &handle_transfers_get_finished,
     325             :                                   gth);
     326           0 :   return gth;
     327             : }
     328             : 
     329             : 
     330             : /**
     331             :  * Cancel a GET /transfers request.  This function cannot be used
     332             :  * on a request handle if a response is already served for it.
     333             :  *
     334             :  * @param gth handle to the tracking operation being cancelled
     335             :  */
     336             : void
     337           0 : TALER_MERCHANT_transfers_get_cancel (
     338             :   struct TALER_MERCHANT_GetTransfersHandle *gth)
     339             : {
     340           0 :   if (NULL != gth->job)
     341             :   {
     342           0 :     GNUNET_CURL_job_cancel (gth->job);
     343           0 :     gth->job = NULL;
     344             :   }
     345           0 :   GNUNET_free (gth->url);
     346           0 :   GNUNET_free (gth);
     347           0 : }
     348             : 
     349             : 
     350             : /* end of merchant_api_get_transfers.c */

Generated by: LCOV version 1.14