diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-26 14:57:05 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-26 14:57:05 +0000 |
commit | b68a1cacd29fb91dee84f736085c48db4ea5acaf (patch) | |
tree | 16b343e23754b0eec27f927e877e0f457955c105 /src/rest | |
parent | 496e08d0b3e7719b4bf63eb9e0a3e0f8f19d6569 (diff) | |
download | gnunet-b68a1cacd29fb91dee84f736085c48db4ea5acaf.tar.gz gnunet-b68a1cacd29fb91dee84f736085c48db4ea5acaf.zip |
-more REST
Diffstat (limited to 'src/rest')
-rw-r--r-- | src/rest/rest.c | 199 |
1 files changed, 187 insertions, 12 deletions
diff --git a/src/rest/rest.c b/src/rest/rest.c index ec46e1273..d6fb8a2f3 100644 --- a/src/rest/rest.c +++ b/src/rest/rest.c | |||
@@ -50,7 +50,7 @@ struct JsonApiResource | |||
50 | }; | 50 | }; |
51 | 51 | ||
52 | 52 | ||
53 | struct JsonApiResponse | 53 | struct JsonApiObject |
54 | { | 54 | { |
55 | /** | 55 | /** |
56 | * DLL Resource | 56 | * DLL Resource |
@@ -135,26 +135,163 @@ GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource, | |||
135 | return GNUNET_OK; | 135 | return GNUNET_OK; |
136 | } | 136 | } |
137 | 137 | ||
138 | /** | ||
139 | * Read a JSON API attribute | ||
140 | * | ||
141 | * @param res the JSON resource | ||
142 | * @param key the key for the attribute | ||
143 | * @return the json_t object | ||
144 | */ | ||
145 | json_t* | ||
146 | GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource, | ||
147 | const char* key) | ||
148 | { | ||
149 | if ( (NULL == resource) || | ||
150 | (NULL == key)) | ||
151 | return NULL; | ||
152 | return json_object_get (resource->res_obj, key); | ||
153 | } | ||
154 | |||
155 | int | ||
156 | check_resource_attr_str (const struct JsonApiResource *resource, | ||
157 | const char* key, | ||
158 | const char* attr) | ||
159 | { | ||
160 | json_t *value; | ||
161 | if ( (NULL == resource) || | ||
162 | (NULL == key) || | ||
163 | (NULL == attr)) | ||
164 | return GNUNET_NO; | ||
165 | value = json_object_get (resource->res_obj, key); | ||
166 | if (NULL == value) | ||
167 | return GNUNET_NO; | ||
168 | if (!json_is_string (value) || | ||
169 | (0 != strcmp (attr, json_string_value(value)))) | ||
170 | { | ||
171 | json_decref (value); | ||
172 | return GNUNET_NO; | ||
173 | } | ||
174 | json_decref (value); | ||
175 | return GNUNET_YES; | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Check a JSON API resource id | ||
180 | * | ||
181 | * @param res the JSON resource | ||
182 | * @param id the expected id | ||
183 | * @return GNUNET_YES if id matches | ||
184 | */ | ||
185 | int | ||
186 | GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource, | ||
187 | const char* id) | ||
188 | { | ||
189 | return check_resource_attr_str (resource, id, GNUNET_REST_JSONAPI_KEY_ID); | ||
190 | } | ||
138 | 191 | ||
139 | 192 | ||
193 | /** | ||
194 | * Check a JSON API resource type | ||
195 | * | ||
196 | * @param res the JSON resource | ||
197 | * @param type the expected type | ||
198 | * @return GNUNET_YES if id matches | ||
199 | */ | ||
200 | int | ||
201 | GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource, | ||
202 | const char* type) | ||
203 | { | ||
204 | return check_resource_attr_str (resource, type, GNUNET_REST_JSONAPI_KEY_TYPE); | ||
205 | } | ||
206 | |||
140 | 207 | ||
141 | /** | 208 | /** |
142 | * Create a JSON API primary data | 209 | * Create a JSON API primary data |
143 | * | 210 | * |
144 | * @param type the JSON API resource type | ||
145 | * @param id the JSON API resource id | ||
146 | * @return a new JSON API resource or NULL on error. | 211 | * @return a new JSON API resource or NULL on error. |
147 | */ | 212 | */ |
148 | struct JsonApiResponse* | 213 | struct JsonApiObject* |
149 | GNUNET_REST_jsonapi_response_new () | 214 | GNUNET_REST_jsonapi_object_new () |
150 | { | 215 | { |
151 | struct JsonApiResponse *result; | 216 | struct JsonApiObject *result; |
152 | 217 | ||
153 | result = GNUNET_new (struct JsonApiResponse); | 218 | result = GNUNET_new (struct JsonApiObject); |
154 | result->res_count = 0; | 219 | result->res_count = 0; |
155 | return result; | 220 | return result; |
156 | } | 221 | } |
157 | 222 | ||
223 | |||
224 | static void | ||
225 | add_json_resource (struct JsonApiObject *obj, | ||
226 | const json_t *res_json) | ||
227 | { | ||
228 | struct JsonApiResource *res; | ||
229 | json_t *type_json; | ||
230 | json_t *id_json; | ||
231 | |||
232 | id_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_ID); | ||
233 | type_json = json_object_get (res_json, GNUNET_REST_JSONAPI_KEY_TYPE); | ||
234 | |||
235 | if (!json_is_string (id_json) || !json_is_string (type_json)) | ||
236 | return; | ||
237 | |||
238 | res = GNUNET_new (struct JsonApiResource); | ||
239 | res->res_obj = json_deep_copy (res_json); | ||
240 | GNUNET_REST_jsonapi_object_resource_add (obj, res); | ||
241 | } | ||
242 | |||
243 | /** | ||
244 | * Create a JSON API primary data from a string | ||
245 | * | ||
246 | * @param data the string of the JSON API data | ||
247 | * @return a new JSON API resource or NULL on error. | ||
248 | */ | ||
249 | struct JsonApiObject* | ||
250 | GNUNET_REST_jsonapi_object_parse (const char* data) | ||
251 | { | ||
252 | struct JsonApiObject *result; | ||
253 | json_t *root_json; | ||
254 | json_t *data_json; | ||
255 | json_error_t error; | ||
256 | int res_count = 0; | ||
257 | int i; | ||
258 | |||
259 | if (NULL == data) | ||
260 | return NULL; | ||
261 | root_json = json_loads (data, 0, &error); | ||
262 | |||
263 | if ( (NULL == root_json) || !json_is_object (root_json)) | ||
264 | { | ||
265 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "json error: %s", error.text); | ||
266 | return NULL; | ||
267 | } | ||
268 | data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA); | ||
269 | if (NULL == data_json) | ||
270 | { | ||
271 | json_decref (root_json); | ||
272 | return NULL; | ||
273 | } | ||
274 | |||
275 | result = GNUNET_new (struct JsonApiObject); | ||
276 | result->res_count = 0; | ||
277 | if (json_is_object (data_json)) | ||
278 | add_json_resource (result, data_json); | ||
279 | else if (json_is_array (data_json)) | ||
280 | { | ||
281 | res_count = json_array_size (data_json); | ||
282 | for (i = 0; i < res_count; i++) | ||
283 | add_json_resource (result, json_array_get (data_json, i)); | ||
284 | } | ||
285 | json_decref (root_json); | ||
286 | if (0 == res_count) | ||
287 | { | ||
288 | GNUNET_free (result); | ||
289 | result = NULL; | ||
290 | } | ||
291 | return result; | ||
292 | } | ||
293 | |||
294 | |||
158 | /** | 295 | /** |
159 | * Delete a JSON API primary data | 296 | * Delete a JSON API primary data |
160 | * | 297 | * |
@@ -163,7 +300,7 @@ GNUNET_REST_jsonapi_response_new () | |||
163 | * @return a new JSON API resource or NULL on error. | 300 | * @return a new JSON API resource or NULL on error. |
164 | */ | 301 | */ |
165 | void | 302 | void |
166 | GNUNET_REST_jsonapi_response_delete (struct JsonApiResponse *resp) | 303 | GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp) |
167 | { | 304 | { |
168 | struct JsonApiResource *res; | 305 | struct JsonApiResource *res; |
169 | 306 | ||
@@ -175,14 +312,14 @@ GNUNET_REST_jsonapi_response_delete (struct JsonApiResponse *resp) | |||
175 | } | 312 | } |
176 | 313 | ||
177 | /** | 314 | /** |
178 | * Add a JSON API resource to primary data | 315 | * Add a JSON API object to primary data |
179 | * | 316 | * |
180 | * @param data The JSON API data to add to | 317 | * @param data The JSON API data to add to |
181 | * @param res the JSON API resource to add | 318 | * @param res the JSON API resource to add |
182 | * @return the new number of resources | 319 | * @return the new number of resources |
183 | */ | 320 | */ |
184 | void | 321 | void |
185 | GNUNET_REST_jsonapi_response_resource_add (struct JsonApiResponse *resp, | 322 | GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp, |
186 | struct JsonApiResource *res) | 323 | struct JsonApiResource *res) |
187 | { | 324 | { |
188 | GNUNET_CONTAINER_DLL_insert (resp->res_list_head, | 325 | GNUNET_CONTAINER_DLL_insert (resp->res_list_head, |
@@ -192,6 +329,44 @@ GNUNET_REST_jsonapi_response_resource_add (struct JsonApiResponse *resp, | |||
192 | resp->res_count++; | 329 | resp->res_count++; |
193 | } | 330 | } |
194 | 331 | ||
332 | |||
333 | /** | ||
334 | * Get a JSON API object resource count | ||
335 | * | ||
336 | * @param resp the JSON API object | ||
337 | * @return the number of resources | ||
338 | */ | ||
339 | int | ||
340 | GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp) | ||
341 | { | ||
342 | return resp->res_count++; | ||
343 | } | ||
344 | |||
345 | /** | ||
346 | * Get a JSON API object resource #num | ||
347 | * | ||
348 | * @param resp the JSON API object | ||
349 | * @param num the number of the resource | ||
350 | * @return the resource | ||
351 | */ | ||
352 | struct JsonApiResource* | ||
353 | GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num) | ||
354 | { | ||
355 | struct JsonApiResource *res; | ||
356 | int i; | ||
357 | |||
358 | if ((0 < resp->res_count) && | ||
359 | (num < resp->res_count)) | ||
360 | return NULL; | ||
361 | res = resp->res_list_head; | ||
362 | for (i = 0; i < num; i++) | ||
363 | { | ||
364 | res = res->next; | ||
365 | } | ||
366 | return res; | ||
367 | } | ||
368 | |||
369 | |||
195 | /** | 370 | /** |
196 | * Add a JSON API resource to primary data | 371 | * Add a JSON API resource to primary data |
197 | * | 372 | * |
@@ -200,7 +375,7 @@ GNUNET_REST_jsonapi_response_resource_add (struct JsonApiResponse *resp, | |||
200 | * @return the new number of resources | 375 | * @return the new number of resources |
201 | */ | 376 | */ |
202 | void | 377 | void |
203 | GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiResponse *resp, | 378 | GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp, |
204 | struct JsonApiResource *res) | 379 | struct JsonApiResource *res) |
205 | { | 380 | { |
206 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, | 381 | GNUNET_CONTAINER_DLL_remove (resp->res_list_head, |
@@ -217,7 +392,7 @@ GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiResponse *resp, | |||
217 | * @return GNUNET_SYSERR on error else GNUNET_OK | 392 | * @return GNUNET_SYSERR on error else GNUNET_OK |
218 | */ | 393 | */ |
219 | int | 394 | int |
220 | GNUNET_REST_jsonapi_data_serialize (const struct JsonApiResponse *resp, | 395 | GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp, |
221 | char **result) | 396 | char **result) |
222 | { | 397 | { |
223 | struct JsonApiResource *res; | 398 | struct JsonApiResource *res; |