2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet 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.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file gnunet_jsonapi_lib.h
18 * @brief functions to parse jsonapi objects
19 * @author Martin Schanzenbach
21 #ifndef GNUNET_JSONAPI_LIB_H
22 #define GNUNET_JSONAPI_LIB_H
24 #include "gnunet_util_lib.h"
25 #include "gnunet_rest_lib.h"
26 #include "gnunet_json_lib.h"
28 #define GNUNET_JSONAPI_KEY_DATA "data"
30 #define GNUNET_JSONAPI_KEY_ID "id"
32 #define GNUNET_JSONAPI_KEY_TYPE "type"
34 #define GNUNET_JSONAPI_KEY_META "meta"
36 #define GNUNET_JSONAPI_KEY_ATTRIBUTES "attributes"
38 #define GNUNET_JSONAPI_KEY_CODE "code"
40 #define GNUNET_JSONAPI_KEY_TITLE "title"
42 #define GNUNET_JSONAPI_KEY_DETAIL "detail"
44 #define GNUNET_JSONAPI_KEY_SOURCE "source"
46 #define GNUNET_JSONAPI_KEY_LINKS "links"
48 #define GNUNET_JSONAPI_KEY_STATUS "status"
50 #define GNUNET_JSONAPI_KEY_ERRORS "errors"
52 /* ****************** JSONAPI parsing ******************* */
54 struct GNUNET_JSONAPI_Relationship;
56 struct GNUNET_JSONAPI_Error;
58 struct GNUNET_JSONAPI_Resource;
60 struct GNUNET_JSONAPI_Document;
63 * Specification for parsing a jsonapi relationship.
65 * @param jsonapi_obj where to store the jsonapi relationship
67 struct GNUNET_JSON_Specification
68 GNUNET_JSON_spec_jsonapi_relationship (struct GNUNET_JSONAPI_Relationship **jsonapi_obj);
71 * Specification for parsing a jsonapi error.
73 * @param jsonapi_obj where to store the jsonapi error
75 struct GNUNET_JSON_Specification
76 GNUNET_JSON_spec_jsonapi_error (struct GNUNET_JSONAPI_Error **jsonapi_obj);
79 * Specification for parsing a jsonapi resource.
81 * @param jsonapi_obj where to store the jsonapi resource
83 struct GNUNET_JSON_Specification
84 GNUNET_JSON_spec_jsonapi_resource (struct GNUNET_JSONAPI_Resource **jsonapi_obj);
87 * Specification for parsing a jsonapi object.
89 * @param jsonapi_obj where to store the jsonapi object
91 struct GNUNET_JSON_Specification
92 GNUNET_JSON_spec_jsonapi_document (struct GNUNET_JSONAPI_Document **jsonapi_obj);
95 * Delete a JSON API relationship
97 * @param res the JSON resource
98 * @param result Pointer where the resource should be stored
101 GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
105 * Add a JSON API error to document
107 * @param data The JSON API document to add to
108 * @param res the JSON API error to add
109 * @return the new number of resources
112 GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
113 struct GNUNET_JSONAPI_Error *err);
116 * String serialze jsonapi error to json
118 * @param data the JSON API error
119 * @param result where to store the result
120 * @return GNUNET_SYSERR on error else GNUNET_OK
123 GNUNET_JSONAPI_error_to_json (const struct GNUNET_JSONAPI_Error *err,
127 * Parse json to error object
129 * @param err_json JSON object
130 * @param[out] err error object
131 * @return GNUNET_OK on success
134 GNUNET_JSONAPI_json_to_error (json_t *err_json,
135 struct GNUNET_JSONAPI_Error **err);
138 * Delete a JSON API error
140 * @param res the JSON error
143 GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
146 * Create a JSON API resource
148 * @param type the JSON API resource type
149 * @param id the JSON API resource id
150 * @return a new JSON API resource or NULL on error.
152 struct GNUNET_JSONAPI_Resource*
153 GNUNET_JSONAPI_resource_new (const char *type, const char *id);
157 * String serialze jsonapi to json
159 * @param data the JSON API resource
160 * @param result where to store the result
161 * @return GNUNET_SYSERR on error else GNUNET_OK
164 GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
168 * Parse json to resource object
170 * @param res_json JSON object
171 * @param[out] res resource object
172 * @return GNUNET_OK on success
175 GNUNET_JSONAPI_json_to_resource (json_t *res_json,
176 struct GNUNET_JSONAPI_Resource **res);
179 * Delete a JSON API resource
181 * @param res the JSON resource
182 * @param result Pointer where the resource should be stored
185 GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
188 * Add a JSON API attribute
190 * @param res the JSON resource
191 * @param key the key for the attribute
192 * @param json the json_t attribute to add
193 * @return #GNUNET_OK if added successfully
194 * #GNUNET_SYSERR if not
197 GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource,
201 * Read a JSON API attribute
203 * @param res the JSON resource
204 * @param key the key for the attribute
205 * @return the json attr
208 GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource,
213 * Check a JSON API resource id
215 * @param res the JSON resource
216 * @param id the expected id
217 * @return GNUNET_YES if id matches
220 GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
225 * Check a JSON API resource type
227 * @param res the JSON resource
228 * @param type the expected type
229 * @return GNUNET_YES if id matches
232 GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
237 * Create a JSON API primary data
239 * @param type the JSON API resource type
240 * @param id the JSON API resource id
241 * @return a new JSON API resource or NULL on error.
243 struct GNUNET_JSONAPI_Document*
244 GNUNET_JSONAPI_document_new ();
248 * Create a JSON API primary data from a string
250 * @param data the string of the JSON API data
251 * @param Pointer where to store new jsonapi Object.
252 * @return GNUNET_OK on success
255 GNUNET_JSONAPI_document_parse (const char* data,
256 struct GNUNET_JSONAPI_Document** obj);
260 * Delete a JSON API primary data
262 * @param type the JSON API resource type
263 * @param id the JSON API resource id
264 * @return a new JSON API resource or NULL on error.
267 GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
270 * Add a JSON API resource to primary data
272 * @param data The JSON API data to add to
273 * @param res the JSON API resource to add
274 * @return the new number of resources
277 GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *resp,
278 struct GNUNET_JSONAPI_Resource *res);
280 * Get a JSON API object resource count
282 * @param resp the JSON API object
283 * @return the number of resources
286 GNUNET_JSONAPI_document_resource_count (struct GNUNET_JSONAPI_Document *resp);
289 * Get a JSON API object resource num
291 * @param resp the JSON API object
292 * @param num the number of the resource
293 * @return the resource
295 struct GNUNET_JSONAPI_Resource*
296 GNUNET_JSONAPI_document_get_resource (struct GNUNET_JSONAPI_Document *resp, int num);
300 * Add a JSON API resource to primary data
302 * @param resp The JSON API data to add to
303 * @param res the JSON API resource to add
304 * @return the new number of resources
307 GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
308 struct GNUNET_JSONAPI_Resource *res);
311 * String serialze jsonapi primary data
313 * @param data the JSON API primary data
314 * @param result where to store the result
315 * @return GNUNET_SYSERR on error else GNUNET_OK
318 GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
322 * Check a JSON API resource id
324 * @param res the JSON resource
325 * @return the resource id
328 GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
331 /* end of gnunet_jsonapi_lib.h */
334 * Check rest request for validity
336 * @param req handle to the request
337 * @return GNUNET_OK if valid
340 GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle *req);
343 * Check rest request for validity
345 * @param req handle to the request
346 * @return GNUNET_OK if valid
349 GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req);
353 * Handle jsonapi rest request. Checks request headers for jsonapi compliance
355 * @param req rest request handle
356 * @param handler rest request handlers
358 * @return GNUNET_OK if successful
361 GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *req,
362 const struct GNUNET_REST_RequestHandler *handlers,
363 struct GNUNET_REST_RequestHandlerError *err,