2
* librest - RESTful web services access
3
* Copyright (c) 2008, 2009, Intel Corporation.
5
* Authors: Rob Bradford <rob@linux.intel.com>
6
* Ross Burton <ross@linux.intel.com>
8
* This program is free software; you can redistribute it and/or modify it
9
* under the terms and conditions of the GNU Lesser General Public License,
10
* version 2.1, as published by the Free Software Foundation.
12
* This program is distributed in the hope it will be useful, but WITHOUT ANY
13
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with this program; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24
#include <glib-object.h>
25
#include "rest-params.h"
29
* @short_description: Container for call parameters
30
* @see_also: #RestParam, #RestProxyCall.
34
* RestParams is an alias for GHashTable achieved by opaque types in the public
35
* headers and casting internally. This has several limitations, mainly
36
* supporting multiple parameters with the same name and preserving the ordering
39
* These are not requirements for the bulk of the web services, but this
40
* limitation does mean librest can't be used for a few web services.
42
* TODO: this should be a list to support multiple parameters with the same
49
* Create a new #RestParams.
51
* Returns: A empty #RestParams.
54
rest_params_new (void)
56
/* The key is a string that is owned by the RestParam, so we don't need to
57
explicitly free it on removal. */
59
g_hash_table_new_full (g_str_hash, g_str_equal,
60
NULL, (GDestroyNotify)rest_param_unref);
65
* @params: a valid #RestParams
67
* Destroy the #RestParams and the #RestParam objects that it contains.
70
rest_params_free (RestParams *params)
72
GHashTable *hash = (GHashTable *)params;
74
g_return_if_fail (params);
76
g_hash_table_destroy (hash);
81
* @params: a valid #RestParams
82
* @param: a valid #RestParam
84
* Add @param to @params.
87
rest_params_add (RestParams *params, RestParam *param)
89
GHashTable *hash = (GHashTable *)params;
91
g_return_if_fail (params);
92
g_return_if_fail (param);
94
g_hash_table_insert (hash, (gpointer)rest_param_get_name (param), param);
99
* @params: a valid #RestParams
100
* @name: a parameter name
102
* Return the #RestParam called @name, or %NULL if it doesn't exist.
104
* Returns: a #RestParam or %NULL if the name doesn't exist
107
rest_params_get (RestParams *params, const char *name)
109
GHashTable *hash = (GHashTable *)params;
111
g_return_val_if_fail (params, NULL);
112
g_return_val_if_fail (name, NULL);
114
return g_hash_table_lookup (hash, name);
118
* rest_params_remove:
119
* @params: a valid #RestParams
120
* @name: a parameter name
122
* Remove the #RestParam called @name.
125
rest_params_remove (RestParams *params, const char *name)
127
GHashTable *hash = (GHashTable *)params;
129
g_return_if_fail (params);
130
g_return_if_fail (name);
132
g_hash_table_remove (hash, name);
136
* rest_params_are_strings:
137
* @params: a valid #RestParams
139
* Checks if the parameters are all simple strings (have a content type of
142
* Returns: %TRUE if all of the parameters are simple strings, %FALSE otherwise.
145
rest_params_are_strings (RestParams *params)
147
GHashTable *hash = (GHashTable *)params;
151
g_return_val_if_fail (params, FALSE);
153
g_hash_table_iter_init (&iter, hash);
154
while (g_hash_table_iter_next (&iter, NULL, (gpointer)¶m)) {
155
if (!rest_param_is_string (param))
164
* rest_params_as_string_hash_table:
165
* @params: a valid #RestParams
167
* Create a new #GHashTable which contains the name and value of all string
168
* (content type of text/plain) parameters.
170
* The values are owned by the #RestParams, so don't destroy the #RestParams
171
* before the hash table.
173
* Returns: a new #GHashTable.
176
rest_params_as_string_hash_table (RestParams *params)
178
GHashTable *hash, *strings;
180
const char *name = NULL;
181
RestParam *param = NULL;
183
g_return_val_if_fail (params, NULL);
185
hash = (GHashTable *)params;
186
strings = g_hash_table_new (g_str_hash, g_str_equal);
188
g_hash_table_iter_init (&iter, hash);
189
while (g_hash_table_iter_next (&iter, (gpointer)&name, (gpointer)¶m)) {
190
if (rest_param_is_string (param))
191
g_hash_table_insert (strings, (gpointer)name, (gpointer)rest_param_get_content (param));
198
* rest_params_iter_init:
199
* @iter: an uninitialized #RestParamsIter
200
* @params: a valid #RestParams
202
* Initialize a parameter iterator over @params. Modifying @params after calling
203
* this function invalidates the returned iterator.
205
* RestParamsIter iter;
209
* rest_params_iter_init (&iter, params);
210
* while (rest_params_iter_next (&iter, &name, ¶m)) {
211
* /* do something with name and param */
216
rest_params_iter_init (RestParamsIter *iter, RestParams *params)
218
g_return_if_fail (iter);
219
g_return_if_fail (params);
221
g_hash_table_iter_init ((GHashTableIter *)iter, (GHashTable *)params);
225
* rest_params_iter_next:
226
* @iter: an initialized #RestParamsIter
227
* @name: a location to store the name, or %NULL
228
* @param: a location to store the #RestParam, or %NULL
230
* Advances @iter and retrieves the name and/or parameter that are now pointed
231
* at as a result of this advancement. If FALSE is returned, @name and @param
232
* are not set and the iterator becomes invalid.
234
* Returns: %FALSE if the end of the #RestParams has been reached, %TRUE otherwise.
237
rest_params_iter_next (RestParamsIter *iter, const char **name, RestParam **param)
239
g_return_val_if_fail (iter, FALSE);
241
return g_hash_table_iter_next ((GHashTableIter *)iter, (gpointer)name, (gpointer)param);