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: 2021-04-12 06:04:58 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 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 response 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_GENERIC_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_GENERIC_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_GENERIC_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           0 :           struct TALER_MERCHANT_TransferData *td = &tds[i];
     134             :           struct GNUNET_JSON_Specification ispec[] = {
     135           0 :             TALER_JSON_spec_amount ("credit_amount",
     136             :                                     &td->credit_amount),
     137           0 :             GNUNET_JSON_spec_fixed_auto ("wtid",
     138             :                                          &td->wtid),
     139           0 :             GNUNET_JSON_spec_string ("payto_uri",
     140             :                                      &td->payto_uri),
     141           0 :             GNUNET_JSON_spec_string ("exchange_url",
     142             :                                      &td->exchange_url),
     143           0 :             GNUNET_JSON_spec_uint64 ("transfer_serial_id",
     144             :                                      &td->credit_serial),
     145           0 :             GNUNET_JSON_spec_mark_optional (
     146             :             TALER_JSON_spec_absolute_time ("execution_time",
     147             :                                            &td->execution_time)),
     148           0 :             GNUNET_JSON_spec_mark_optional (
     149             :             GNUNET_JSON_spec_bool ("verified",
     150             :                                    &td->verified)),
     151           0 :             GNUNET_JSON_spec_mark_optional (
     152             :             GNUNET_JSON_spec_bool ("confirmed",
     153             :                                    &td->confirmed)),
     154           0 :             GNUNET_JSON_spec_end ()
     155             :           };
     156             : 
     157           0 :           if (GNUNET_OK !=
     158           0 :               GNUNET_JSON_parse (transfer,
     159             :                                  ispec,
     160             :                                  NULL, NULL))
     161             :           {
     162           0 :             GNUNET_break_op (0);
     163           0 :             ok = false;
     164           0 :             break;
     165             :           }
     166             :         }
     167             : 
     168           0 :         if (! ok)
     169             :         {
     170           0 :           GNUNET_break_op (0);
     171           0 :           GNUNET_free (tds);
     172           0 :           GNUNET_JSON_parse_free (spec);
     173           0 :           hr.http_status = 0;
     174           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     175           0 :           break;
     176             :         }
     177           0 :         gth->cb (gth->cb_cls,
     178             :                  &hr,
     179             :                  tds_length,
     180             :                  tds);
     181           0 :         GNUNET_free (tds);
     182           0 :         GNUNET_JSON_parse_free (spec);
     183           0 :         TALER_MERCHANT_transfers_get_cancel (gth);
     184           0 :         return;
     185             :       }
     186             :     }
     187           0 :   case MHD_HTTP_NOT_FOUND:
     188             :     /* Nothing really to verify, this should never
     189             :        happen, we should pass the JSON reply to the application */
     190           0 :     hr.ec = TALER_JSON_get_error_code (json);
     191           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     192           0 :     break;
     193           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     194             :     /* Server had an internal issue; we should retry, but this API
     195             :        leaves this to the application */
     196           0 :     hr.ec = TALER_JSON_get_error_code (json);
     197           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     198           0 :     break;
     199           0 :   default:
     200             :     /* unexpected response code */
     201           0 :     GNUNET_break_op (0);
     202           0 :     TALER_MERCHANT_parse_error_details_ (json,
     203             :                                          response_code,
     204             :                                          &hr);
     205           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     206             :                 "Unexpected response code %u/%d\n",
     207             :                 (unsigned int) response_code,
     208             :                 (int) hr.ec);
     209           0 :     response_code = 0;
     210           0 :     break;
     211             :   }
     212           0 :   gth->cb (gth->cb_cls,
     213             :            &hr,
     214             :            0,
     215             :            NULL);
     216           0 :   TALER_MERCHANT_transfers_get_cancel (gth);
     217             : }
     218             : 
     219             : 
     220             : struct TALER_MERCHANT_GetTransfersHandle *
     221           0 : TALER_MERCHANT_transfers_get (
     222             :   struct GNUNET_CURL_Context *ctx,
     223             :   const char *backend_url,
     224             :   const char *payto_uri,
     225             :   const struct GNUNET_TIME_Absolute before,
     226             :   const struct GNUNET_TIME_Absolute after,
     227             :   int64_t limit,
     228             :   uint64_t offset,
     229             :   enum TALER_EXCHANGE_YesNoAll verified,
     230             :   TALER_MERCHANT_GetTransfersCallback cb,
     231             :   void *cb_cls)
     232             : {
     233             :   struct TALER_MERCHANT_GetTransfersHandle *gth;
     234             :   CURL *eh;
     235           0 :   const char *verified_s = NULL;
     236             :   char limit_s[30];
     237             :   char offset_s[30];
     238             :   char *before_s;
     239             :   char *after_s;
     240             : 
     241           0 :   gth = GNUNET_new (struct TALER_MERCHANT_GetTransfersHandle);
     242           0 :   gth->ctx = ctx;
     243           0 :   gth->cb = cb;
     244           0 :   gth->cb_cls = cb_cls;
     245           0 :   verified_s = TALER_yna_to_string (verified);
     246           0 :   GNUNET_snprintf (limit_s,
     247             :                    sizeof (limit_s),
     248             :                    "%lld",
     249             :                    (long long) limit);
     250           0 :   GNUNET_snprintf (offset_s,
     251             :                    sizeof (offset_s),
     252             :                    "%lld",
     253             :                    (unsigned long long) offset);
     254           0 :   before_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (before));
     255           0 :   after_s = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (after));
     256           0 :   gth->url = TALER_url_join (backend_url,
     257             :                              "private/transfers",
     258             :                              "payto_uri",
     259             :                              payto_uri,
     260             :                              "verified",
     261             :                              (TALER_EXCHANGE_YNA_ALL != verified)
     262             :                              ? verified_s
     263             :                              : NULL,
     264             :                              "limit",
     265             :                              0 != limit
     266             :                              ? limit_s
     267             :                              : NULL,
     268             :                              "offset",
     269           0 :                              ((0 != offset) && (UINT64_MAX != offset))
     270             :                              ? offset_s
     271             :                              : NULL,
     272             :                              "before",
     273           0 :                              before.abs_value_us !=
     274           0 :                              GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us
     275             :                              ? before_s
     276             :                              : NULL,
     277             :                              "after",
     278           0 :                              after.abs_value_us != 0
     279             :                              ? after_s
     280             :                              : NULL,
     281             :                              NULL);
     282           0 :   GNUNET_free (before_s);
     283           0 :   GNUNET_free (after_s);
     284           0 :   if (NULL == gth->url)
     285             :   {
     286           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     287             :                 "Could not construct request URL.\n");
     288           0 :     GNUNET_free (gth);
     289           0 :     return NULL;
     290             :   }
     291           0 :   eh = curl_easy_init ();
     292           0 :   GNUNET_assert (CURLE_OK ==
     293             :                  curl_easy_setopt (eh,
     294             :                                    CURLOPT_URL,
     295             :                                    gth->url));
     296           0 :   gth->job = GNUNET_CURL_job_add (ctx,
     297             :                                   eh,
     298             :                                   &handle_transfers_get_finished,
     299             :                                   gth);
     300           0 :   return gth;
     301             : }
     302             : 
     303             : 
     304             : void
     305           0 : TALER_MERCHANT_transfers_get_cancel (
     306             :   struct TALER_MERCHANT_GetTransfersHandle *gth)
     307             : {
     308           0 :   if (NULL != gth->job)
     309             :   {
     310           0 :     GNUNET_CURL_job_cancel (gth->job);
     311           0 :     gth->job = NULL;
     312             :   }
     313           0 :   GNUNET_free (gth->url);
     314           0 :   GNUNET_free (gth);
     315           0 : }
     316             : 
     317             : 
     318             : /* end of merchant_api_get_transfers.c */

Generated by: LCOV version 1.14