[couchdb-glib/wip/query-response: 1/2] Add CouchdbResponse and CouchdbQuery classes



commit 709b3a537b126067251468667d919baa609626af
Author: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
Date:   Fri Nov 19 17:24:01 2010 +0100

    Add CouchdbResponse and CouchdbQuery classes
    
    First part of the big refactor of 2010. Introduced CouchdbQuery
    and CouchdbResponse classes that will make a fundament for the
    refactored couchdb-glib and desktopcouch-glib.

 couchdb-glib/Makefile.am        |    6 +
 couchdb-glib/couchdb-glib.h     |    2 +
 couchdb-glib/couchdb-query.c    |  313 +++++++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-query.h    |   85 +++++++++++
 couchdb-glib/couchdb-response.c |  288 +++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-response.h |   69 +++++++++
 6 files changed, 763 insertions(+), 0 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index 6d4f9ad..62a4558 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -36,6 +36,8 @@ libcouchdb_glib_1_0_la_headers =	\
 	couchdb-document-info.h		\
 	couchdb-document-task.h		\
 	couchdb-glib.h			\
+	couchdb-query.h			\
+	couchdb-response.h		\
 	couchdb-session.h		\
 	couchdb-struct-field.h		\
 	couchdb-types.h			\
@@ -52,6 +54,8 @@ libcouchdb_glib_1_0_la_sources =	\
 	couchdb-document-contact.c	\
 	couchdb-document-info.c		\
 	couchdb-document-task.c		\
+	couchdb-query.c			\
+	couchdb-response.c		\
 	couchdb-session.c		\
 	couchdb-struct-field.c		\
 	dbwatch.c			\
@@ -85,6 +89,8 @@ h_DATA = 				\
 	couchdb-document-info.h		\
 	couchdb-document-task.h		\
 	couchdb-glib.h			\
+	couchdb-query.h			\
+	couchdb-response.h		\
 	couchdb-session.h		\
 	couchdb-struct-field.h		\
 	couchdb-types.h
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index 2282ce9..7a99154 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -34,6 +34,8 @@
 #include <couchdb-document-contact.h>
 #include <couchdb-document-info.h>
 #include <couchdb-document-task.h>
+#include <couchdb-query.h>
+#include <couchdb-response.h>
 #include <couchdb-session.h>
 #include <couchdb-struct-field.h>
 
diff --git a/couchdb-glib/couchdb-query.c b/couchdb-glib/couchdb-query.c
new file mode 100644
index 0000000..21b772a
--- /dev/null
+++ b/couchdb-glib/couchdb-query.c
@@ -0,0 +1,313 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include <libsoup/soup-uri.h>
+
+#include "couchdb-query.h"
+#include "utils.h"
+
+#define GET_PRIVATE(obj)				\
+  (G_TYPE_INSTANCE_GET_PRIVATE ((obj),			\
+				COUCHDB_TYPE_QUERY,	\
+				CouchdbQueryPrivate))
+
+G_DEFINE_TYPE(CouchdbQuery, couchdb_query, G_TYPE_OBJECT)
+
+struct _CouchdbQueryPrivate {
+	GHashTable *query_options;
+	gchar *query_string;
+	JsonObject *body;
+	gchar *path;
+	gchar *method;
+};
+
+enum {
+	PROP_0,
+
+	PROP_QUERY_OPTIONS,
+	PROP_PATH
+};
+
+static void
+couchdb_query_get_property (GObject *object, guint property_id,
+			    GValue *value, GParamSpec *pspec)
+{
+	CouchdbQuery *self;
+
+	self = COUCHDB_QUERY (object);
+
+	switch (property_id) {
+	case PROP_QUERY_OPTIONS:
+		g_value_set_boxed (value, self->priv->query_options);
+		break;
+	case PROP_PATH:
+		g_value_set_string (value, self->priv->path);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+static void
+couchdb_query_set_property (GObject *object, guint property_id,
+			    const GValue *value, GParamSpec *pspec)
+{
+	CouchdbQuery *self;
+
+	self = COUCHDB_QUERY (object);
+
+	switch (property_id) {
+	case PROP_QUERY_OPTIONS:
+		self->priv->query_options = g_value_get_boxed (value);
+		break;
+	case PROP_PATH:
+		self->priv->path = g_strdup (g_value_get_string (value));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+static void
+couchdb_query_dispose (GObject *object)
+{
+	CouchdbQuery *self = COUCHDB_QUERY (object);
+
+	g_hash_table_destroy (self->priv->query_options);
+
+	if (self->priv->body)
+		json_object_unref (self->priv->body);
+
+ 	g_free (self->priv->query_string);
+	g_free (self->priv->path);
+	g_free (self->priv->method);
+
+	G_OBJECT_CLASS (couchdb_query_parent_class)->dispose (object);
+}
+
+static void
+couchdb_query_class_init (CouchdbQueryClass *klass)
+{
+	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+	GParamSpec *pspec;
+
+	gobject_class->dispose = couchdb_query_dispose;
+	gobject_class->set_property = couchdb_query_set_property;
+	gobject_class->get_property = couchdb_query_get_property;
+
+	g_type_class_add_private (klass, sizeof (CouchdbQueryPrivate));
+  
+	pspec = g_param_spec_boxed ("query-options",
+				    "Query Options",
+				    "Query options associated with the query",
+				    G_TYPE_HASH_TABLE,
+				    G_PARAM_READWRITE);
+	g_object_class_install_property (gobject_class,
+					 PROP_QUERY_OPTIONS,
+					 pspec);
+
+	pspec = g_param_spec_string ("path",
+				     "Query's Path",
+				     "A path for the query.",
+				     NULL,
+				     G_PARAM_READWRITE);
+	g_object_class_install_property (gobject_class,
+					 PROP_PATH,
+					 pspec);
+									      
+}
+
+static void
+couchdb_query_init (CouchdbQuery *self)
+{
+	self->priv = GET_PRIVATE (self);
+
+	self->priv->query_options =
+		g_hash_table_new_full (g_str_hash, g_str_equal,
+				       g_free, g_free);
+	self->priv->query_string = NULL;
+	self->priv->body = NULL;
+	self->priv->path = NULL;
+	self->priv->method = g_strdup ("GET");
+}
+
+CouchdbQuery *
+couchdb_query_new ()
+{
+	return g_object_new (COUCHDB_TYPE_QUERY, NULL);
+}
+
+CouchdbQuery *
+couchdb_query_new_for_path (const gchar *path)
+{
+	return g_object_new (COUCHDB_TYPE_QUERY,
+			     "path", path,
+			     NULL);
+}
+
+CouchdbQuery *
+couchdb_query_new_for_view (const gchar *design_doc, const gchar *view_name)
+{
+	CouchdbQuery *query;
+	gchar *path;
+
+	if (!g_str_has_prefix (design_doc, "_design")) {
+		path = g_strdup_printf ("_design/%s/_view/%s",
+					design_doc, view_name);
+	} else {
+		path = g_strdup_printf ("%s/_view/%s",
+					design_doc, view_name);
+	}
+
+	query = g_object_new (COUCHDB_TYPE_QUERY,
+			      "path", path,
+			      NULL);
+	
+	g_free (path);
+
+	return query;
+
+}
+
+const char *
+couchdb_query_get_option (CouchdbQuery *self, const gchar *name)
+{
+	g_return_val_if_fail (COUCHDB_IS_QUERY (self), NULL);
+
+	return g_hash_table_lookup (self->priv->query_options, name);
+}
+
+void
+couchdb_query_set_option (CouchdbQuery *self, const gchar *name,
+			  const gchar *value)
+{
+	g_return_if_fail (COUCHDB_IS_QUERY (self));
+
+	if (self->priv->query_string != NULL) {
+		g_free (self->priv->query_string);
+		self->priv->query_string = NULL;
+	}
+
+	g_hash_table_insert (self->priv->query_options,
+			     g_strdup (name), g_strdup (value));
+}
+
+const char *
+couchdb_query_get_query_options_string (CouchdbQuery *self)
+{
+	GHashTableIter iter;
+	gpointer key, value;
+	GString *query_string;
+
+	g_return_val_if_fail (COUCHDB_IS_QUERY (self), NULL);
+
+	if (self->priv->query_string != NULL) {
+		return self->priv->query_string;
+	}
+
+	query_string = g_string_new (NULL);
+
+	g_hash_table_iter_init (&iter, (GHashTable *) self->priv->query_options);
+
+	while (g_hash_table_iter_next (&iter, &key, &value)) {
+		/* key, startkey and endkey must be a properly url 
+		 * encoded JSON values */
+		if (!g_strcmp0 (key, "key") || !g_strcmp0 (key, "startkey") ||
+		    !g_strcmp0 (key, "endkey")) {
+			char *tmp;
+
+			tmp = couchdb_uri_encode (value);
+
+			/* an array of values can be passed as a key, in this
+			 * case don't add quotation marks around it */
+			g_string_append_printf (query_string,
+						*((char *) value) == '[' ?
+						"%s=%s&" : "%s=\"%s\"&",
+						(char *) key, (char *) tmp);
+
+			g_free (tmp);
+		} else {
+			g_string_append_printf (query_string, "%s=%s&",
+						(char *) key, (char *) value);
+		}
+	}
+
+	/* remove trailing & */
+	if (query_string->len > 0) {
+		query_string =
+			g_string_truncate (query_string, query_string->len-1);
+
+		return g_string_free (query_string, FALSE);
+	} else {
+		g_string_free (query_string, TRUE);
+
+		return NULL;
+	}
+}
+
+JsonObject *
+couchdb_query_get_json_object (CouchdbQuery *self)
+{
+	g_return_val_if_fail (COUCHDB_IS_QUERY (self), NULL);
+
+	return self->priv->body;
+}
+
+void
+couchdb_query_set_json_object (CouchdbQuery *self, JsonObject *body)
+{
+	g_return_if_fail (COUCHDB_IS_QUERY (self));
+
+	self->priv->body = json_object_ref (body);
+}
+
+void
+couchdb_query_set_path (CouchdbQuery *self, const char *path)
+{
+	g_return_if_fail (COUCHDB_IS_QUERY (self));
+
+	self->priv->path = g_strdup (path);
+}
+
+const char *
+couchdb_query_get_path (CouchdbQuery *self)
+{
+	g_return_val_if_fail (COUCHDB_IS_QUERY (self), NULL);
+
+	return (const gchar *) self->priv->path;
+}
+
+void
+couchdb_query_set_method (CouchdbQuery *self, const gchar *method)
+{
+	g_return_if_fail (COUCHDB_IS_QUERY (self));
+
+	self->priv->method = g_strdup (method);
+}
+
+const char *
+couchdb_query_get_method (CouchdbQuery *self)
+{
+	g_return_val_if_fail (COUCHDB_IS_QUERY (self), NULL);
+
+	return (const gchar *) self->priv->method;
+}
diff --git a/couchdb-glib/couchdb-query.h b/couchdb-glib/couchdb-query.h
new file mode 100644
index 0000000..69669e9
--- /dev/null
+++ b/couchdb-glib/couchdb-query.h
@@ -0,0 +1,85 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef __COUCHDB_QUERY_H__
+#define __COUCHDB_QUERY_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <json-glib/json-glib.h>
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_QUERY (couchdb_query_get_type ())
+#define COUCHDB_QUERY(obj)					\
+	(G_TYPE_CHECK_INSTANCE_CAST ((obj),			\
+				     COUCHDB_TYPE_QUERY,	\
+				     CouchdbQuery))
+#define COUCHDB_IS_QUERY(obj)					\
+	(G_TYPE_CHECK_INSTANCE_TYPE ((obj),			\
+				     COUCHDB_TYPE_QUERY))
+#define COUCHDB_QUERY_CLASS(klass)			\
+	(G_TYPE_CHECK_CLASS_CAST ((klass),		\
+				  COUCHDB_TYPE_QUERY,	\
+				  CouchdbQueryClass))
+#define COUCHDB_IS_QUERY_CLASS(klass)			\
+	(G_TYPE_CHECK_CLASS_TYPE ((klass),		\
+				  COUCHDB_TYPE_QUERY))
+#define COUCHDB_QUERY_GET_CLASS(obj)			\
+	(G_TYPE_INSTANCE_GET_CLASS ((obj),		\
+				    COUCHDB_TYPE_QUERY, \
+				    CouchdbQueryClass))
+
+typedef struct _CouchdbQueryPrivate CouchdbQueryPrivate;
+
+typedef struct {
+	GObject parent;
+	CouchdbQueryPrivate *priv;
+} CouchdbQuery;
+
+typedef struct {
+	GObjectClass parent_class;
+} CouchdbQueryClass;
+
+GType couchdb_query_get_type (void);
+CouchdbQuery *couchdb_query_new (void);
+CouchdbQuery *couchdb_query_new_for_path (const char *path);
+CouchdbQuery *couchdb_query_new_for_view (const char * design_doc,
+					  const char * view_name);
+
+const char *couchdb_query_get_path (CouchdbQuery * self);
+void couchdb_query_set_path (CouchdbQuery * self,
+			     const char * path);
+const char *couchdb_query_get_method (CouchdbQuery * self);
+void couchdb_query_set_method (CouchdbQuery * self,
+			       const char * method);
+const char *couchdb_query_get_option (CouchdbQuery * self,
+				      const char * name);
+void couchdb_query_set_option (CouchdbQuery * self,
+			       const char * name,
+			       const char * value);
+
+const char *couchdb_query_get_query_options_string (CouchdbQuery * self);
+JsonObject *couchdb_query_get_json_object (CouchdbQuery * self);
+void couchdb_query_set_json_object (CouchdbQuery * self, JsonObject * object);
+
+G_END_DECLS
+
+#endif /* __COUCHDB_QUERY_H__ */
diff --git a/couchdb-glib/couchdb-response.c b/couchdb-glib/couchdb-response.c
new file mode 100644
index 0000000..f392a79
--- /dev/null
+++ b/couchdb-glib/couchdb-response.c
@@ -0,0 +1,288 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include "couchdb-response.h"
+
+#define GET_PRIVATE(obj)					\
+	(G_TYPE_INSTANCE_GET_PRIVATE ((obj),			\
+				      COUCHDB_TYPE_RESPONSE,	\
+				      CouchdbResponsePrivate))
+
+G_DEFINE_TYPE(CouchdbResponse, couchdb_response, G_TYPE_OBJECT)
+
+struct _CouchdbResponsePrivate {
+	JsonObject *response;
+	gchar *etag;
+	guint status_code;
+	gchar *content_type;
+	gsize content_length;
+};
+
+enum {
+	PROP_0,
+	PROP_RESPONSE,
+	PROP_ETAG,
+	PROP_STATUS_CODE,
+	PROP_CONTENT_TYPE,
+	PROP_CONTENT_LENGTH
+};
+
+static void
+couchdb_response_get_property (GObject *object, guint property_id,
+			       GValue *value, GParamSpec *pspec)
+{
+	CouchdbResponse *self;
+
+	self = COUCHDB_RESPONSE (object);
+
+	switch (property_id) {
+ 	case PROP_RESPONSE:
+		g_value_set_boxed (value, self->priv->response);
+		break;
+	case PROP_ETAG:
+		g_value_set_string (value, self->priv->etag);
+		break;
+	case PROP_STATUS_CODE:
+		g_value_set_uint (value, self->priv->status_code);
+		break;
+	case PROP_CONTENT_TYPE:
+		g_value_set_string (value, self->priv->content_type);
+		break;
+	case PROP_CONTENT_LENGTH:
+		g_value_set_ulong (value, self->priv->content_length);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+static void
+couchdb_response_set_property (GObject *object, guint property_id,
+			       const GValue *value, GParamSpec *pspec)
+{
+	CouchdbResponse *self;
+
+	self = COUCHDB_RESPONSE (object);
+
+	switch (property_id) {
+	case PROP_RESPONSE:
+		if (self->priv->response != NULL)
+			json_object_unref (self->priv->response);
+		self->priv->response = g_value_dup_boxed (value);
+		break;
+	case PROP_ETAG:
+		if (self->priv->etag)
+			g_free (self->priv->etag);
+		self->priv->etag = g_value_dup_string (value);
+		break;
+	case PROP_STATUS_CODE:
+		self->priv->status_code = g_value_get_uint (value);
+		break;
+	case PROP_CONTENT_TYPE:
+		if (self->priv->content_type)
+			g_free (self->priv->content_type);
+		self->priv->content_type = g_value_dup_string (value);
+		break;
+	case PROP_CONTENT_LENGTH:
+		self->priv->content_length = g_value_get_ulong (value);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+static void
+couchdb_response_finalize (GObject *object)
+{
+	CouchdbResponse *self = COUCHDB_RESPONSE (object);
+	
+	if (self->priv->response) {
+		json_object_unref (self->priv->response);
+		self->priv->response = NULL;
+	}
+
+	g_free (self->priv->etag);
+	g_free (self->priv->content_type);
+
+	G_OBJECT_CLASS (couchdb_response_parent_class)->finalize (object);
+}
+
+static void
+couchdb_response_class_init (CouchdbResponseClass *klass)
+{
+	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+	GParamSpec *pspec;
+
+	gobject_class->finalize = couchdb_response_finalize;
+	gobject_class->set_property = couchdb_response_set_property;
+	gobject_class->get_property = couchdb_response_get_property;
+
+	g_type_class_add_private (klass, sizeof (CouchdbResponsePrivate));
+
+	pspec = g_param_spec_boxed ("response", "Response",
+				    "Json Response",
+				    JSON_TYPE_OBJECT,
+				    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+	g_object_class_install_property (gobject_class, PROP_RESPONSE, pspec);
+
+	pspec = g_param_spec_string ("etag", "ETag", "Entity Tag",
+				     NULL,
+				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+	g_object_class_install_property (gobject_class, PROP_ETAG, pspec);
+
+	pspec = g_param_spec_uint ("status-code", "Status Code",
+				   "Http Response Status Code",
+				   100, 599, 200,
+				   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+	g_object_class_install_property (gobject_class, PROP_STATUS_CODE, pspec);
+
+	pspec = g_param_spec_string ("content-type", "Content-Type",
+				     "Response Content-Type",
+				     NULL,
+				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+	g_object_class_install_property (gobject_class, PROP_CONTENT_TYPE, pspec);
+
+	pspec = g_param_spec_ulong ("content-length", "Content Length",
+				    "Content's Size",
+				    0, G_MAXULONG, 0,
+				    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+	g_object_class_install_property (gobject_class, PROP_CONTENT_LENGTH, pspec);
+}
+
+static void
+couchdb_response_init (CouchdbResponse *self)
+{
+	self->priv = GET_PRIVATE (self);
+
+	self->priv->response = NULL;
+}
+
+/**
+ * couchdb_response_get_json_object:
+ * @self: A #CouchdbResponse object
+ *
+ * Returns a #JsonObject containing the response from CouchDB server.
+ *
+ * Return value: (transfer none): A #JsonObject containing response from
+ * the server. Object is owned by #CouchdbResponse and should not be
+ * freed.
+ */
+JsonObject *
+couchdb_response_get_json_object(CouchdbResponse *self)
+{
+	g_return_val_if_fail(COUCHDB_IS_RESPONSE(self), NULL);
+
+	return self->priv->response;
+}
+
+/**
+ * couchdb_response_get_rows:
+ * @self: A #CouchdbResponse object
+ *
+ * Returns a list of #JsonObject rows returned by the CouchDB server.
+ *
+ * Return value: (element-type Json.Object) (transfer container): A #GList
+ * of #JsonObject objects. Returned objects are owned by #CouchdbResponse,
+ * #GList should be freed by caller when no longer needed.
+ */
+GList *
+couchdb_response_get_rows (CouchdbResponse *self)
+{
+	GList *nodes, *iter;
+
+	nodes = json_array_get_elements (
+		json_object_get_array_member (self->priv->response, "rows"));
+	
+	for (iter = nodes; iter != NULL; iter = g_list_next (iter)) {
+		iter->data = json_node_get_object (iter->data);;
+	}
+
+	return nodes;
+}
+
+/**
+ * couchdb_response_get_etag
+ * @self: A #CouchdbResponse object
+ *
+ * Returns the value of ETag header from the response. This header
+ * is set to the current revision of the document requested.
+ *
+ * Return value: A @string containing the ETag header or %NULL
+ * if there has been no ETag in response headers.
+ */
+const gchar *
+couchdb_response_get_etag (CouchdbResponse * self)
+{
+	g_return_val_if_fail (COUCHDB_IS_RESPONSE (self), NULL);
+
+	return (const gchar *) self->priv->etag;
+}
+
+/**
+ * couchdb_response_get_status_code
+ * @self: A #CouchdbResponse object
+ * 
+ * Returns the response status code
+ *
+ * Return value: status code of the response
+ */
+guint
+couchdb_response_get_status_code (CouchdbResponse * self)
+{
+	g_return_val_if_fail (COUCHDB_IS_RESPONSE (self), 500);
+
+	return self->priv->status_code;
+}
+
+/**
+ * couchdb_response_get_content_type
+ * @self: A #CouchdbResponse object
+ *
+ * Returns the string containing content type of the response.
+ * 
+ * Return value: Content-Type of the response or %NULL if the header
+ * wasn't set
+ */
+const gchar *
+couchdb_response_get_content_type (CouchdbResponse * self)
+{
+	g_return_val_if_fail (COUCHDB_IS_RESPONSE (self), NULL);
+
+	return (const gchar *) self->priv->content_type;
+}
+
+/**
+ * couchdb_response_get_content_length
+ * @self: A #CouchdbResponse object
+ *
+ * Returns content length of the CouchDB response.
+ *
+ * Return value: content length or 0 if response doesn't contain
+ * any body.
+ */
+gsize
+couchdb_response_get_content_length (CouchdbResponse * self)
+{
+	g_return_val_if_fail (COUCHDB_IS_RESPONSE (self), 0);
+
+	return self->priv->content_length;
+}
diff --git a/couchdb-glib/couchdb-response.h b/couchdb-glib/couchdb-response.h
new file mode 100644
index 0000000..4be6e45
--- /dev/null
+++ b/couchdb-glib/couchdb-response.h
@@ -0,0 +1,69 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef __COUCHDB_RESPONSE_H__
+#define __COUCHDB_RESPONSE_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <json-glib/json-glib.h>
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_RESPONSE (couchdb_response_get_type ())
+#define COUCHDB_RESPONSE(obj)					\
+	(G_TYPE_CHECK_INSTANCE_CAST ((obj),			\
+				     COUCHDB_TYPE_RESPONSE,	\
+				     CouchdbResponse))
+#define COUCHDB_IS_RESPONSE(obj)				\
+	(G_TYPE_CHECK_INSTANCE_TYPE ((obj),			\
+				     COUCHDB_TYPE_RESPONSE))
+#define COUCHDB_RESPONSE_CLASS(klass)				\
+	(G_TYPE_CHECK_CLASS_CAST ((klass),			\
+				  COUCHDB_TYPE_RESPONSE,	\
+				  CouchdbResponseClass))
+#define COUCHDB_IS_RESPONSE_CLASS(klass)			\
+	(G_TYPE_CHECK_CLASS_TYPE ((klass),			\
+				  COUCHDB_TYPE_RESPONSE))
+
+typedef struct _CouchdbResponsePrivate CouchdbResponsePrivate;
+
+typedef struct {
+	GObject parent;
+	CouchdbResponsePrivate *priv;
+} CouchdbResponse;
+
+typedef struct {
+	GObjectClass parent_class;
+} CouchdbResponseClass;
+
+GType couchdb_response_get_type (void);
+CouchdbResponse *couchdb_response_new (void);
+
+const char *couchdb_response_get_etag (CouchdbResponse *self);
+guint couchdb_response_get_status_code (CouchdbResponse *self);
+const char *couchdb_response_get_content_type (CouchdbResponse *self);
+gsize couchdb_response_get_content_length (CouchdbResponse *self);
+JsonObject *couchdb_response_get_json_object (CouchdbResponse *self);
+GList *couchdb_response_get_rows (CouchdbResponse *self);
+
+G_END_DECLS
+
+#endif /* __COUCHDB_RESPONSE_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]