Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2019-2023 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 enum GNUNET_GenericReturnValue
33 4 : add_instance (void *cls,
34 : const struct GNUNET_HashCode *key,
35 : void *value)
36 : {
37 4 : json_t *ja = cls;
38 4 : 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 4 : pta = json_array ();
45 4 : GNUNET_assert (NULL != pta);
46 4 : for (struct TMH_WireMethod *wm = mi->wm_head;
47 6 : NULL != wm;
48 2 : wm = wm->next)
49 : {
50 2 : bool duplicate = false;
51 :
52 2 : 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 2 : for (struct TMH_WireMethod *pm = mi->wm_head;
57 2 : 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 2 : if (duplicate)
66 0 : continue;
67 2 : GNUNET_assert (0 ==
68 : json_array_append_new (pta,
69 : json_string (wm->wire_method)));
70 : }
71 4 : 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_allow_null (
78 : GNUNET_JSON_pack_string ("website",
79 : mi->settings.website)),
80 : GNUNET_JSON_pack_allow_null (
81 : GNUNET_JSON_pack_string ("logo",
82 : mi->settings.logo)),
83 : GNUNET_JSON_pack_string ("id",
84 : mi->settings.id),
85 : GNUNET_JSON_pack_data_auto ("merchant_pub",
86 : &mi->merchant_pub),
87 : GNUNET_JSON_pack_array_steal ("payment_targets",
88 : pta),
89 : GNUNET_JSON_pack_bool ("deleted",
90 : mi->deleted))));
91 4 : return GNUNET_OK;
92 : }
93 :
94 :
95 : /**
96 : * Handle a GET "/instances" request.
97 : *
98 : * @param rh context of the handler
99 : * @param connection the MHD connection to handle
100 : * @param[in,out] hc context with further information about the request
101 : * @return MHD result code
102 : */
103 : MHD_RESULT
104 5 : TMH_private_get_instances (const struct TMH_RequestHandler *rh,
105 : struct MHD_Connection *connection,
106 : struct TMH_HandlerContext *hc)
107 : {
108 : json_t *ia;
109 :
110 : (void) rh;
111 : (void) hc;
112 5 : ia = json_array ();
113 5 : GNUNET_assert (NULL != ia);
114 5 : GNUNET_CONTAINER_multihashmap_iterate (TMH_by_id_map,
115 : &add_instance,
116 : ia);
117 5 : return TALER_MHD_REPLY_JSON_PACK (
118 : connection,
119 : MHD_HTTP_OK,
120 : GNUNET_JSON_pack_array_steal ("instances",
121 : ia));
122 : }
123 :
124 :
125 : /* end of taler-merchant-httpd_private-get-instances.c */
|