2 This file is part of GNUnet
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_rest_lib.h
23 * @brief API for helper library to parse/create REST
24 * @author Martin Schanzenbach
26 #ifndef GNUNET_REST_LIB_H
27 #define GNUNET_REST_LIB_H
29 #include "gnunet_util_lib.h"
30 #include "microhttpd.h"
33 #define GNUNET_REST_JSONAPI_KEY_DATA "data"
35 #define GNUNET_REST_JSONAPI_KEY_ID "id"
37 #define GNUNET_REST_JSONAPI_KEY_TYPE "type"
39 #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL}
41 struct RestConnectionDataHandle
43 struct GNUNET_CONTAINER_MultiHashMap *url_param_map;
51 struct GNUNET_REST_RestConnectionHandler
54 * Http method to handle
61 const char *namespace;
66 void (*proc) (struct RestConnectionDataHandle *handle,
74 * Iterator called on obtained result for a REST result.
77 * @param resp the response
78 * @param status status code (HTTP)
80 typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
81 struct MHD_Response *resp,
86 * Resource structs for JSON API
88 struct JsonApiResource;
91 * Responses for JSON API
96 * Create a JSON API resource
98 * @param type the JSON API resource type
99 * @param id the JSON API resource id
100 * @return a new JSON API resource or NULL on error.
102 struct JsonApiResource*
103 GNUNET_REST_jsonapi_resource_new (const char *type, const char *id);
106 * Delete a JSON API resource
108 * @param res the JSON resource
109 * @param result Pointer where the resource should be stored
112 GNUNET_REST_jsonapi_resource_delete (struct JsonApiResource *resource);
115 * Add a JSON API attribute
117 * @param res the JSON resource
118 * @param key the key for the attribute
119 * @param json the json_t attribute to add
120 * @return #GNUNET_OK if added successfully
121 * #GNUNET_SYSERR if not
124 GNUNET_REST_jsonapi_resource_add_attr (const struct JsonApiResource *resource,
128 * Read a JSON API attribute
130 * @param res the JSON resource
131 * @param key the key for the attribute
132 * @return the json attr
135 GNUNET_REST_jsonapi_resource_read_attr (const struct JsonApiResource *resource,
140 * Check a JSON API resource id
142 * @param res the JSON resource
143 * @param id the expected id
144 * @return GNUNET_YES if id matches
147 GNUNET_REST_jsonapi_resource_check_id (const struct JsonApiResource *resource,
152 * Check a JSON API resource type
154 * @param res the JSON resource
155 * @param type the expected type
156 * @return GNUNET_YES if id matches
159 GNUNET_REST_jsonapi_resource_check_type (const struct JsonApiResource *resource,
164 * Create a JSON API primary data
166 * @param type the JSON API resource type
167 * @param id the JSON API resource id
168 * @return a new JSON API resource or NULL on error.
170 struct JsonApiObject*
171 GNUNET_REST_jsonapi_object_new ();
175 * Create a JSON API primary data from a string
177 * @param data the string of the JSON API data
178 * @return a new JSON API resource or NULL on error.
180 struct JsonApiObject*
181 GNUNET_REST_jsonapi_object_parse (const char* data);
185 * Delete a JSON API primary data
187 * @param type the JSON API resource type
188 * @param id the JSON API resource id
189 * @return a new JSON API resource or NULL on error.
192 GNUNET_REST_jsonapi_object_delete (struct JsonApiObject *resp);
195 * Add a JSON API resource to primary data
197 * @param data The JSON API data to add to
198 * @param res the JSON API resource to add
199 * @return the new number of resources
202 GNUNET_REST_jsonapi_object_resource_add (struct JsonApiObject *resp,
203 struct JsonApiResource *res);
205 * Get a JSON API object resource count
207 * @param resp the JSON API object
208 * @return the number of resources
211 GNUNET_REST_jsonapi_object_resource_count (struct JsonApiObject *resp);
214 * Get a JSON API object resource num
216 * @param resp the JSON API object
217 * @param num the number of the resource
218 * @return the resource
220 struct JsonApiResource*
221 GNUNET_REST_jsonapi_object_get_resource (struct JsonApiObject *resp, int num);
225 * Add a JSON API resource to primary data
227 * @param resp The JSON API data to add to
228 * @param res the JSON API resource to add
229 * @return the new number of resources
232 GNUNET_REST_jsonapi_data_resource_remove (struct JsonApiObject *resp,
233 struct JsonApiResource *res);
236 * String serialze jsonapi primary data
238 * @param data the JSON API primary data
239 * @param result where to store the result
240 * @return GNUNET_SYSERR on error else GNUNET_OK
243 GNUNET_REST_jsonapi_data_serialize (const struct JsonApiObject *resp,
247 * Check if namespace is in URL.
249 * @param url URL to check
250 * @param namespace namespace to check against
251 * @retun GNUNET_YES if namespace matches
254 GNUNET_REST_namespace_match (const char *url, const char *namespace);
257 * Create JSON API MHD response
259 * @param data JSON result
260 * @retun MHD response
263 GNUNET_REST_create_json_response (const char *data);
267 GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn,
268 const struct GNUNET_REST_RestConnectionHandler *handlers,