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"
29 /* ****************** JSONAPI parsing ******************* */
31 struct GNUNET_JSONAPI_Resource;
33 struct GNUNET_JSONAPI_Object;
36 * Specification for parsing a jsonapi object.
38 * @param jsonapi_obj where to store the jsonapi object
40 struct GNUNET_JSON_Specification
41 GNUNET_JSON_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_obj);
44 * Create a JSON API resource
46 * @param type the JSON API resource type
47 * @param id the JSON API resource id
48 * @return a new JSON API resource or NULL on error.
50 struct GNUNET_JSONAPI_Resource*
51 GNUNET_JSONAPI_resource_new (const char *type, const char *id);
54 * Delete a JSON API resource
56 * @param res the JSON resource
57 * @param result Pointer where the resource should be stored
60 GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
63 * Add a JSON API attribute
65 * @param res the JSON resource
66 * @param key the key for the attribute
67 * @param json the json_t attribute to add
68 * @return #GNUNET_OK if added successfully
69 * #GNUNET_SYSERR if not
72 GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource,
76 * Read a JSON API attribute
78 * @param res the JSON resource
79 * @param key the key for the attribute
80 * @return the json attr
83 GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource,
88 * Check a JSON API resource id
90 * @param res the JSON resource
91 * @param id the expected id
92 * @return GNUNET_YES if id matches
95 GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
100 * Check a JSON API resource type
102 * @param res the JSON resource
103 * @param type the expected type
104 * @return GNUNET_YES if id matches
107 GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
112 * Create a JSON API primary data
114 * @param type the JSON API resource type
115 * @param id the JSON API resource id
116 * @return a new JSON API resource or NULL on error.
118 struct GNUNET_JSONAPI_Object*
119 GNUNET_JSONAPI_object_new ();
123 * Create a JSON API primary data from a string
125 * @param data the string of the JSON API data
126 * @param Pointer where to store new jsonapi Object.
127 * @return GNUNET_OK on success
130 GNUNET_JSONAPI_object_parse (const char* data,
131 struct GNUNET_JSONAPI_Object** obj);
135 * Delete a JSON API primary data
137 * @param type the JSON API resource type
138 * @param id the JSON API resource id
139 * @return a new JSON API resource or NULL on error.
142 GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp);
145 * Add a JSON API resource to primary data
147 * @param data The JSON API data to add to
148 * @param res the JSON API resource to add
149 * @return the new number of resources
152 GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp,
153 struct GNUNET_JSONAPI_Resource *res);
155 * Get a JSON API object resource count
157 * @param resp the JSON API object
158 * @return the number of resources
161 GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp);
164 * Get a JSON API object resource num
166 * @param resp the JSON API object
167 * @param num the number of the resource
168 * @return the resource
170 struct GNUNET_JSONAPI_Resource*
171 GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, int num);
175 * Add a JSON API resource to primary data
177 * @param resp The JSON API data to add to
178 * @param res the JSON API resource to add
179 * @return the new number of resources
182 GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp,
183 struct GNUNET_JSONAPI_Resource *res);
186 * String serialze jsonapi primary data
188 * @param data the JSON API primary data
189 * @param result where to store the result
190 * @return GNUNET_SYSERR on error else GNUNET_OK
193 GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp,
197 * Check a JSON API resource id
199 * @param res the JSON resource
200 * @return the resource id
203 GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
206 /* end of gnunet_jsonapi_lib.h */
209 * Check rest request for validity
211 * @param req handle to the request
212 * @return GNUNET_OK if valid
215 GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle *req);
218 * Check rest request for validity
220 * @param req handle to the request
221 * @return GNUNET_OK if valid
224 GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req);
228 * Handle jsonapi rest request. Checks request headers for jsonapi compliance
230 * @param req rest request handle
231 * @param handler rest request handlers
233 * @return GNUNET_OK if successful
236 GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *req,
237 const struct GNUNET_REST_RequestHandler *handlers,
238 struct GNUNET_REST_RequestHandlerError *err,