summaryrefslogtreecommitdiff
path: root/rest/rest-params.c
blob: a5666fa0b1615f19192750166723d86672f37bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * librest - RESTful web services access
 * Copyright (c) 2008, 2009, Intel Corporation.
 *
 * Authors: Rob Bradford <rob@linux.intel.com>
 *          Ross Burton <ross@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <config.h>
#include <glib-object.h>
#include "rest-params.h"

/**
 * SECTION:rest-params
 * @short_description: Container for call parameters
 * @see_also: #RestParam, #RestProxyCall.
 */

/*
 * RestParams is an alias for GHashTable achieved by opaque types in the public
 * headers and casting internally. This has several limitations, mainly
 * supporting multiple parameters with the same name and preserving the ordering
 * of parameters.
 *
 * These are not requirements for the bulk of the web services, but this
 * limitation does mean librest can't be used for a few web services.
 *
 * TODO: this should be a list to support multiple parameters with the same
 * name.
 */

/**
 * rest_params_new:
 *
 * Create a new #RestParams.
 *
 * Returns: A empty #RestParams.
 **/
RestParams *
rest_params_new (void)
{
  /* The key is a string that is owned by the RestParam, so we don't need to
     explicitly free it on removal. */
  return (RestParams *)
    g_hash_table_new_full (g_str_hash, g_str_equal,
                           NULL, (GDestroyNotify)rest_param_unref);
}

/**
 * rest_params_free:
 * @params: a valid #RestParams
 *
 * Destroy the #RestParams and the #RestParam objects that it contains.
 **/
void
rest_params_free (RestParams *params)
{
  GHashTable *hash = (GHashTable *)params;

  g_return_if_fail (params);

  g_hash_table_destroy (hash);
}

/**
 * rest_params_add:
 * @params: a valid #RestParams
 * @param: a valid #RestParam
 *
 * Add @param to @params.
 **/
void
rest_params_add (RestParams *params, RestParam *param)
{
  GHashTable *hash = (GHashTable *)params;

  g_return_if_fail (params);
  g_return_if_fail (param);

  g_hash_table_insert (hash, (gpointer)rest_param_get_name (param), param);
}

/**
 * rest_params_get:
 * @params: a valid #RestParams
 * @name: a parameter name
 *
 * Return the #RestParam called @name, or %NULL if it doesn't exist.
 *
 * Returns: a #RestParam or %NULL if the name doesn't exist
 **/
RestParam *
rest_params_get (RestParams *params, const char *name)
{
  GHashTable *hash = (GHashTable *)params;

  g_return_val_if_fail (params, NULL);
  g_return_val_if_fail (name, NULL);

  return g_hash_table_lookup (hash, name);
}

/**
 * rest_params_remove:
 * @params: a valid #RestParams
 * @name: a parameter name
 *
 * Remove the #RestParam called @name.
 **/
void
rest_params_remove (RestParams *params, const char *name)
{
  GHashTable *hash = (GHashTable *)params;

  g_return_if_fail (params);
  g_return_if_fail (name);

  g_hash_table_remove (hash, name);
}

/**
 * rest_params_are_strings:
 * @params: a valid #RestParams
 *
 * Checks if the parameters are all simple strings (have a content type of
 * "text/plain").
 *
 * Returns: %TRUE if all of the parameters are simple strings, %FALSE otherwise.
 **/
gboolean
rest_params_are_strings (RestParams *params)
{
  GHashTable *hash = (GHashTable *)params;
  GHashTableIter iter;
  RestParam *param;

  g_return_val_if_fail (params, FALSE);

  g_hash_table_iter_init (&iter, hash);
  while (g_hash_table_iter_next (&iter, NULL, (gpointer)&param)) {
    if (!rest_param_is_string (param))
      return FALSE;
  }

  return TRUE;

}

/**
 * rest_params_as_string_hash_table:
 * @params: a valid #RestParams
 *
 * Create a new #GHashTable which contains the name and value of all string
 * (content type of text/plain) parameters.
 *
 * The values are owned by the #RestParams, so don't destroy the #RestParams
 * before the hash table.
 *
 * Returns: (element-type utf8 Rest.Param) (transfer container): a new #GHashTable.
 **/
GHashTable *
rest_params_as_string_hash_table (RestParams *params)
{
  GHashTable *hash, *strings;
  GHashTableIter iter;
  const char *name = NULL;
  RestParam *param = NULL;

  g_return_val_if_fail (params, NULL);

  hash = (GHashTable *)params;
  strings = g_hash_table_new (g_str_hash, g_str_equal);

  g_hash_table_iter_init (&iter, hash);
  while (g_hash_table_iter_next (&iter, (gpointer)&name, (gpointer)&param)) {
    if (rest_param_is_string (param))
      g_hash_table_insert (strings, (gpointer)name, (gpointer)rest_param_get_content (param));
  }

  return strings;
}

/**
 * rest_params_iter_init:
 * @iter: an uninitialized #RestParamsIter
 * @params: a valid #RestParams
 *
 * Initialize a parameter iterator over @params. Modifying @params after calling
 * this function invalidates the returned iterator.
 * |[
 * RestParamsIter iter;
 * const char *name;
 * RestParam *param;
 *
 * rest_params_iter_init (&iter, params);
 * while (rest_params_iter_next (&iter, &name, &param)) {
 *   /&ast; do something with name and param &ast;/
 * }
 * ]|
 **/
void
rest_params_iter_init (RestParamsIter *iter, RestParams *params)
{
  g_return_if_fail (iter);
  g_return_if_fail (params);

  g_hash_table_iter_init ((GHashTableIter *)iter, (GHashTable *)params);
}

/**
 * rest_params_iter_next:
 * @iter: an initialized #RestParamsIter
 * @name: a location to store the name, or %NULL
 * @param: a location to store the #RestParam, or %NULL
 *
 * Advances @iter and retrieves the name and/or parameter that are now pointed
 * at as a result of this advancement.  If FALSE is returned, @name and @param
 * are not set and the iterator becomes invalid.
 *
 * Returns: %FALSE if the end of the #RestParams has been reached, %TRUE otherwise.
 **/
gboolean
rest_params_iter_next (RestParamsIter *iter, const char **name, RestParam **param)
{
  g_return_val_if_fail (iter, FALSE);

  return g_hash_table_iter_next ((GHashTableIter *)iter, (gpointer)name, (gpointer)param);
}