LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-instances.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 19 26 73.1 %
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) 2019-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-instances.c
      18             :  * @brief implement GET /instances
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler-merchant-httpd_private-get-instances.h"
      23             : 
      24             : /**
      25             :  * Add merchant instance to our JSON array.
      26             :  *
      27             :  * @param cls a `json_t *` JSON array to build
      28             :  * @param key unused
      29             :  * @param value a `struct TMH_MerchantInstance *`
      30             :  * @return #GNUNET_OK (continue to iterate)
      31             :  */
      32             : static int
      33          10 : add_instance (void *cls,
      34             :               const struct GNUNET_HashCode *key,
      35             :               void *value)
      36             : {
      37          10 :   json_t *ja = cls;
      38          10 :   struct TMH_MerchantInstance *mi = value;
      39             :   json_t *pta;
      40             : 
      41             :   (void) key;
      42             :   /* Compile array of all unique wire methods supported by this
      43             :      instance */
      44          10 :   pta = json_array ();
      45          10 :   GNUNET_assert (NULL != pta);
      46          20 :   for (struct TMH_WireMethod *wm = mi->wm_head;
      47             :        NULL != wm;
      48          10 :        wm = wm->next)
      49             :   {
      50          10 :     bool duplicate = false;
      51             : 
      52          10 :     if (! wm->active)
      53           0 :       break;
      54             :     /* Yes, O(n^2), but really how many bank accounts can an
      55             :        instance realistically have for this to matter? */
      56          10 :     for (struct TMH_WireMethod *pm = mi->wm_head;
      57             :          pm != wm;
      58           0 :          pm = pm->next)
      59           0 :       if (0 == strcasecmp (pm->wire_method,
      60           0 :                            wm->wire_method))
      61             :       {
      62           0 :         duplicate = true;
      63           0 :         break;
      64             :       }
      65          10 :     if (duplicate)
      66           0 :       continue;
      67          10 :     GNUNET_assert (0 ==
      68             :                    json_array_append_new (pta,
      69             :                                           json_string (wm->wire_method)));
      70             :   }
      71          10 :   GNUNET_assert (0 ==
      72             :                  json_array_append_new (
      73             :                    ja,
      74             :                    GNUNET_JSON_PACK (
      75             :                      GNUNET_JSON_pack_string ("name",
      76             :                                               mi->settings.name),
      77             :                      GNUNET_JSON_pack_string ("id",
      78             :                                               mi->settings.id),
      79             :                      GNUNET_JSON_pack_data_auto ("merchant_pub",
      80             :                                                  &mi->merchant_pub),
      81             :                      GNUNET_JSON_pack_array_steal ("payment_targets",
      82             :                                                    pta),
      83             :                      GNUNET_JSON_pack_bool ("deleted",
      84             :                                             mi->deleted))));
      85          10 :   return GNUNET_OK;
      86             : }
      87             : 
      88             : 
      89             : /**
      90             :  * Handle a GET "/instances" request.
      91             :  *
      92             :  * @param rh context of the handler
      93             :  * @param connection the MHD connection to handle
      94             :  * @param[in,out] hc context with further information about the request
      95             :  * @return MHD result code
      96             :  */
      97             : MHD_RESULT
      98           8 : TMH_private_get_instances (const struct TMH_RequestHandler *rh,
      99             :                            struct MHD_Connection *connection,
     100             :                            struct TMH_HandlerContext *hc)
     101             : {
     102             :   json_t *ia;
     103             : 
     104             :   (void) hc;
     105           8 :   ia = json_array ();
     106           8 :   GNUNET_assert (NULL != ia);
     107           8 :   GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map,
     108             :                                          &add_instance,
     109             :                                          ia);
     110           8 :   return TALER_MHD_REPLY_JSON_PACK (
     111             :     connection,
     112             :     MHD_HTTP_OK,
     113             :     GNUNET_JSON_pack_array_steal ("instances",
     114             :                                   ia));
     115             : }
     116             : 
     117             : 
     118             : /* end of taler-merchant-httpd_private-get-instances.c */

Generated by: LCOV version 1.14