LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-reserves-ID.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 28 51 54.9 %
Date: 2021-08-30 06:54:17 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2018, 2020, 2021 Taler Systems SA
       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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-merchant-httpd_private-get-reserves-ID.c
      18             :  * @brief implement GET /reserves/$RESERVE_PUB endpoint
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <jansson.h>
      23             : #include <taler/taler_util.h>
      24             : #include <taler/taler_json_lib.h>
      25             : #include "taler-merchant-httpd.h"
      26             : #include "taler-merchant-httpd_mhd.h"
      27             : #include "taler-merchant-httpd_exchanges.h"
      28             : #include "taler-merchant-httpd_private-get-reserves-ID.h"
      29             : 
      30             : 
      31             : /**
      32             :  * Closure for handle_reserve_details().
      33             :  */
      34             : struct GetReserveContext
      35             : {
      36             :   /**
      37             :    * Connection we are handling.
      38             :    */
      39             :   struct MHD_Connection *connection;
      40             : 
      41             :   /**
      42             :    * Value to return from the callback.
      43             :    */
      44             :   MHD_RESULT res;
      45             : 
      46             :   /**
      47             :    * Should we return details about tips?
      48             :    */
      49             :   bool tips;
      50             : };
      51             : 
      52             : 
      53             : /**
      54             :  * Callback with reserve details.
      55             :  *
      56             :  * @param cls closure with a `struct GetReserveContext`
      57             :  * @param creation_time time when the reserve was setup
      58             :  * @param expiration_time time when the reserve will be closed by the exchange
      59             :  * @param merchant_initial_amount initial amount that the merchant claims to have filled the
      60             :  *           reserve with
      61             :  * @param exchange_initial_amount initial amount that the exchange claims to have received
      62             :  * @param picked_up_amount total of tips that were picked up from this reserve
      63             :  * @param committed_amount total of tips that the merchant committed to, but that were not
      64             :  *           picked up yet
      65             :  * @param active true if the reserve is still active (we have the private key)
      66             :  * @param exchange_url URL of the exchange, NULL if not active
      67             :  * @param payto_uri payto:// URI to fill the reserve, NULL if not active or already paid
      68             :  * @param tips_length length of the @a tips array
      69             :  * @param tips information about the tips created by this reserve
      70             :  */
      71             : static void
      72           2 : handle_reserve_details (void *cls,
      73             :                         struct GNUNET_TIME_Absolute creation_time,
      74             :                         struct GNUNET_TIME_Absolute expiration_time,
      75             :                         const struct TALER_Amount *merchant_initial_amount,
      76             :                         const struct TALER_Amount *exchange_initial_amount,
      77             :                         const struct TALER_Amount *picked_up_amount,
      78             :                         const struct TALER_Amount *committed_amount,
      79             :                         bool active,
      80             :                         const char *exchange_url,
      81             :                         const char *payto_uri,
      82             :                         unsigned int tips_length,
      83             :                         const struct TALER_MERCHANTDB_TipDetails *tips)
      84             : {
      85           2 :   struct GetReserveContext *ctx = cls;
      86             :   json_t *tips_json;
      87           2 :   struct GNUNET_TIME_Absolute creation_time_round = creation_time;
      88           2 :   struct GNUNET_TIME_Absolute expiration_time_round = expiration_time;
      89             : 
      90           2 :   GNUNET_TIME_round_abs (&creation_time_round);
      91           2 :   GNUNET_TIME_round_abs (&expiration_time_round);
      92             : 
      93           2 :   if (NULL != tips)
      94             :   {
      95           1 :     tips_json = json_array ();
      96           1 :     GNUNET_assert (NULL != tips_json);
      97           3 :     for (unsigned int i = 0; i<tips_length; i++)
      98             :     {
      99           2 :       GNUNET_assert (0 ==
     100             :                      json_array_append_new (
     101             :                        tips_json,
     102             :                        GNUNET_JSON_PACK (
     103             :                          GNUNET_JSON_pack_data_auto ("tip_id",
     104             :                                                      &tips[i].tip_id),
     105             :                          TALER_JSON_pack_amount ("total_amount",
     106             :                                                  &tips[i].total_amount),
     107             :                          GNUNET_JSON_pack_string ("reason",
     108             :                                                   tips[i].reason))));
     109             :     }
     110             :   }
     111             :   else
     112             :   {
     113           1 :     tips_json = NULL;
     114             :   }
     115           2 :   ctx->res = TALER_MHD_REPLY_JSON_PACK (
     116             :     ctx->connection,
     117             :     MHD_HTTP_OK,
     118             :     GNUNET_JSON_pack_time_abs ("creation_time",
     119             :                                creation_time_round),
     120             :     GNUNET_JSON_pack_time_abs ("expiration_time",
     121             :                                expiration_time_round),
     122             :     TALER_JSON_pack_amount ("merchant_initial_amount",
     123             :                             merchant_initial_amount),
     124             :     TALER_JSON_pack_amount ("exchange_initial_amount",
     125             :                             exchange_initial_amount),
     126             :     TALER_JSON_pack_amount ("pickup_amount",
     127             :                             picked_up_amount),
     128             :     TALER_JSON_pack_amount ("committed_amount",
     129             :                             committed_amount),
     130             :     GNUNET_JSON_pack_allow_null (
     131             :       GNUNET_JSON_pack_array_steal ("tips",
     132             :                                     tips_json)),
     133             :     GNUNET_JSON_pack_bool ("active",
     134             :                            active),
     135             :     GNUNET_JSON_pack_allow_null (
     136             :       GNUNET_JSON_pack_string ("exchange_url",
     137             :                                exchange_url)),
     138             :     GNUNET_JSON_pack_allow_null (
     139             :       GNUNET_JSON_pack_string ("payto_uri",
     140             :                                payto_uri)));
     141           2 : }
     142             : 
     143             : 
     144             : /**
     145             :  * Manages a GET /reserves/$RESERVE_PUB call.
     146             :  *
     147             :  * @param rh context of the handler
     148             :  * @param connection the MHD connection to handle
     149             :  * @param[in,out] hc context with further information about the request
     150             :  * @return MHD result code
     151             :  */
     152             : MHD_RESULT
     153           2 : TMH_private_get_reserves_ID (const struct TMH_RequestHandler *rh,
     154             :                              struct MHD_Connection *connection,
     155             :                              struct TMH_HandlerContext *hc)
     156             : {
     157             :   struct TALER_ReservePublicKeyP reserve_pub;
     158             :   bool tips;
     159             : 
     160           2 :   if (GNUNET_OK !=
     161           2 :       GNUNET_STRINGS_string_to_data (hc->infix,
     162           2 :                                      strlen (hc->infix),
     163             :                                      &reserve_pub,
     164             :                                      sizeof (reserve_pub)))
     165             :   {
     166             :     /* tip_id has wrong encoding */
     167           0 :     GNUNET_break_op (0);
     168           0 :     return TALER_MHD_reply_with_error (connection,
     169             :                                        MHD_HTTP_BAD_REQUEST,
     170             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     171           0 :                                        hc->infix);
     172             :   }
     173             :   {
     174             :     const char *tstr;
     175             : 
     176           2 :     tstr = MHD_lookup_connection_value (connection,
     177             :                                         MHD_GET_ARGUMENT_KIND,
     178             :                                         "tips");
     179           2 :     tips = (NULL != tstr)
     180           2 :            ? 0 == strcasecmp (tstr, "yes")
     181           2 :            : false;
     182             :   }
     183             :   {
     184           2 :     struct GetReserveContext ctx = {
     185             :       .connection = connection,
     186             :       .tips = tips
     187             :     };
     188             :     enum GNUNET_DB_QueryStatus qs;
     189             : 
     190           2 :     TMH_db->preflight (TMH_db->cls);
     191           2 :     qs = TMH_db->lookup_reserve (TMH_db->cls,
     192           2 :                                  hc->instance->settings.id,
     193             :                                  &reserve_pub,
     194             :                                  tips,
     195             :                                  &handle_reserve_details,
     196             :                                  &ctx);
     197           2 :     if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     198             :     {
     199             :       unsigned int response_code;
     200             :       enum TALER_ErrorCode ec;
     201             : 
     202           0 :       switch (qs)
     203             :       {
     204           0 :       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     205           0 :         ec = TALER_EC_MERCHANT_GENERIC_TIP_ID_UNKNOWN;
     206           0 :         response_code = MHD_HTTP_NOT_FOUND;
     207           0 :         break;
     208           0 :       case GNUNET_DB_STATUS_SOFT_ERROR:
     209           0 :         ec = TALER_EC_GENERIC_DB_SOFT_FAILURE;
     210           0 :         response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
     211           0 :         break;
     212           0 :       case GNUNET_DB_STATUS_HARD_ERROR:
     213           0 :         ec = TALER_EC_GENERIC_DB_FETCH_FAILED;
     214           0 :         response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
     215           0 :         break;
     216           0 :       default:
     217           0 :         GNUNET_break (0);
     218           0 :         ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     219           0 :         response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
     220           0 :         break;
     221             :       }
     222           0 :       return TALER_MHD_reply_with_error (connection,
     223             :                                          response_code,
     224             :                                          ec,
     225           0 :                                          hc->infix);
     226             :     }
     227           2 :     return ctx.res;
     228             :   }
     229             : }
     230             : 
     231             : 
     232             : /* end of taler-merchant-httpd_private-get-reserves-ID.c */

Generated by: LCOV version 1.14