[virt-tools-list] [virt-viewer 02/20] Add OvirtRestCall

Christophe Fergeau cfergeau at redhat.com
Wed Jun 13 12:23:10 UTC 2012


This inherits from RestProxyCall and is needed to serialize
parameters to XML in REST calls instead of getting the default
behaviour.
---
 src/Makefile.am       |    1 +
 src/ovirt-rest-call.c |   92 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/ovirt-rest-call.h |   68 ++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 src/ovirt-rest-call.c
 create mode 100644 src/ovirt-rest-call.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 38fb683..f76024c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -40,6 +40,7 @@ endif
 
 if HAVE_OVIRT
 COMMON_SOURCES +=						\
+	ovirt-rest-call.h ovirt-rest-call.c			\
 	$(NULL)
 endif
 
diff --git a/src/ovirt-rest-call.c b/src/ovirt-rest-call.c
new file mode 100644
index 0000000..2dc6946
--- /dev/null
+++ b/src/ovirt-rest-call.c
@@ -0,0 +1,92 @@
+/*
+ * ovirt-rest-call.c: oVirt librest call proxy
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Christophe Fergeau <cfergeau at redhat.com>
+ */
+
+#include <config.h>
+
+#include "ovirt-rest-call.h"
+#include <rest/rest-params.h>
+
+#define OVIRT_REST_CALL_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), OVIRT_TYPE_REST_CALL, OvirtRestCallPrivate))
+
+G_DEFINE_TYPE(OvirtRestCall, ovirt_rest_call, REST_TYPE_PROXY_CALL);
+
+GQuark ovirt_rest_call_error_quark(void)
+{
+    return g_quark_from_static_string("ovirt-rest-call");
+}
+
+static gboolean ovirt_rest_call_class_serialize_params(RestProxyCall *call,
+                                                     gchar **content_type,
+                                                     gchar **content,
+                                                     gsize *content_len,
+                                                     GError **error)
+{
+    RestParams *params;
+    RestParamsIter it;
+    GString *body;
+    const char *name;
+    RestParam *param;
+
+    g_return_val_if_fail(OVIRT_IS_REST_CALL(call), FALSE);
+    g_return_val_if_fail(content_type != NULL, FALSE);
+    g_return_val_if_fail(content != NULL, FALSE);
+    g_return_val_if_fail(content_len != NULL, FALSE);
+
+    params = rest_proxy_call_get_params(call);
+    if (!rest_params_are_strings(params)) {
+        g_set_error(error, OVIRT_REST_CALL_ERROR, 0,
+                    "unexpected parameter type in REST call");
+        return FALSE;
+    }
+
+    body = g_string_new("<action>");
+    rest_params_iter_init(&it, params);
+    while (rest_params_iter_next(&it, &name, &param)) {
+        const char *val = rest_param_get_content(param);
+        g_string_append_printf(body, "<%s>%s</%s>", name, val, name);
+    }
+    g_string_append(body, "</action>");
+
+    *content_type = g_strdup("application/xml");
+    *content = body->str;
+    *content_len = body->len;
+
+    g_string_free(body, FALSE);
+
+    return TRUE;
+}
+
+static void ovirt_rest_call_class_init(OvirtRestCallClass *klass)
+{
+    REST_PROXY_CALL_CLASS(klass)->serialize_params = ovirt_rest_call_class_serialize_params;
+}
+
+
+static void ovirt_rest_call_init(G_GNUC_UNUSED OvirtRestCall *call)
+{
+}
+
+OvirtRestCall *ovirt_rest_call_new(RestProxy *proxy)
+{
+    return OVIRT_REST_CALL(g_object_new(OVIRT_TYPE_REST_CALL, "proxy", proxy, NULL));
+}
diff --git a/src/ovirt-rest-call.h b/src/ovirt-rest-call.h
new file mode 100644
index 0000000..bd844d4
--- /dev/null
+++ b/src/ovirt-rest-call.h
@@ -0,0 +1,68 @@
+/*
+ * ovirt-rest-call.h: oVirt librest call proxy
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: Christophe Fergeau <cfergeau at redhat.com>
+ */
+#ifndef __OVIRT_REST_CALL_H__
+#define __OVIRT_REST_CALL_H__
+
+#include <rest/rest-proxy.h>
+
+G_BEGIN_DECLS
+
+#define OVIRT_TYPE_REST_CALL            (ovirt_rest_call_get_type ())
+#define OVIRT_REST_CALL(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), OVIRT_TYPE_REST_CALL, OvirtRestCall))
+#define OVIRT_REST_CALL_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), OVIRT_TYPE_REST_CALL, OvirtRestCallClass))
+#define OVIRT_IS_REST_CALL(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), OVIRT_TYPE_REST_CALL))
+#define OVIRT_IS_REST_CALL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), OVIRT_TYPE_REST_CALL))
+#define OVIRT_REST_CALL_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), OVIRT_TYPE_REST_CALL, OvirtRestCallClass))
+
+typedef struct _OvirtRestCall OvirtRestCall;
+typedef struct _OvirtRestCallPrivate OvirtRestCallPrivate;
+typedef struct _OvirtRestCallClass OvirtRestCallClass;
+
+struct _OvirtRestCall
+{
+    RestProxyCall parent;
+
+    OvirtRestCallPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _OvirtRestCallClass
+{
+    RestProxyCallClass parent_class;
+
+    gpointer padding[20];
+};
+
+typedef enum {
+    OVIRT_REST_CALL_ERROR_XML
+} OvirtRestCallError;
+
+#define OVIRT_REST_CALL_ERROR ovirt_rest_call_error_quark()
+
+GQuark ovirt_rest_call_error_quark(void);
+GType ovirt_rest_call_get_type(void);
+OvirtRestCall *ovirt_rest_call_new(RestProxy *proxy);
+
+G_END_DECLS
+
+#endif /* __OVIRT_REST_CALL_H__ */
-- 
1.7.10.2




More information about the virt-tools-list mailing list