LCOV - code coverage report
Current view: top level - lib - merchant_api_get_orders.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 104 0.0 %
Date: 2021-04-12 06:04:58 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018, 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_orders.c
      19             :  * @brief Implementation of the GET /orders request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      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             : /**
      34             :  * Handle for a GET /orders operation.
      35             :  */
      36             : struct TALER_MERCHANT_OrdersGetHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_OrdersGetCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Parse order information from @a ia.
      68             :  *
      69             :  * @param ia JSON array (or NULL!) with order data
      70             :  * @param ogh operation handle
      71             :  * @return #GNUNET_OK on success
      72             :  */
      73             : static int
      74           0 : parse_orders (const json_t *ia,
      75             :               struct TALER_MERCHANT_OrdersGetHandle *ogh)
      76           0 : {
      77           0 :   unsigned int oes_len = json_array_size (ia);
      78           0 :   struct TALER_MERCHANT_OrderEntry oes[oes_len];
      79             :   size_t index;
      80             :   json_t *value;
      81             :   int ret;
      82             : 
      83           0 :   ret = GNUNET_OK;
      84           0 :   json_array_foreach (ia, index, value) {
      85           0 :     struct TALER_MERCHANT_OrderEntry *ie = &oes[index];
      86             :     struct GNUNET_JSON_Specification spec[] = {
      87           0 :       GNUNET_JSON_spec_string ("order_id",
      88             :                                &ie->order_id),
      89           0 :       TALER_JSON_spec_absolute_time ("timestamp",
      90             :                                      &ie->timestamp),
      91           0 :       GNUNET_JSON_spec_uint64 ("row_id",
      92             :                                &ie->order_serial),
      93           0 :       TALER_JSON_spec_amount ("amount",
      94             :                               &ie->amount),
      95           0 :       GNUNET_JSON_spec_string ("summary",
      96             :                                &ie->summary),
      97           0 :       GNUNET_JSON_spec_bool ("refundable",
      98             :                              &ie->refundable),
      99           0 :       GNUNET_JSON_spec_bool ("paid",
     100             :                              &ie->paid),
     101           0 :       GNUNET_JSON_spec_end ()
     102             :     };
     103             : 
     104           0 :     if (GNUNET_OK !=
     105           0 :         GNUNET_JSON_parse (value,
     106             :                            spec,
     107             :                            NULL, NULL))
     108             :     {
     109           0 :       GNUNET_break_op (0);
     110           0 :       ret = GNUNET_SYSERR;
     111           0 :       continue;
     112             :     }
     113           0 :     if (GNUNET_SYSERR == ret)
     114           0 :       break;
     115             :   }
     116           0 :   if (GNUNET_OK == ret)
     117             :   {
     118           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     119             :       .http_status = MHD_HTTP_OK
     120             :     };
     121             : 
     122           0 :     ogh->cb (ogh->cb_cls,
     123             :              &hr,
     124             :              oes_len,
     125             :              oes);
     126           0 :     ogh->cb = NULL; /* just to be sure */
     127             :   }
     128           0 :   return ret;
     129             : }
     130             : 
     131             : 
     132             : /**
     133             :  * Function called when we're done processing the
     134             :  * HTTP /orders request.
     135             :  *
     136             :  * @param cls the `struct TALER_MERCHANT_OrdersGetHandle`
     137             :  * @param response_code HTTP response code, 0 on error
     138             :  * @param response response body, NULL if not in JSON
     139             :  */
     140             : static void
     141           0 : handle_get_orders_finished (void *cls,
     142             :                             long response_code,
     143             :                             const void *response)
     144             : {
     145           0 :   struct TALER_MERCHANT_OrdersGetHandle *ogh = cls;
     146           0 :   const json_t *json = response;
     147           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     148           0 :     .http_status = (unsigned int) response_code,
     149             :     .reply = json
     150             :   };
     151             : 
     152           0 :   ogh->job = NULL;
     153           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     154             :               "Got /orders response with status code %u\n",
     155             :               (unsigned int) response_code);
     156           0 :   switch (response_code)
     157             :   {
     158           0 :   case MHD_HTTP_OK:
     159             :     {
     160             :       json_t *orders;
     161             :       struct GNUNET_JSON_Specification spec[] = {
     162           0 :         GNUNET_JSON_spec_json ("orders",
     163             :                                &orders),
     164           0 :         GNUNET_JSON_spec_end ()
     165             :       };
     166             : 
     167           0 :       if (GNUNET_OK !=
     168           0 :           GNUNET_JSON_parse (json,
     169             :                              spec,
     170             :                              NULL, NULL))
     171             :       {
     172           0 :         hr.http_status = 0;
     173           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     174             :       }
     175             :       else
     176             :       {
     177           0 :         if ( (! json_is_array (orders)) ||
     178             :              (GNUNET_OK ==
     179           0 :               parse_orders (orders,
     180             :                             ogh)) )
     181             :         {
     182           0 :           GNUNET_JSON_parse_free (spec);
     183           0 :           TALER_MERCHANT_orders_get_cancel (ogh);
     184           0 :           return;
     185             :         }
     186             :         else
     187             :         {
     188           0 :           hr.http_status = 0;
     189           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     190             :         }
     191             :       }
     192           0 :       GNUNET_JSON_parse_free (spec);
     193           0 :       break;
     194             :     }
     195           0 :   case MHD_HTTP_NOT_FOUND:
     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 :     hr.ec = TALER_JSON_get_error_code (json);
     202           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     203           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     204             :                 "Unexpected response code %u/%d\n",
     205             :                 (unsigned int) response_code,
     206             :                 (int) hr.ec);
     207           0 :     break;
     208             :   }
     209           0 :   ogh->cb (ogh->cb_cls,
     210             :            &hr,
     211             :            0,
     212             :            NULL);
     213           0 :   TALER_MERCHANT_orders_get_cancel (ogh);
     214             : }
     215             : 
     216             : 
     217             : struct TALER_MERCHANT_OrdersGetHandle *
     218           0 : TALER_MERCHANT_orders_get (
     219             :   struct GNUNET_CURL_Context *ctx,
     220             :   const char *backend_url,
     221             :   TALER_MERCHANT_OrdersGetCallback cb,
     222             :   void *cb_cls)
     223             : {
     224           0 :   return TALER_MERCHANT_orders_get2 (ctx,
     225             :                                      backend_url,
     226             :                                      TALER_EXCHANGE_YNA_ALL,
     227             :                                      TALER_EXCHANGE_YNA_ALL,
     228             :                                      TALER_EXCHANGE_YNA_ALL,
     229             :                                      GNUNET_TIME_UNIT_FOREVER_ABS,
     230             :                                      UINT64_MAX,
     231             :                                      -20, /* default is most recent 20 entries */
     232             :                                      GNUNET_TIME_UNIT_ZERO,
     233             :                                      cb,
     234             :                                      cb_cls);
     235             : }
     236             : 
     237             : 
     238             : struct TALER_MERCHANT_OrdersGetHandle *
     239           0 : TALER_MERCHANT_orders_get2 (
     240             :   struct GNUNET_CURL_Context *ctx,
     241             :   const char *backend_url,
     242             :   enum TALER_EXCHANGE_YesNoAll paid,
     243             :   enum TALER_EXCHANGE_YesNoAll refunded,
     244             :   enum TALER_EXCHANGE_YesNoAll wired,
     245             :   struct GNUNET_TIME_Absolute date,
     246             :   uint64_t start_row,
     247             :   int64_t delta,
     248             :   struct GNUNET_TIME_Relative timeout,
     249             :   TALER_MERCHANT_OrdersGetCallback cb,
     250             :   void *cb_cls)
     251             : {
     252             :   struct TALER_MERCHANT_OrdersGetHandle *ogh;
     253             :   CURL *eh;
     254           0 :   unsigned int timeout_ms = timeout.rel_value_us
     255           0 :                             / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
     256             : 
     257           0 :   GNUNET_assert (NULL != backend_url);
     258           0 :   if (0 == delta)
     259             :   {
     260           0 :     GNUNET_break (0);
     261           0 :     return NULL;
     262             :   }
     263           0 :   ogh = GNUNET_new (struct TALER_MERCHANT_OrdersGetHandle);
     264           0 :   ogh->ctx = ctx;
     265           0 :   ogh->cb = cb;
     266           0 :   ogh->cb_cls = cb_cls;
     267             : 
     268             :   /* build ogh->url with the various optional arguments */
     269             :   {
     270             :     const char *dstr;
     271             :     bool have_date;
     272             :     bool have_srow;
     273             :     char cbuf[30];
     274             :     char dbuf[30];
     275             :     char tbuf[30];
     276             : 
     277           0 :     GNUNET_snprintf (tbuf,
     278             :                      sizeof (tbuf),
     279             :                      "%llu",
     280             :                      (unsigned long long) timeout_ms);
     281           0 :     GNUNET_snprintf (dbuf,
     282             :                      sizeof (dbuf),
     283             :                      "%lld",
     284             :                      (long long) delta);
     285           0 :     GNUNET_snprintf (cbuf,
     286             :                      sizeof (cbuf),
     287             :                      "%llu",
     288             :                      (unsigned long long) start_row);
     289           0 :     dstr = GNUNET_STRINGS_absolute_time_to_string (date);
     290           0 :     if (delta > 0)
     291             :     {
     292           0 :       have_date = (0 != date.abs_value_us);
     293           0 :       have_srow = (0 != start_row);
     294             :     }
     295             :     else
     296             :     {
     297           0 :       have_date = (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
     298           0 :                    date.abs_value_us);
     299           0 :       have_srow = (UINT64_MAX != start_row);
     300             :     }
     301           0 :     ogh->url = TALER_url_join (backend_url,
     302             :                                "private/orders",
     303             :                                "paid",
     304             :                                (TALER_EXCHANGE_YNA_ALL != paid)
     305           0 :                                ? TALER_yna_to_string (paid)
     306             :                                : NULL,
     307             :                                "refunded",
     308             :                                (TALER_EXCHANGE_YNA_ALL != refunded)
     309           0 :                                ? TALER_yna_to_string (refunded)
     310             :                                : NULL,
     311             :                                "wired",
     312             :                                (TALER_EXCHANGE_YNA_ALL != wired)
     313           0 :                                ? TALER_yna_to_string (wired)
     314             :                                : NULL,
     315             :                                "date",
     316             :                                (have_date)
     317             :                                ? dstr
     318             :                                : NULL,
     319             :                                "start",
     320             :                                (have_srow)
     321             :                                ? cbuf
     322             :                                : NULL,
     323             :                                "delta",
     324             :                                (-20 != delta)
     325             :                                ? dbuf
     326             :                                : NULL,
     327             :                                "timeout_ms",
     328             :                                (0 != timeout_ms)
     329             :                                ? tbuf
     330             :                                : NULL,
     331             :                                NULL);
     332             :   }
     333           0 :   if (NULL == ogh->url)
     334             :   {
     335           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     336             :                 "Could not construct request URL.\n");
     337           0 :     GNUNET_free (ogh);
     338           0 :     return NULL;
     339             :   }
     340           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     341             :               "Requesting URL '%s'\n",
     342             :               ogh->url);
     343           0 :   eh = curl_easy_init ();
     344           0 :   GNUNET_assert (CURLE_OK ==
     345             :                  curl_easy_setopt (eh,
     346             :                                    CURLOPT_URL,
     347             :                                    ogh->url));
     348           0 :   ogh->job = GNUNET_CURL_job_add (ctx,
     349             :                                   eh,
     350             :                                   &handle_get_orders_finished,
     351             :                                   ogh);
     352           0 :   return ogh;
     353             : }
     354             : 
     355             : 
     356             : void
     357           0 : TALER_MERCHANT_orders_get_cancel (
     358             :   struct TALER_MERCHANT_OrdersGetHandle *ogh)
     359             : {
     360           0 :   if (NULL != ogh->job)
     361           0 :     GNUNET_CURL_job_cancel (ogh->job);
     362           0 :   GNUNET_free (ogh->url);
     363           0 :   GNUNET_free (ogh);
     364           0 : }

Generated by: LCOV version 1.14