LCOV - code coverage report
Current view: top level - lib - merchant_api_get_tips.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 90 0.0 %
Date: 2022-08-25 06:17:04 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 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a GET /private/tips operation.
      36             :  */
      37             : struct TALER_MERCHANT_TipsGetHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_TipsGetCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Parse tip information from @a ia.
      69             :  *
      70             :  * @param ia JSON array (or NULL!) tip order data
      71             :  * @param tgh operation handle
      72             :  * @return #GNUNET_OK on success
      73             :  */
      74             : static int
      75           0 : parse_tips (const json_t *ia,
      76             :             struct TALER_MERCHANT_TipsGetHandle *tgh)
      77           0 : {
      78           0 :   unsigned int tes_len = json_array_size (ia);
      79           0 :   struct TALER_MERCHANT_TipEntry tes[tes_len];
      80             :   size_t index;
      81             :   json_t *value;
      82             :   int ret;
      83             : 
      84           0 :   ret = GNUNET_OK;
      85           0 :   json_array_foreach (ia, index, value) {
      86           0 :     struct TALER_MERCHANT_TipEntry *ie = &tes[index];
      87             :     struct GNUNET_JSON_Specification spec[] = {
      88           0 :       GNUNET_JSON_spec_uint64 ("row_id",
      89             :                                &ie->row_id),
      90           0 :       GNUNET_JSON_spec_fixed_auto ("tip_id",
      91             :                                    &ie->tip_id),
      92           0 :       TALER_JSON_spec_amount_any ("tip_amount",
      93             :                                   &ie->tip_amount),
      94           0 :       GNUNET_JSON_spec_end ()
      95             :     };
      96             : 
      97           0 :     if (GNUNET_OK !=
      98           0 :         GNUNET_JSON_parse (value,
      99             :                            spec,
     100             :                            NULL, NULL))
     101             :     {
     102           0 :       GNUNET_break_op (0);
     103           0 :       ret = GNUNET_SYSERR;
     104           0 :       continue;
     105             :     }
     106           0 :     if (GNUNET_SYSERR == ret)
     107           0 :       break;
     108             :   }
     109           0 :   if (GNUNET_OK == ret)
     110             :   {
     111           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     112             :       .http_status = MHD_HTTP_OK
     113             :     };
     114             : 
     115           0 :     tgh->cb (tgh->cb_cls,
     116             :              &hr,
     117             :              tes_len,
     118             :              tes);
     119           0 :     tgh->cb = NULL; /* just to be sure */
     120             :   }
     121           0 :   return ret;
     122             : }
     123             : 
     124             : 
     125             : /**
     126             :  * Function called when we're done processing the
     127             :  * HTTP GET /private/tips request.
     128             :  *
     129             :  * @param cls the `struct TALER_MERCHANT_TipsGetHandle`
     130             :  * @param response_code HTTP response code, 0 on error
     131             :  * @param response response body, NULL if not in JSON
     132             :  */
     133             : static void
     134           0 : handle_get_tips_finished (void *cls,
     135             :                           long response_code,
     136             :                           const void *response)
     137             : {
     138           0 :   struct TALER_MERCHANT_TipsGetHandle *tgh = cls;
     139           0 :   const json_t *json = response;
     140           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     141           0 :     .http_status = (unsigned int) response_code,
     142             :     .reply = json
     143             :   };
     144             : 
     145           0 :   tgh->job = NULL;
     146           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     147             :               "Got /private/tips response with status code %u\n",
     148             :               (unsigned int) response_code);
     149           0 :   switch (response_code)
     150             :   {
     151           0 :   case MHD_HTTP_OK:
     152             :     {
     153             :       json_t *tips;
     154             :       struct GNUNET_JSON_Specification spec[] = {
     155           0 :         GNUNET_JSON_spec_json ("tips",
     156             :                                &tips),
     157           0 :         GNUNET_JSON_spec_end ()
     158             :       };
     159             : 
     160           0 :       if (GNUNET_OK !=
     161           0 :           GNUNET_JSON_parse (json,
     162             :                              spec,
     163             :                              NULL, NULL))
     164             :       {
     165           0 :         hr.http_status = 0;
     166           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     167             :       }
     168             :       else
     169             :       {
     170           0 :         if ( (! json_is_array (tips)) ||
     171             :              (GNUNET_OK ==
     172           0 :               parse_tips (tips,
     173             :                           tgh)) )
     174             :         {
     175           0 :           GNUNET_JSON_parse_free (spec);
     176           0 :           TALER_MERCHANT_tips_get_cancel (tgh);
     177           0 :           return;
     178             :         }
     179             :         else
     180             :         {
     181           0 :           hr.http_status = 0;
     182           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     183             :         }
     184             :       }
     185           0 :       GNUNET_JSON_parse_free (spec);
     186           0 :       break;
     187             :     }
     188           0 :   case MHD_HTTP_UNAUTHORIZED:
     189           0 :     hr.ec = TALER_JSON_get_error_code (json);
     190           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     191             :     /* Nothing really to verify, merchant says we need to authenticate. */
     192           0 :     break;
     193           0 :   default:
     194             :     /* unexpected response code */
     195           0 :     hr.ec = TALER_JSON_get_error_code (json);
     196           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     197           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                 "Unexpected response code %u/%d\n",
     199             :                 (unsigned int) response_code,
     200             :                 (int) hr.ec);
     201           0 :     break;
     202             :   }
     203           0 :   tgh->cb (tgh->cb_cls,
     204             :            &hr,
     205             :            0,
     206             :            NULL);
     207           0 :   TALER_MERCHANT_tips_get_cancel (tgh);
     208             : }
     209             : 
     210             : 
     211             : struct TALER_MERCHANT_TipsGetHandle *
     212           0 : TALER_MERCHANT_tips_get (
     213             :   struct GNUNET_CURL_Context *ctx,
     214             :   const char *backend_url,
     215             :   TALER_MERCHANT_TipsGetCallback cb,
     216             :   void *cb_cls)
     217             : {
     218           0 :   return TALER_MERCHANT_tips_get2 (ctx,
     219             :                                    backend_url,
     220             :                                    TALER_EXCHANGE_YNA_NO,
     221             :                                    -20,
     222             :                                    UINT64_MAX,
     223             :                                    cb,
     224             :                                    cb_cls);
     225             : }
     226             : 
     227             : 
     228             : struct TALER_MERCHANT_TipsGetHandle *
     229           0 : TALER_MERCHANT_tips_get2 (struct GNUNET_CURL_Context *ctx,
     230             :                           const char *backend_url,
     231             :                           enum TALER_EXCHANGE_YesNoAll expired,
     232             :                           int64_t limit,
     233             :                           uint64_t offset,
     234             :                           TALER_MERCHANT_TipsGetCallback cb,
     235             :                           void *cb_cls)
     236             : {
     237             :   struct TALER_MERCHANT_TipsGetHandle *tgh;
     238             :   CURL *eh;
     239             : 
     240           0 :   GNUNET_assert (NULL != backend_url);
     241           0 :   if (0 == limit)
     242             :   {
     243           0 :     GNUNET_break (0);
     244           0 :     return NULL;
     245             :   }
     246           0 :   tgh = GNUNET_new (struct TALER_MERCHANT_TipsGetHandle);
     247           0 :   tgh->ctx = ctx;
     248           0 :   tgh->cb = cb;
     249           0 :   tgh->cb_cls = cb_cls;
     250             : 
     251             :   /* build tgh->url with the various optional arguments */
     252             :   {
     253             :     char cbuf[30];
     254             :     char lbuf[30];
     255             :     bool have_offset;
     256             : 
     257           0 :     GNUNET_snprintf (lbuf,
     258             :                      sizeof (lbuf),
     259             :                      "%lld",
     260             :                      (long long) limit);
     261             : 
     262           0 :     if (limit > 0)
     263           0 :       have_offset = (0 != offset);
     264             :     else
     265           0 :       have_offset = (UINT64_MAX != offset);
     266           0 :     GNUNET_snprintf (cbuf,
     267             :                      sizeof (cbuf),
     268             :                      "%llu",
     269             :                      (unsigned long long) offset);
     270           0 :     tgh->url = TALER_url_join (backend_url,
     271             :                                "private/tips",
     272             :                                "expired",
     273             :                                (TALER_EXCHANGE_YNA_ALL != expired)
     274           0 :                                ? TALER_yna_to_string (expired)
     275             :                                : NULL,
     276             :                                "offset", (have_offset) ? cbuf : NULL,
     277             :                                "limit", (-20 != limit) ? lbuf : NULL,
     278             :                                NULL);
     279             :   }
     280           0 :   if (NULL == tgh->url)
     281             :   {
     282           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     283             :                 "Could not construct request URL.\n");
     284           0 :     GNUNET_free (tgh);
     285           0 :     return NULL;
     286             :   }
     287           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     288             :               "Requesting URL '%s'\n",
     289             :               tgh->url);
     290           0 :   eh = TALER_MERCHANT_curl_easy_get_ (tgh->url);
     291           0 :   tgh->job = GNUNET_CURL_job_add (ctx,
     292             :                                   eh,
     293             :                                   &handle_get_tips_finished,
     294             :                                   tgh);
     295           0 :   return tgh;
     296             : }
     297             : 
     298             : 
     299             : void
     300           0 : TALER_MERCHANT_tips_get_cancel (
     301             :   struct TALER_MERCHANT_TipsGetHandle *tgh)
     302             : {
     303           0 :   if (NULL != tgh->job)
     304           0 :     GNUNET_CURL_job_cancel (tgh->job);
     305           0 :   GNUNET_free (tgh->url);
     306           0 :   GNUNET_free (tgh);
     307           0 : }

Generated by: LCOV version 1.14