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 100 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 json 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_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_INVALID_RESPONSE;
     190             :         }
     191             :       }
     192           0 :       GNUNET_JSON_parse_free (spec);
     193           0 :       break;
     194             :     }
     195           0 :   default:
     196             :     /* unexpected response code */
     197           0 :     hr.ec = TALER_JSON_get_error_code (json);
     198           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     199           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     200             :                 "Unexpected response code %u/%d\n",
     201             :                 (unsigned int) response_code,
     202             :                 (int) hr.ec);
     203           0 :     break;
     204             :   }
     205           0 :   ogh->cb (ogh->cb_cls,
     206             :            &hr,
     207             :            0,
     208             :            NULL);
     209           0 :   TALER_MERCHANT_orders_get_cancel (ogh);
     210             : }
     211             : 
     212             : 
     213             : /**
     214             :  * Make a GET /orders request.
     215             :  *
     216             :  * @param ctx the context
     217             :  * @param backend_url HTTP base URL for the backend
     218             :  * @param cb function to call with the backend's inventory information
     219             :  * @param cb_cls closure for @a cb
     220             :  * @return the request handle; NULL upon error
     221             :  */
     222             : struct TALER_MERCHANT_OrdersGetHandle *
     223           0 : TALER_MERCHANT_orders_get (
     224             :   struct GNUNET_CURL_Context *ctx,
     225             :   const char *backend_url,
     226             :   TALER_MERCHANT_OrdersGetCallback cb,
     227             :   void *cb_cls)
     228             : {
     229           0 :   return TALER_MERCHANT_orders_get2 (ctx,
     230             :                                      backend_url,
     231             :                                      TALER_EXCHANGE_YNA_ALL,
     232             :                                      TALER_EXCHANGE_YNA_ALL,
     233             :                                      TALER_EXCHANGE_YNA_ALL,
     234             :                                      GNUNET_TIME_UNIT_FOREVER_ABS,
     235             :                                      UINT64_MAX,
     236             :                                      -20, /* default is most recent 20 entries */
     237             :                                      GNUNET_TIME_UNIT_ZERO,
     238             :                                      cb,
     239             :                                      cb_cls);
     240             : }
     241             : 
     242             : 
     243             : /**
     244             :  * Make a GET /orders request with filters.
     245             :  *
     246             :  * @param ctx the context
     247             :  * @param backend_url HTTP base URL for the backend
     248             :  * @param paid filter on payment status
     249             :  * @param refunded filter on refund status
     250             :  * @param wired filter on wire transfer status
     251             :  * @param date range limit by date
     252             :  * @param start_row range limit by order table row
     253             :  * @param delta range from which @a date and @a start_row apply, positive
     254             :  *              to return delta items after the given limit(s), negative to
     255             :  *              return delta items before the given limit(s)
     256             :  * @param timeout how long to wait (long polling) of zero results match the query
     257             :  * @param cb function to call with the backend's inventory information
     258             :  * @param cb_cls closure for @a cb
     259             :  * @return the request handle; NULL upon error
     260             :  */
     261             : struct TALER_MERCHANT_OrdersGetHandle *
     262           0 : TALER_MERCHANT_orders_get2 (
     263             :   struct GNUNET_CURL_Context *ctx,
     264             :   const char *backend_url,
     265             :   enum TALER_EXCHANGE_YesNoAll paid,
     266             :   enum TALER_EXCHANGE_YesNoAll refunded,
     267             :   enum TALER_EXCHANGE_YesNoAll wired,
     268             :   struct GNUNET_TIME_Absolute date,
     269             :   uint64_t start_row,
     270             :   int64_t delta,
     271             :   struct GNUNET_TIME_Relative timeout,
     272             :   TALER_MERCHANT_OrdersGetCallback cb,
     273             :   void *cb_cls)
     274             : {
     275             :   struct TALER_MERCHANT_OrdersGetHandle *ogh;
     276             :   CURL *eh;
     277           0 :   unsigned int timeout_ms = timeout.rel_value_us
     278           0 :                             / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
     279             : 
     280           0 :   GNUNET_assert (NULL != backend_url);
     281           0 :   if (0 == delta)
     282             :   {
     283           0 :     GNUNET_break (0);
     284           0 :     return NULL;
     285             :   }
     286           0 :   ogh = GNUNET_new (struct TALER_MERCHANT_OrdersGetHandle);
     287           0 :   ogh->ctx = ctx;
     288           0 :   ogh->cb = cb;
     289           0 :   ogh->cb_cls = cb_cls;
     290             : 
     291             :   /* build ogh->url with the various optional arguments */
     292             :   {
     293             :     const char *dstr;
     294             :     bool have_date;
     295             :     bool have_srow;
     296             :     char cbuf[30];
     297             :     char dbuf[30];
     298             :     char tbuf[30];
     299             : 
     300           0 :     GNUNET_snprintf (tbuf,
     301             :                      sizeof (tbuf),
     302             :                      "%llu",
     303             :                      (unsigned long long) timeout_ms);
     304           0 :     GNUNET_snprintf (dbuf,
     305             :                      sizeof (dbuf),
     306             :                      "%lld",
     307             :                      (long long) delta);
     308           0 :     GNUNET_snprintf (cbuf,
     309             :                      sizeof (cbuf),
     310             :                      "%llu",
     311             :                      (unsigned long long) start_row);
     312           0 :     dstr = GNUNET_STRINGS_absolute_time_to_string (date);
     313           0 :     if (delta > 0)
     314             :     {
     315           0 :       have_date = (0 != date.abs_value_us);
     316           0 :       have_srow = (0 != start_row);
     317             :     }
     318             :     else
     319             :     {
     320           0 :       have_date = (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
     321           0 :                    date.abs_value_us);
     322           0 :       have_srow = (UINT64_MAX != start_row);
     323             :     }
     324           0 :     ogh->url = TALER_url_join (backend_url,
     325             :                                "private/orders",
     326             :                                "paid",
     327             :                                (TALER_EXCHANGE_YNA_ALL != paid)
     328           0 :                                ? TALER_yna_to_string (paid)
     329             :                                : NULL,
     330             :                                "refunded",
     331             :                                (TALER_EXCHANGE_YNA_ALL != refunded)
     332           0 :                                ? TALER_yna_to_string (refunded)
     333             :                                : NULL,
     334             :                                "wired",
     335             :                                (TALER_EXCHANGE_YNA_ALL != wired)
     336           0 :                                ? TALER_yna_to_string (wired)
     337             :                                : NULL,
     338             :                                "date",
     339             :                                (have_date)
     340             :                                ? dstr
     341             :                                : NULL,
     342             :                                "start",
     343             :                                (have_srow)
     344             :                                ? cbuf
     345             :                                : NULL,
     346             :                                "delta",
     347             :                                (-20 != delta)
     348             :                                ? dbuf
     349             :                                : NULL,
     350             :                                "timeout_ms",
     351             :                                (0 != timeout_ms)
     352             :                                ? tbuf
     353             :                                : NULL,
     354             :                                NULL);
     355             :   }
     356           0 :   if (NULL == ogh->url)
     357             :   {
     358           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     359             :                 "Could not construct request URL.\n");
     360           0 :     GNUNET_free (ogh);
     361           0 :     return NULL;
     362             :   }
     363           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     364             :               "Requesting URL '%s'\n",
     365             :               ogh->url);
     366           0 :   eh = curl_easy_init ();
     367           0 :   GNUNET_assert (CURLE_OK ==
     368             :                  curl_easy_setopt (eh,
     369             :                                    CURLOPT_URL,
     370             :                                    ogh->url));
     371           0 :   ogh->job = GNUNET_CURL_job_add (ctx,
     372             :                                   eh,
     373             :                                   &handle_get_orders_finished,
     374             :                                   ogh);
     375           0 :   return ogh;
     376             : }
     377             : 
     378             : 
     379             : /**
     380             :  * Cancel /orders request.  Must not be called by clients after
     381             :  * the callback was invoked.
     382             :  *
     383             :  * @param ogh request to cancel.
     384             :  */
     385             : void
     386           0 : TALER_MERCHANT_orders_get_cancel (
     387             :   struct TALER_MERCHANT_OrdersGetHandle *ogh)
     388             : {
     389           0 :   if (NULL != ogh->job)
     390           0 :     GNUNET_CURL_job_cancel (ogh->job);
     391           0 :   GNUNET_free (ogh->url);
     392           0 :   GNUNET_free (ogh);
     393           0 : }

Generated by: LCOV version 1.14