LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_reserve_status.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 21 26 80.8 %
Date: 2017-09-17 17:24:28 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, 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 Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-exchange-httpd_reserve_status.c
      18             :  * @brief Handle /reserve/status requests
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <jansson.h>
      26             : #include "taler-exchange-httpd_reserve_status.h"
      27             : #include "taler-exchange-httpd_parsing.h"
      28             : #include "taler-exchange-httpd_responses.h"
      29             : #include "taler-exchange-httpd_keystate.h"
      30             : 
      31             : 
      32             : /**
      33             :  * Send reserve status information to client.
      34             :  *
      35             :  * @param connection connection to the client
      36             :  * @param rh reserve history to return
      37             :  * @return MHD result code
      38             :  */
      39             : static int
      40           2 : reply_reserve_status_success (struct MHD_Connection *connection,
      41             :                               const struct TALER_EXCHANGEDB_ReserveHistory *rh)
      42             : {
      43             :   json_t *json_balance;
      44             :   json_t *json_history;
      45             :   struct TALER_Amount balance;
      46             : 
      47           2 :   json_history = TEH_RESPONSE_compile_reserve_history (rh,
      48             :                                                        &balance);
      49           2 :   if (NULL == json_history)
      50           0 :     return TEH_RESPONSE_reply_internal_error (connection,
      51             :                                               TALER_EC_RESERVE_STATUS_DB_ERROR,
      52             :                                               "balance calculation failure");
      53           2 :   json_balance = TALER_JSON_from_amount (&balance);
      54           2 :   return TEH_RESPONSE_reply_json_pack (connection,
      55             :                                        MHD_HTTP_OK,
      56             :                                        "{s:o, s:o}",
      57             :                                        "balance", json_balance,
      58             :                                        "history", json_history);
      59             : }
      60             : 
      61             : 
      62             : /**
      63             :  * Closure for #reserve_status_transaction.
      64             :  */
      65             : struct ReserveStatusContext
      66             : {
      67             :   /**
      68             :    * Public key of the reserve the inquiry is about.
      69             :    */
      70             :   struct TALER_ReservePublicKeyP reserve_pub;
      71             : 
      72             :   /**
      73             :    * History of the reserve, set in the callback.
      74             :    */
      75             :   struct TALER_EXCHANGEDB_ReserveHistory *rh;
      76             : 
      77             : };
      78             : 
      79             : 
      80             : /**
      81             :  * Function implementing /reserve/status transaction.  
      82             :  * Execute a /reserve/status.  Given the public key of a reserve,
      83             :  * return the associated transaction history.  Runs the
      84             :  * transaction logic; IF it returns a non-error code, the transaction
      85             :  * logic MUST NOT queue a MHD response.  IF it returns an hard error,
      86             :  * the transaction logic MUST queue a MHD response and set @a mhd_ret.
      87             :  * IF it returns the soft error code, the function MAY be called again
      88             :  * to retry and MUST not queue a MHD response.
      89             :  *
      90             :  * @param cls a `struct ReserveStatusContext *`
      91             :  * @param connection MHD request which triggered the transaction
      92             :  * @param session database session to use
      93             :  * @param[out] mhd_ret set to MHD response status for @a connection,
      94             :  *             if transaction failed (!)
      95             :  * @return transaction status
      96             :  */
      97             : static enum GNUNET_DB_QueryStatus
      98           2 : reserve_status_transaction (void *cls,
      99             :                             struct MHD_Connection *connection,
     100             :                             struct TALER_EXCHANGEDB_Session *session,
     101             :                             int *mhd_ret)
     102             : {
     103           2 :   struct ReserveStatusContext *rsc = cls;
     104             : 
     105           4 :   return TEH_plugin->get_reserve_history (TEH_plugin->cls,
     106             :                                           session,
     107           2 :                                           &rsc->reserve_pub,
     108             :                                           &rsc->rh);
     109             : }
     110             : 
     111             : 
     112             : /**
     113             :  * Handle a "/reserve/status" request.  Parses the
     114             :  * given "reserve_pub" argument (which should contain the
     115             :  * EdDSA public key of a reserve) and then respond with the
     116             :  * status of the reserve.
     117             :  *
     118             :  * @param rh context of the handler
     119             :  * @param connection the MHD connection to handle
     120             :  * @param[in,out] connection_cls the connection's closure (can be updated)
     121             :  * @param upload_data upload data
     122             :  * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
     123             :  * @return MHD result code
     124             :  */
     125             : int
     126           2 : TEH_RESERVE_handler_reserve_status (struct TEH_RequestHandler *rh,
     127             :                                     struct MHD_Connection *connection,
     128             :                                     void **connection_cls,
     129             :                                     const char *upload_data,
     130             :                                     size_t *upload_data_size)
     131             : {
     132             :   struct ReserveStatusContext rsc;
     133             :   int res;
     134             :   int mhd_ret;
     135             : 
     136           2 :   res = TEH_PARSE_mhd_request_arg_data (connection,
     137             :                                         "reserve_pub",
     138             :                                         &rsc.reserve_pub,
     139             :                                         sizeof (struct TALER_ReservePublicKeyP));
     140           2 :   if (GNUNET_SYSERR == res)
     141           0 :     return MHD_NO; /* internal error */
     142           2 :   if (GNUNET_NO == res)
     143           0 :     return MHD_YES; /* parse error */
     144           2 :   rsc.rh = NULL;
     145           2 :   if (GNUNET_OK !=
     146           2 :       TEH_DB_run_transaction (connection,
     147             :                               &mhd_ret,
     148             :                               &reserve_status_transaction,
     149             :                               &rsc))
     150           0 :     return mhd_ret;
     151             : 
     152             :   /* generate proper response */
     153           2 :   if (NULL == rsc.rh)
     154           0 :     return TEH_RESPONSE_reply_json_pack (connection,
     155             :                                          MHD_HTTP_NOT_FOUND,
     156             :                                          "{s:s, s:s}",
     157             :                                          "error", "Reserve not found",
     158             :                                          "parameter", "withdraw_pub");
     159           2 :   mhd_ret = reply_reserve_status_success (connection,
     160           2 :                                           rsc.rh);
     161           2 :   TEH_plugin->free_reserve_history (TEH_plugin->cls,
     162             :                                     rsc.rh);
     163           2 :   return mhd_ret;
     164             : }
     165             : 
     166             : 
     167             : /* end of taler-exchange-httpd_reserve_status.c */

Generated by: LCOV version 1.13