[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r37127 - in gnunet/src: gns identity identity-provider incl
From: |
gnunet |
Subject: |
[GNUnet-SVN] r37127 - in gnunet/src: gns identity identity-provider include jsonapi namestore |
Date: |
Thu, 5 May 2016 12:17:37 +0200 |
Author: schanzen
Date: 2016-05-05 12:17:37 +0200 (Thu, 05 May 2016)
New Revision: 37127
Modified:
gnunet/src/gns/plugin_rest_gns.c
gnunet/src/identity-provider/plugin_rest_identity_provider.c
gnunet/src/identity/plugin_rest_identity.c
gnunet/src/include/gnunet_jsonapi_lib.h
gnunet/src/jsonapi/Makefile.am
gnunet/src/jsonapi/jsonapi_document.c
gnunet/src/jsonapi/jsonapi_error.c
gnunet/src/jsonapi/jsonapi_resource.c
gnunet/src/jsonapi/test_jsonapi.c
gnunet/src/namestore/plugin_rest_namestore.c
Log:
- refactor jsonpi utils, add test
Modified: gnunet/src/gns/plugin_rest_gns.c
===================================================================
--- gnunet/src/gns/plugin_rest_gns.c 2016-05-05 09:13:47 UTC (rev 37126)
+++ gnunet/src/gns/plugin_rest_gns.c 2016-05-05 10:17:37 UTC (rev 37127)
@@ -33,6 +33,7 @@
#include <gnunet_gns_service.h>
#include <gnunet_rest_lib.h>
#include <gnunet_jsonapi_lib.h>
+#include <gnunet_jsonapi_util.h>
#include <jansson.h>
#define GNUNET_REST_API_NS_GNS "/gns"
Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c 2016-05-05 09:13:47 UTC (rev
37126)
+++ gnunet/src/identity/plugin_rest_identity.c 2016-05-05 10:17:37 UTC (rev
37127)
@@ -29,6 +29,7 @@
#include "gnunet_identity_service.h"
#include "gnunet_rest_lib.h"
#include "gnunet_jsonapi_lib.h"
+#include "gnunet_jsonapi_util.h"
#include "microhttpd.h"
#include <jansson.h>
#include "gnunet_signatures.h"
Modified: gnunet/src/identity-provider/plugin_rest_identity_provider.c
===================================================================
--- gnunet/src/identity-provider/plugin_rest_identity_provider.c
2016-05-05 09:13:47 UTC (rev 37126)
+++ gnunet/src/identity-provider/plugin_rest_identity_provider.c
2016-05-05 10:17:37 UTC (rev 37127)
@@ -32,6 +32,7 @@
#include "gnunet_namestore_service.h"
#include "gnunet_rest_lib.h"
#include "gnunet_jsonapi_lib.h"
+#include "gnunet_jsonapi_util.h"
#include "microhttpd.h"
#include <jansson.h>
#include <inttypes.h>
Modified: gnunet/src/include/gnunet_jsonapi_lib.h
===================================================================
--- gnunet/src/include/gnunet_jsonapi_lib.h 2016-05-05 09:13:47 UTC (rev
37126)
+++ gnunet/src/include/gnunet_jsonapi_lib.h 2016-05-05 10:17:37 UTC (rev
37127)
@@ -22,7 +22,6 @@
#define GNUNET_JSONAPI_LIB_H
#include "gnunet_util_lib.h"
-#include "gnunet_rest_lib.h"
#include "gnunet_json_lib.h"
#define GNUNET_JSONAPI_KEY_DATA "data"
@@ -101,7 +100,33 @@
GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
+/****************** jsonapi Error API ********************/
+
/**
+ * Create a JSON API error
+ *
+ * @param res the JSON error
+ */
+struct GNUNET_JSONAPI_Error*
+GNUNET_JSONAPI_error_new (const char *id,
+ const char *status,
+ const char *code,
+ const char *title,
+ const char *detail,
+ json_t *links,
+ json_t *source,
+ json_t *meta);
+
+/**
+ * Delete a JSON API error
+ *
+ * @param res the JSON error
+ */
+void
+GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
+
+
+/**
* Add a JSON API error to document
*
* @param data The JSON API document to add to
@@ -134,13 +159,7 @@
GNUNET_JSONAPI_json_to_error (json_t *err_json,
struct GNUNET_JSONAPI_Error **err);
-/**
- * Delete a JSON API error
- *
- * @param res the JSON error
- */
-void
-GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
+/****************** jsonapi Resource API ********************/
/**
* Create a JSON API resource
@@ -154,6 +173,16 @@
/**
+ * Delete a JSON API resource
+ *
+ * @param res the JSON resource
+ * @param result Pointer where the resource should be stored
+ */
+void
+GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
+
+
+/**
* String serialze jsonapi to json
*
* @param data the JSON API resource
@@ -164,6 +193,7 @@
GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
json_t **result);
+
/**
* Parse json to resource object
*
@@ -175,14 +205,6 @@
GNUNET_JSONAPI_json_to_resource (json_t *res_json,
struct GNUNET_JSONAPI_Resource **res);
-/**
- * Delete a JSON API resource
- *
- * @param res the JSON resource
- * @param result Pointer where the resource should be stored
- */
-void
-GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
/**
* Add a JSON API attribute
@@ -220,7 +242,16 @@
GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource
*resource,
const char* id);
+/**
+ * Check a JSON API resource id
+ *
+ * @param res the JSON resource
+ * @return the resource id
+ */
+char*
+GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource
*resource);
+
/**
* Check a JSON API resource type
*
@@ -232,6 +263,7 @@
GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource
*resource,
const char* type);
+/****************** jsonapi Document API ********************/
/**
* Create a JSON API primary data
@@ -267,6 +299,17 @@
GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
/**
+ * String serialze jsonapi primary data
+ *
+ * @param data the JSON API primary data
+ * @param result where to store the result
+ * @return GNUNET_SYSERR on error else GNUNET_OK
+ */
+int
+GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
+ json_t **root_json);
+
+/**
* Add a JSON API resource to primary data
*
* @param data The JSON API data to add to
@@ -318,49 +361,5 @@
GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
char **result);
-/**
- * Check a JSON API resource id
- *
- * @param res the JSON resource
- * @return the resource id
- */
-char*
-GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource
*resource);
-
-
/* end of gnunet_jsonapi_lib.h */
-
-/**
- * Check rest request for validity
- *
- * @param req handle to the request
- * @return GNUNET_OK if valid
- */
-int
-GNUNET_JSONAPI_check_request_acceptable (struct GNUNET_REST_RequestHandle
*req);
-
-/**
- * Check rest request for validity
- *
- * @param req handle to the request
- * @return GNUNET_OK if valid
- */
-int
-GNUNET_JSONAPI_check_request_supported (struct GNUNET_REST_RequestHandle *req);
-
-
-/**
- * Handle jsonapi rest request. Checks request headers for jsonapi compliance
- *
- * @param req rest request handle
- * @param handler rest request handlers
- * @param cls closure
- * @return GNUNET_OK if successful
- */
-int
-GNUNET_JSONAPI_handle_request (struct GNUNET_REST_RequestHandle *req,
- const struct GNUNET_REST_RequestHandler
*handlers,
- struct GNUNET_REST_RequestHandlerError *err,
- void *cls);
-
#endif
Modified: gnunet/src/jsonapi/Makefile.am
===================================================================
--- gnunet/src/jsonapi/Makefile.am 2016-05-05 09:13:47 UTC (rev 37126)
+++ gnunet/src/jsonapi/Makefile.am 2016-05-05 10:17:37 UTC (rev 37127)
@@ -7,13 +7,24 @@
endif
lib_LTLIBRARIES = \
- libgnunetjsonapi.la
+ libgnunetjsonapi.la \
+ libgnunetjsonapiutils.la
+libgnunetjsonapiutils_la_LDFLAGS = \
+ -version-info 0:0:0 \
+ -no-undefined
+libgnunetjsonapiutils_la_SOURCES = \
+ jsonapi.c
+libgnunetjsonapiutils_la_LIBADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \
+ $(top_builddir)/src/rest/libgnunetrest.la \
+ $(XLIB)
+
libgnunetjsonapi_la_LDFLAGS = \
-version-info 0:0:0 \
-no-undefined
libgnunetjsonapi_la_SOURCES = \
- jsonapi.c \
jsonapi_document.c \
jsonapi_resource.c \
jsonapi_error.c \
@@ -21,7 +32,6 @@
libgnunetjsonapi_la_LIBADD = \
$(top_builddir)/src/util/libgnunetutil.la \
$(top_builddir)/src/json/libgnunetjson.la \
- $(top_builddir)/src/rest/libgnunetrest.la \
-ljansson \
$(XLIB)
Modified: gnunet/src/jsonapi/jsonapi_document.c
===================================================================
--- gnunet/src/jsonapi/jsonapi_document.c 2016-05-05 09:13:47 UTC (rev
37126)
+++ gnunet/src/jsonapi/jsonapi_document.c 2016-05-05 10:17:37 UTC (rev
37127)
@@ -286,12 +286,11 @@
* @return GNUNET_SYSERR on error else GNUNET_OK
*/
int
-GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
- char **result)
+GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
+ json_t **root_json)
{
struct GNUNET_JSONAPI_Resource *res;
struct GNUNET_JSONAPI_Error *error;
- json_t *root_json;
json_t *res_json;
json_t *res_json_tmp;
@@ -298,7 +297,7 @@
if ((NULL == doc))
return GNUNET_SYSERR;
- root_json = json_object ();
+ *root_json = json_object ();
//Check for errors first
if (doc->err_count != 0)
@@ -313,7 +312,9 @@
&res_json_tmp));
json_array_append (res_json, res_json_tmp);
}
- json_object_set (root_json, GNUNET_JSONAPI_KEY_ERRORS, res_json);
+ json_object_set_new (*root_json,
+ GNUNET_JSONAPI_KEY_ERRORS,
+ res_json);
} else {
switch (doc->res_count)
{
@@ -338,14 +339,34 @@
}
break;
}
- json_object_set (root_json, GNUNET_JSONAPI_KEY_DATA, res_json);
+ json_object_set_new (*root_json,
+ GNUNET_JSONAPI_KEY_DATA,
+ res_json);
}
+ json_object_set (*root_json,
+ GNUNET_JSONAPI_KEY_META,
+ doc->meta);
+ return GNUNET_OK;
+}
- //Add meta
- json_object_set (root_json, GNUNET_JSONAPI_KEY_META, doc->meta);
- *result = json_dumps (root_json, JSON_INDENT(2));
- json_decref (root_json);
- json_decref (res_json);
+/**
+ * String serialze jsonapi primary data
+ *
+ * @param data the JSON API primary data
+ * @param result where to store the result
+ * @return GNUNET_SYSERR on error else GNUNET_OK
+ */
+int
+GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
+ char **result)
+{
+ json_t *json_doc;
+ if (GNUNET_OK != GNUNET_JSONAPI_document_to_json (doc,
+ &json_doc))
+ return GNUNET_SYSERR;
+
+ *result = json_dumps (json_doc, JSON_INDENT(2));
+ json_decref (json_doc);
return GNUNET_OK;
}
Modified: gnunet/src/jsonapi/jsonapi_error.c
===================================================================
--- gnunet/src/jsonapi/jsonapi_error.c 2016-05-05 09:13:47 UTC (rev 37126)
+++ gnunet/src/jsonapi/jsonapi_error.c 2016-05-05 10:17:37 UTC (rev 37127)
@@ -132,6 +132,42 @@
}
/**
+ * Create a JSON API error
+ *
+ * @param res the JSON error
+ */
+struct GNUNET_JSONAPI_Error*
+GNUNET_JSONAPI_error_new (const char *id,
+ const char *status,
+ const char *code,
+ const char *title,
+ const char *detail,
+ json_t *links,
+ json_t *source,
+ json_t *meta)
+{
+ struct GNUNET_JSONAPI_Error *error;
+ error = GNUNET_new (struct GNUNET_JSONAPI_Error);
+
+ GNUNET_assert (NULL != id);
+ error->id = GNUNET_strdup (id);
+ GNUNET_assert (NULL != status);
+ error->status = GNUNET_strdup (status);
+ GNUNET_assert (NULL != code);
+ error->code = GNUNET_strdup (code);
+ GNUNET_assert (NULL != title);
+ error->title = GNUNET_strdup (title);
+ GNUNET_assert (NULL != detail);
+ error->detail = GNUNET_strdup (detail);
+ GNUNET_assert (NULL != links);
+ error->links = json_deep_copy (links);
+ GNUNET_assert (NULL != source);
+ error->source = json_deep_copy (source);
+ GNUNET_assert (NULL != meta);
+ error->meta = json_deep_copy (meta);
+ return error;
+}
+/**
* Delete a JSON API error
*
* @param res the JSON error
Modified: gnunet/src/jsonapi/jsonapi_resource.c
===================================================================
--- gnunet/src/jsonapi/jsonapi_resource.c 2016-05-05 09:13:47 UTC (rev
37126)
+++ gnunet/src/jsonapi/jsonapi_resource.c 2016-05-05 10:17:37 UTC (rev
37127)
@@ -145,7 +145,7 @@
return GNUNET_SYSERR;
if (NULL == resource->attr_obj)
resource->attr_obj = json_object ();
- json_object_set (resource->attr_obj, key, json);
+ json_object_set_new (resource->attr_obj, key, json);
return GNUNET_OK;
}
Modified: gnunet/src/jsonapi/test_jsonapi.c
===================================================================
--- gnunet/src/jsonapi/test_jsonapi.c 2016-05-05 09:13:47 UTC (rev 37126)
+++ gnunet/src/jsonapi/test_jsonapi.c 2016-05-05 10:17:37 UTC (rev 37127)
@@ -24,11 +24,47 @@
#include "gnunet_jsonapi_lib.h"
#include "gnunet_json_lib.h"
+#define TEST_JSONAPI_DOCUMENT
"{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
+
static int
+test_document ()
+{
+ struct GNUNET_JSONAPI_Document *obj;
+ struct GNUNET_JSONAPI_Resource *res;
+ json_t *doc_json;
+ json_t *data_js;
+ json_error_t err;
+
+ obj = GNUNET_JSONAPI_document_new ();
+ res = GNUNET_JSONAPI_resource_new ("bar",
+ "1");
+
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_JSONAPI_resource_add_attr (res,
+ "foo",
+ json_string ("bar")));
+
+ GNUNET_JSONAPI_document_resource_add (obj,
+ res);
+
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_JSONAPI_document_to_json (obj,
+ &doc_json));
+ data_js = json_loads (TEST_JSONAPI_DOCUMENT,
+ JSON_DECODE_ANY,
+ &err);
+ GNUNET_assert (NULL != data_js);
+ GNUNET_assert (0 != json_equal (data_js, doc_json));
+ GNUNET_JSONAPI_document_delete (obj);
+ json_decref (data_js);
+ json_decref (doc_json);
+ return 0;
+}
+
+static int
test_serialize ()
{
struct GNUNET_JSONAPI_Document *obj;
- char* data = "{\"data\":{\"id\":\"1\",\"type\":\"bar\",
\"attributes\":{\"foo\":\"bar\"}}}";
char* tmp_data;
json_t* data_js;
json_t* tmp_data_js;
@@ -37,7 +73,9 @@
GNUNET_JSON_spec_jsonapi_document (&obj),
GNUNET_JSON_spec_end()
};
- data_js = json_loads (data, JSON_DECODE_ANY, &err);
+ data_js = json_loads (TEST_JSONAPI_DOCUMENT,
+ JSON_DECODE_ANY,
+ &err);
GNUNET_assert (NULL != data_js);
GNUNET_assert (GNUNET_OK ==
GNUNET_JSON_parse (data_js, jsonapispec,
@@ -98,6 +136,8 @@
return 1;
if (0 != test_serialize ())
return 1;
+ if (0 != test_document ())
+ return 1;
return 0;
}
Modified: gnunet/src/namestore/plugin_rest_namestore.c
===================================================================
--- gnunet/src/namestore/plugin_rest_namestore.c 2016-05-05 09:13:47 UTC
(rev 37126)
+++ gnunet/src/namestore/plugin_rest_namestore.c 2016-05-05 10:17:37 UTC
(rev 37127)
@@ -30,6 +30,7 @@
#include "gnunet_identity_service.h"
#include "gnunet_rest_lib.h"
#include "gnunet_jsonapi_lib.h"
+#include "gnunet_jsonapi_util.h"
#include "microhttpd.h"
#include <jansson.h>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r37127 - in gnunet/src: gns identity identity-provider include jsonapi namestore,
gnunet <=