LCOV - code coverage report
Current view: top level - lib - merchant_api_history.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 25 45 55.6 %
Date: 2018-05-16 06:16:58 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V. and INRIA
       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_contract.c
      19             :  * @brief Implementation of the /history request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      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             : 
      31             : 
      32             : /**
      33             :  * @brief A Contract Operation Handle
      34             :  */
      35             : struct TALER_MERCHANT_HistoryOperation
      36             : {
      37             : 
      38             :   /**
      39             :    * The url for this request, including parameters.
      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_HistoryOperationCallback 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             :  * Cancel a pending /history request
      67             :  *
      68             :  * @param handle from the operation to cancel
      69             :  */
      70             : void
      71           9 : TALER_MERCHANT_history_cancel (struct TALER_MERCHANT_HistoryOperation *ho)
      72             : {
      73           9 :   if (NULL != ho->job)
      74             :   {
      75           0 :     GNUNET_CURL_job_cancel (ho->job);
      76           0 :     ho->job = NULL;
      77             :   }
      78           9 :   GNUNET_free (ho->url);
      79           9 :   GNUNET_free (ho);
      80           9 : }
      81             : 
      82             : 
      83             : /**
      84             :  * Function called when we're done processing the
      85             :  * HTTP /history request.
      86             :  *
      87             :  * @param cls the `struct TALER_MERCHANT_TrackTransactionHandle`
      88             :  * @param response_code HTTP response code, 0 on error
      89             :  * @param json response body, NULL if not in JSON
      90             :  */
      91             : static void
      92           9 : history_raw_cb (void *cls,
      93             :                 long response_code,
      94             :                 const json_t *json)
      95             : {
      96           9 :   struct TALER_MERCHANT_HistoryOperation *ho = cls;
      97             : 
      98           9 :   ho->job = NULL;
      99           9 :   switch (response_code)
     100             :   {
     101             :   case 0:
     102           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     103             :                 "/history returned response code 0\n");
     104             :     /**
     105             :      * The response was malformed or didn't have the
     106             :      * application/json header.
     107             :      */
     108           0 :     ho->cb (ho->cb_cls,
     109             :             response_code,
     110             :             TALER_EC_INVALID_RESPONSE,
     111             :             json);
     112           0 :     TALER_MERCHANT_history_cancel (ho);
     113           0 :     return;
     114             :   case MHD_HTTP_OK:
     115           9 :     ho->cb (ho->cb_cls,
     116             :             MHD_HTTP_OK,
     117             :             TALER_EC_NONE,
     118             :             json);
     119           9 :     TALER_MERCHANT_history_cancel (ho);
     120           9 :     return;
     121             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     122           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     123             :                 "/history URL not found\n");
     124           0 :     break;
     125             :   case MHD_HTTP_BAD_REQUEST:
     126           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     127             :                 "Wrong/missing URL parameter\n");
     128           0 :     break;
     129             :   default:
     130             :     /* unexpected response code */
     131           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     132             :                 "Unexpected response code %u\n",
     133             :                 (unsigned int) response_code);
     134           0 :     GNUNET_break (0);
     135           0 :     response_code = 0;
     136           0 :     break;
     137             :   }
     138             : 
     139           0 :   ho->cb (ho->cb_cls,
     140             :           response_code,
     141             :           TALER_JSON_get_error_code (json),
     142             :           json);
     143           0 :   TALER_MERCHANT_history_cancel (ho);
     144             : }
     145             : 
     146             : 
     147             : /**
     148             :  * Issue a /history request to the backend.
     149             :  *
     150             :  * @param ctx execution context
     151             :  * @param backend_url base URL of the merchant backend
     152             :  * @param instance which merchant instance is performing this call
     153             :  * @param start return `delta` records starting from position `start`.
     154             :  * If given as zero, then no initial skip of `start` records is done.
     155             :  * @param delta return `delta` records starting from position `start`
     156             :  * @param date only transactions younger than/equals to date will be returned
     157             :  * @param history_cb callback which will work the response gotten from the backend
     158             :  * @param history_cb_cls closure to pass to @a history_cb
     159             :  * @return handle for this operation, NULL upon errors
     160             :  */
     161             : struct TALER_MERCHANT_HistoryOperation *
     162           9 : TALER_MERCHANT_history (struct GNUNET_CURL_Context *ctx,
     163             :                         const char *backend_url,
     164             :                         const char *instance,
     165             :                         unsigned int start,
     166             :                         unsigned int delta,
     167             :                         struct GNUNET_TIME_Absolute date,
     168             :                         TALER_MERCHANT_HistoryOperationCallback history_cb,
     169             :                         void *history_cb_cls)
     170             : {
     171             :   struct TALER_MERCHANT_HistoryOperation *ho;
     172             :   uint64_t seconds;
     173             :   CURL *eh;
     174             :   char *base;
     175             : 
     176           9 :   ho = GNUNET_new (struct TALER_MERCHANT_HistoryOperation);
     177           9 :   ho->ctx = ctx;
     178           9 :   ho->cb = history_cb;
     179           9 :   ho->cb_cls = history_cb_cls;
     180           9 :   seconds = date.abs_value_us / 1000LL / 1000LL;
     181           9 :   base = TALER_url_join (backend_url, "/history", NULL);
     182           9 :   GNUNET_asprintf (&ho->url,
     183             :                    "%s?date=%llu&instance=%s&start=%d&delta=%d",
     184             :                    base,
     185             :                    seconds,
     186             :                    instance,
     187             :                    start,
     188             :                    delta);
     189           9 :   GNUNET_free (base);
     190           9 :   eh = curl_easy_init ();
     191           9 :   if (CURLE_OK != curl_easy_setopt (eh,
     192             :                                     CURLOPT_URL,
     193             :                                     ho->url))
     194             :   {
     195           0 :     GNUNET_break (0);
     196           0 :     return NULL;
     197             :   }
     198             : 
     199           9 :   if (NULL == (ho->job = GNUNET_CURL_job_add (ctx,
     200             :                                               eh,
     201             :                                               GNUNET_YES,
     202             :                                               &history_raw_cb,
     203             :                                               ho)))
     204             :   {
     205           0 :     GNUNET_break (0);
     206           0 :     return NULL;
     207             :   }
     208           9 :   return ho;
     209             : }
     210             : 
     211             : 
     212             : /* end of merchant_api_contract.c */

Generated by: LCOV version 1.13