LCOV - code coverage report
Current view: top level - lib - merchant_api_get_tips.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 87 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) 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_tips.c
      19             :  * @brief Implementation of the GET /private/tips request of the merchant's HTTP API
      20             :  * @author Jonathan Buchanan
      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 /private/tips operation.
      35             :  */
      36             : struct TALER_MERCHANT_TipsGetHandle
      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_TipsGetCallback 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 tip information from @a ia.
      68             :  *
      69             :  * @param ia JSON array (or NULL!) tip order data
      70             :  * @param tgh operation handle
      71             :  * @return #GNUNET_OK on success
      72             :  */
      73             : static int
      74           0 : parse_tips (const json_t *ia,
      75             :             struct TALER_MERCHANT_TipsGetHandle *tgh)
      76           0 : {
      77           0 :   unsigned int tes_len = json_array_size (ia);
      78           0 :   struct TALER_MERCHANT_TipEntry tes[tes_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_TipEntry *ie = &tes[index];
      86             :     struct GNUNET_JSON_Specification spec[] = {
      87           0 :       GNUNET_JSON_spec_uint64 ("row_id",
      88             :                                &ie->row_id),
      89           0 :       GNUNET_JSON_spec_fixed_auto ("tip_id",
      90             :                                    &ie->tip_id),
      91           0 :       TALER_JSON_spec_amount ("tip_amount",
      92             :                               &ie->tip_amount),
      93           0 :       GNUNET_JSON_spec_end ()
      94             :     };
      95             : 
      96           0 :     if (GNUNET_OK !=
      97           0 :         GNUNET_JSON_parse (value,
      98             :                            spec,
      99             :                            NULL, NULL))
     100             :     {
     101           0 :       GNUNET_break_op (0);
     102           0 :       ret = GNUNET_SYSERR;
     103           0 :       continue;
     104             :     }
     105           0 :     if (GNUNET_SYSERR == ret)
     106           0 :       break;
     107             :   }
     108           0 :   if (GNUNET_OK == ret)
     109             :   {
     110           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     111             :       .http_status = MHD_HTTP_OK
     112             :     };
     113             : 
     114           0 :     tgh->cb (tgh->cb_cls,
     115             :              &hr,
     116             :              tes_len,
     117             :              tes);
     118           0 :     tgh->cb = NULL; /* just to be sure */
     119             :   }
     120           0 :   return ret;
     121             : }
     122             : 
     123             : 
     124             : /**
     125             :  * Function called when we're done processing the
     126             :  * HTTP GET /private/tips request.
     127             :  *
     128             :  * @param cls the `struct TALER_MERCHANT_TipsGetHandle`
     129             :  * @param response_code HTTP response code, 0 on error
     130             :  * @param json response body, NULL if not in JSON
     131             :  */
     132             : static void
     133           0 : handle_get_tips_finished (void *cls,
     134             :                           long response_code,
     135             :                           const void *response)
     136             : {
     137           0 :   struct TALER_MERCHANT_TipsGetHandle *tgh = cls;
     138           0 :   const json_t *json = response;
     139           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     140           0 :     .http_status = (unsigned int) response_code,
     141             :     .reply = json
     142             :   };
     143             : 
     144           0 :   tgh->job = NULL;
     145           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     146             :               "Got /private/tips response with status code %u\n",
     147             :               (unsigned int) response_code);
     148           0 :   switch (response_code)
     149             :   {
     150           0 :   case MHD_HTTP_OK:
     151             :     {
     152             :       json_t *tips;
     153             :       struct GNUNET_JSON_Specification spec[] = {
     154           0 :         GNUNET_JSON_spec_json ("tips",
     155             :                                &tips),
     156           0 :         GNUNET_JSON_spec_end ()
     157             :       };
     158             : 
     159           0 :       if (GNUNET_OK !=
     160           0 :           GNUNET_JSON_parse (json,
     161             :                              spec,
     162             :                              NULL, NULL))
     163             :       {
     164           0 :         hr.http_status = 0;
     165           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     166             :       }
     167             :       else
     168             :       {
     169           0 :         if ( (! json_is_array (tips)) ||
     170             :              (GNUNET_OK ==
     171           0 :               parse_tips (tips,
     172             :                           tgh)) )
     173             :         {
     174           0 :           GNUNET_JSON_parse_free (spec);
     175           0 :           TALER_MERCHANT_tips_get_cancel (tgh);
     176           0 :           return;
     177             :         }
     178             :         else
     179             :         {
     180           0 :           hr.http_status = 0;
     181           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     182             :         }
     183             :       }
     184           0 :       GNUNET_JSON_parse_free (spec);
     185           0 :       break;
     186             :     }
     187           0 :   default:
     188             :     /* unexpected response code */
     189           0 :     hr.ec = TALER_JSON_get_error_code (json);
     190           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     191           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     192             :                 "Unexpected response code %u/%d\n",
     193             :                 (unsigned int) response_code,
     194             :                 (int) hr.ec);
     195           0 :     break;
     196             :   }
     197           0 :   tgh->cb (tgh->cb_cls,
     198             :            &hr,
     199             :            0,
     200             :            NULL);
     201           0 :   TALER_MERCHANT_tips_get_cancel (tgh);
     202             : }
     203             : 
     204             : 
     205             : /**
     206             :  * Make a GET /private/tips request.
     207             :  *
     208             :  * @param ctx the context
     209             :  * @param backend_url HTTP base URL for the backend
     210             :  * @param cb function to call with the backend's tip information
     211             :  * @param cb_cls closure for @a cb
     212             :  * @return the request handle; NULL upon error
     213             :  */
     214             : struct TALER_MERCHANT_TipsGetHandle *
     215           0 : TALER_MERCHANT_tips_get (
     216             :   struct GNUNET_CURL_Context *ctx,
     217             :   const char *backend_url,
     218             :   TALER_MERCHANT_TipsGetCallback cb,
     219             :   void *cb_cls)
     220             : {
     221           0 :   return TALER_MERCHANT_tips_get2 (ctx,
     222             :                                    backend_url,
     223             :                                    TALER_EXCHANGE_YNA_NO,
     224             :                                    -20,
     225             :                                    UINT64_MAX,
     226             :                                    cb,
     227             :                                    cb_cls);
     228             : }
     229             : 
     230             : 
     231             : /**
     232             :  * Issue a GET /private/tips request with filters to the backend.
     233             :  *
     234             :  * @param ctx execution context
     235             :  * @param backend_url base URL of the merchant backend
     236             :  * @param include_expired whether to return all tips or only unexpired tips
     237             :  * @param limit number of results to return, negative for descending row id, positive for ascending
     238             :  * @param offset row id to start returning results from
     239             :  * @param cb function to call with the result
     240             :  * @param cb_cls closure for @a cb
     241             :  * @return handle for this operation, NULL upon errors
     242             :  */
     243             : struct TALER_MERCHANT_TipsGetHandle *
     244           0 : TALER_MERCHANT_tips_get2 (struct GNUNET_CURL_Context *ctx,
     245             :                           const char *backend_url,
     246             :                           enum TALER_EXCHANGE_YesNoAll expired,
     247             :                           int64_t limit,
     248             :                           uint64_t offset,
     249             :                           TALER_MERCHANT_TipsGetCallback cb,
     250             :                           void *cb_cls)
     251             : {
     252             :   struct TALER_MERCHANT_TipsGetHandle *tgh;
     253             :   CURL *eh;
     254             : 
     255           0 :   GNUNET_assert (NULL != backend_url);
     256           0 :   if (0 == limit)
     257             :   {
     258           0 :     GNUNET_break (0);
     259           0 :     return NULL;
     260             :   }
     261           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipsGetHandle);
     262           0 :   tgh->ctx = ctx;
     263           0 :   tgh->cb = cb;
     264           0 :   tgh->cb_cls = cb_cls;
     265             : 
     266             :   /* build tgh->url with the various optional arguments */
     267             :   {
     268             :     char cbuf[30];
     269             :     char lbuf[30];
     270             :     bool have_offset;
     271             : 
     272           0 :     GNUNET_snprintf (lbuf,
     273             :                      sizeof (lbuf),
     274             :                      "%lld",
     275             :                      (long long) limit);
     276             : 
     277           0 :     if (limit > 0)
     278           0 :       have_offset = (0 != offset);
     279             :     else
     280           0 :       have_offset = (UINT64_MAX != offset);
     281           0 :     GNUNET_snprintf (cbuf,
     282             :                      sizeof (cbuf),
     283             :                      "%llu",
     284             :                      (unsigned long long) offset);
     285           0 :     tgh->url = TALER_url_join (backend_url,
     286             :                                "private/tips",
     287             :                                "expired",
     288             :                                (TALER_EXCHANGE_YNA_ALL != expired)
     289           0 :                                ? TALER_yna_to_string (expired)
     290             :                                : NULL,
     291             :                                "offset", (have_offset) ? cbuf : NULL,
     292             :                                "limit", (-20 != limit) ? lbuf : NULL,
     293             :                                NULL);
     294             :   }
     295           0 :   if (NULL == tgh->url)
     296             :   {
     297           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     298             :                 "Could not construct request URL.\n");
     299           0 :     GNUNET_free (tgh);
     300           0 :     return NULL;
     301             :   }
     302           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     303             :               "Requesting URL '%s'\n",
     304             :               tgh->url);
     305           0 :   eh = curl_easy_init ();
     306           0 :   GNUNET_assert (CURLE_OK ==
     307             :                  curl_easy_setopt (eh,
     308             :                                    CURLOPT_URL,
     309             :                                    tgh->url));
     310           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     311             :                                   eh,
     312             :                                   &handle_get_tips_finished,
     313             :                                   tgh);
     314           0 :   return tgh;
     315             : }
     316             : 
     317             : 
     318             : /**
     319             :  * Cancel GET /private/tips request.  Must not be called by clients after
     320             :  * the callback was invoked.
     321             :  *
     322             :  * @param ogh request to cancel.
     323             :  */
     324             : void
     325           0 : TALER_MERCHANT_tips_get_cancel (
     326             :   struct TALER_MERCHANT_TipsGetHandle *tgh)
     327             : {
     328           0 :   if (NULL != tgh->job)
     329           0 :     GNUNET_CURL_job_cancel (tgh->job);
     330           0 :   GNUNET_free (tgh->url);
     331           0 :   GNUNET_free (tgh);
     332           0 : }

Generated by: LCOV version 1.14