[libgovirt] Rework Ovirt*RestCall



commit c544fac2f04b287d22f1a593c0c21064a57d4756
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Thu Sep 5 15:52:15 2013 +0200

    Rework Ovirt*RestCall
    
    Rename OvirtRestCall to OvirtActionRestCall and add a base abstract
    OvirtRestCall class that will be shared by OvirtActionRestCall and
    OvirtResourceRestCall.

 govirt/Makefile.am                |    2 +
 govirt/govirt-private.h           |    1 +
 govirt/ovirt-action-rest-call.c   |  102 +++++++++++++++++++++++++++++++++++++
 govirt/ovirt-action-rest-call.h   |   61 ++++++++++++++++++++++
 govirt/ovirt-proxy.c              |    4 +-
 govirt/ovirt-resource-rest-call.c |   11 +----
 govirt/ovirt-resource-rest-call.h |    6 +-
 govirt/ovirt-rest-call.c          |   95 +++++++++++++---------------------
 govirt/ovirt-rest-call.h          |    1 -
 govirt/ovirt-vm.c                 |    2 +-
 10 files changed, 209 insertions(+), 76 deletions(-)
---
diff --git a/govirt/Makefile.am b/govirt/Makefile.am
index 7a84fbf..0c694ca 100644
--- a/govirt/Makefile.am
+++ b/govirt/Makefile.am
@@ -31,6 +31,7 @@ libgovirt_la_HEADERS =                                                \
 noinst_HEADERS =                                               \
        glib-compat.h                                           \
        govirt-private.h                                        \
+       ovirt-action-rest-call.h                                \
        ovirt-api-private.h                                     \
        ovirt-collection-private.h                              \
        ovirt-proxy-private.h                                   \
@@ -44,6 +45,7 @@ noinst_HEADERS =                                              \
 
 libgovirt_la_SOURCES =                                         \
        glib-compat.c                                           \
+       ovirt-action-rest-call.c                                \
        ovirt-api.c                                             \
        ovirt-cdrom.c                                           \
        ovirt-collection.c                                      \
diff --git a/govirt/govirt-private.h b/govirt/govirt-private.h
index 07b5606..972ebac 100644
--- a/govirt/govirt-private.h
+++ b/govirt/govirt-private.h
@@ -22,6 +22,7 @@
 #ifndef __OVIRT_PRIVATE_H__
 #define __OVIRT_PRIVATE_H__
 
+#include <govirt/ovirt-action-rest-call.h>
 #include <govirt/ovirt-api-private.h>
 #include <govirt/ovirt-collection-private.h>
 #include <govirt/ovirt-enum-types-private.h>
diff --git a/govirt/ovirt-action-rest-call.c b/govirt/ovirt-action-rest-call.c
new file mode 100644
index 0000000..a4bf7c4
--- /dev/null
+++ b/govirt/ovirt-action-rest-call.c
@@ -0,0 +1,102 @@
+/*
+ * ovirt-action-rest-call.c: oVirt librest call proxy
+ *
+ * Copyright (C) 2012, 2013 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+
+#include <config.h>
+
+#include "ovirt-action-rest-call.h"
+#include "ovirt-proxy.h"
+#include "ovirt-rest-call-error.h"
+#include <rest/rest-params.h>
+
+#define OVIRT_ACTION_REST_CALL_GET_PRIVATE(obj)                         \
+        (G_TYPE_INSTANCE_GET_PRIVATE((obj), OVIRT_TYPE_ACTION_REST_CALL, OvirtActionRestCallPrivate))
+
+G_DEFINE_TYPE(OvirtActionRestCall, ovirt_action_rest_call, OVIRT_TYPE_REST_CALL);
+
+static gboolean ovirt_action_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_ACTION_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_action_rest_call_class_init(OvirtActionRestCallClass *klass)
+{
+    REST_PROXY_CALL_CLASS(klass)->serialize_params = ovirt_action_rest_call_class_serialize_params;
+}
+
+
+static void ovirt_action_rest_call_init(G_GNUC_UNUSED OvirtActionRestCall *call)
+{
+}
+
+OvirtActionRestCall *ovirt_action_rest_call_new(RestProxy *proxy)
+{
+    OvirtActionRestCall *call;
+    gboolean admin;
+
+    g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
+    call = OVIRT_ACTION_REST_CALL(g_object_new(OVIRT_TYPE_ACTION_REST_CALL, "proxy", proxy, NULL));
+    g_return_val_if_fail(call != NULL, NULL);
+    g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
+    if (admin) {
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "false");
+    } else {
+        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "true");
+    }
+
+    return call;
+}
diff --git a/govirt/ovirt-action-rest-call.h b/govirt/ovirt-action-rest-call.h
new file mode 100644
index 0000000..9fb6f6f
--- /dev/null
+++ b/govirt/ovirt-action-rest-call.h
@@ -0,0 +1,61 @@
+/*
+ * ovirt-action-rest-call.h: oVirt librest call proxy
+ *
+ * Copyright (C) 2012, 2013 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Christophe Fergeau <cfergeau redhat com>
+ */
+#ifndef __OVIRT_ACTION_REST_CALL_H__
+#define __OVIRT_ACTION_REST_CALL_H__
+
+#include <ovirt-rest-call.h>
+
+G_BEGIN_DECLS
+
+#define OVIRT_TYPE_ACTION_REST_CALL            (ovirt_action_rest_call_get_type ())
+#define OVIRT_ACTION_REST_CALL(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
OVIRT_TYPE_ACTION_REST_CALL, OvirtActionRestCall))
+#define OVIRT_ACTION_REST_CALL_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), 
OVIRT_TYPE_ACTION_REST_CALL, OvirtActionRestCallClass))
+#define OVIRT_IS_ACTION_REST_CALL(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
OVIRT_TYPE_ACTION_REST_CALL))
+#define OVIRT_IS_ACTION_REST_CALL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), 
OVIRT_TYPE_ACTION_REST_CALL))
+#define OVIRT_ACTION_REST_CALL_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), 
OVIRT_TYPE_ACTION_REST_CALL, OvirtActionRestCallClass))
+
+typedef struct _OvirtActionRestCall OvirtActionRestCall;
+typedef struct _OvirtActionRestCallPrivate OvirtActionRestCallPrivate;
+typedef struct _OvirtActionRestCallClass OvirtActionRestCallClass;
+
+struct _OvirtActionRestCall
+{
+    OvirtRestCall parent;
+
+    OvirtActionRestCallPrivate *priv;
+
+    /* Do not add fields to this struct */
+};
+
+struct _OvirtActionRestCallClass
+{
+    OvirtRestCallClass parent_class;
+
+    gpointer padding[20];
+};
+
+G_GNUC_INTERNAL GType ovirt_action_rest_call_get_type(void);
+G_GNUC_INTERNAL OvirtActionRestCall *ovirt_action_rest_call_new(RestProxy *proxy);
+
+G_END_DECLS
+
+#endif /* __OVIRT_ACTION_REST_CALL_H__ */
diff --git a/govirt/ovirt-proxy.c b/govirt/ovirt-proxy.c
index 232709e..21877ba 100644
--- a/govirt/ovirt-proxy.c
+++ b/govirt/ovirt-proxy.c
@@ -140,7 +140,7 @@ RestXmlNode *ovirt_proxy_get_collection_xml(OvirtProxy *proxy,
 
     g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
 
-    call = REST_PROXY_CALL(ovirt_rest_call_new(REST_PROXY(proxy)));
+    call = REST_PROXY_CALL(ovirt_action_rest_call_new(REST_PROXY(proxy)));
     href = ovirt_utils_strip_api_base_dir(href);
     rest_proxy_call_set_function(call, href);
     rest_proxy_call_add_header(call, "All-Content", "true");
@@ -238,7 +238,7 @@ void ovirt_rest_call_async(OvirtProxy *proxy,
     g_return_if_fail(OVIRT_IS_PROXY(proxy));
     g_return_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable));
 
-    call = REST_PROXY_CALL(ovirt_rest_call_new(REST_PROXY(proxy)));
+    call = REST_PROXY_CALL(ovirt_action_rest_call_new(REST_PROXY(proxy)));
     if (method != NULL) {
         rest_proxy_call_set_method(call, method);
     }
diff --git a/govirt/ovirt-resource-rest-call.c b/govirt/ovirt-resource-rest-call.c
index 5f25e35..d04054e 100644
--- a/govirt/ovirt-resource-rest-call.c
+++ b/govirt/ovirt-resource-rest-call.c
@@ -36,7 +36,7 @@
 struct _OvirtResourceRestCallPrivate {
     OvirtResource *resource;
 } ;
-G_DEFINE_TYPE(OvirtResourceRestCall, ovirt_resource_rest_call, REST_TYPE_PROXY_CALL);
+G_DEFINE_TYPE(OvirtResourceRestCall, ovirt_resource_rest_call, OVIRT_TYPE_REST_CALL);
 
 enum {
     PROP_0,
@@ -142,21 +142,12 @@ OvirtResourceRestCall *ovirt_resource_rest_call_new(RestProxy *proxy,
                                                     OvirtResource *resource)
 {
     OvirtResourceRestCall *call;
-    gboolean admin;
 
     g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
     call = OVIRT_RESOURCE_REST_CALL(g_object_new(OVIRT_TYPE_RESOURCE_REST_CALL,
                                                  "proxy", proxy,
                                                  "resource", resource,
                                                  NULL));
-    g_return_val_if_fail(call != NULL, NULL);
-    g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
-    if (admin) {
-        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "false");
-    } else {
-        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "true");
-    }
-
 
     return call;
 }
diff --git a/govirt/ovirt-resource-rest-call.h b/govirt/ovirt-resource-rest-call.h
index 290d7e6..4c5422f 100644
--- a/govirt/ovirt-resource-rest-call.h
+++ b/govirt/ovirt-resource-rest-call.h
@@ -23,7 +23,7 @@
 #define __OVIRT_RESOURCE_REST_CALL_H__
 
 #include <govirt/ovirt-resource.h>
-#include <rest/rest-proxy.h>
+#include <govirt/ovirt-rest-call.h>
 
 G_BEGIN_DECLS
 
@@ -40,7 +40,7 @@ typedef struct _OvirtResourceRestCallClass OvirtResourceRestCallClass;
 
 struct _OvirtResourceRestCall
 {
-    RestProxyCall parent;
+    OvirtRestCall parent;
 
     OvirtResourceRestCallPrivate *priv;
 
@@ -49,7 +49,7 @@ struct _OvirtResourceRestCall
 
 struct _OvirtResourceRestCallClass
 {
-    RestProxyCallClass parent_class;
+    OvirtRestCallClass parent_class;
 
     gpointer padding[20];
 };
diff --git a/govirt/ovirt-rest-call.c b/govirt/ovirt-rest-call.c
index 5167e0d..f56ec3a 100644
--- a/govirt/ovirt-rest-call.c
+++ b/govirt/ovirt-rest-call.c
@@ -1,7 +1,7 @@
 /*
  * ovirt-rest-call.c: oVirt librest call proxy
  *
- * Copyright (C) 2012 Red Hat, Inc.
+ * Copyright (C) 2012, 2013 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
@@ -22,86 +22,63 @@
 
 #include <config.h>
 
+#include <rest/rest-params.h>
+
 #include "ovirt-proxy.h"
 #include "ovirt-rest-call.h"
 #include "ovirt-rest-call-error.h"
-#include <rest/rest-params.h>
+#include "ovirt-utils.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);
+
+struct _OvirtRestCallPrivate {
+    gboolean unused;
+};
+
+
+G_DEFINE_ABSTRACT_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);
+static void ovirt_rest_call_constructed(GObject *object)
+{
+    OvirtProxy *proxy;
+
+    G_OBJECT_CLASS(ovirt_rest_call_parent_class)->constructed(object);
+
+    g_object_get(object, "proxy", &proxy, NULL);
+    if (proxy != NULL) {
+        gboolean admin;
+        g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
+        if (admin) {
+            rest_proxy_call_add_header(REST_PROXY_CALL(object), "Filter", "false");
+        } else {
+            rest_proxy_call_add_header(REST_PROXY_CALL(object), "Filter", "true");
+        }
+        g_object_unref(proxy);
     }
-    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;
-}
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+    GParamSpec *param_spec;
 
+    g_type_class_add_private(klass, sizeof(OvirtRestCallPrivate));
 
-static void ovirt_rest_call_init(G_GNUC_UNUSED OvirtRestCall *call)
-{
+    object_class->constructed = ovirt_rest_call_constructed;
 }
 
-OvirtRestCall *ovirt_rest_call_new(RestProxy *proxy)
-{
-    OvirtRestCall *call;
-    gboolean admin;
-
-    g_return_val_if_fail(OVIRT_IS_PROXY(proxy), NULL);
-    call = OVIRT_REST_CALL(g_object_new(OVIRT_TYPE_REST_CALL, "proxy", proxy, NULL));
-    g_return_val_if_fail(call != NULL, NULL);
-    g_object_get(G_OBJECT(proxy), "admin", &admin, NULL);
-    if (admin) {
-        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "false");
-    } else {
-        rest_proxy_call_add_header(REST_PROXY_CALL(call), "Filter", "true");
-    }
 
-    return call;
+static void ovirt_rest_call_init(G_GNUC_UNUSED OvirtRestCall *call)
+{
+    call->priv = OVIRT_REST_CALL_GET_PRIVATE(call);
 }
diff --git a/govirt/ovirt-rest-call.h b/govirt/ovirt-rest-call.h
index f53b6be..57c08a7 100644
--- a/govirt/ovirt-rest-call.h
+++ b/govirt/ovirt-rest-call.h
@@ -54,7 +54,6 @@ struct _OvirtRestCallClass
 };
 
 G_GNUC_INTERNAL GType ovirt_rest_call_get_type(void);
-G_GNUC_INTERNAL OvirtRestCall *ovirt_rest_call_new(RestProxy *proxy);
 
 G_END_DECLS
 
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
index 1628495..07acf29 100644
--- a/govirt/ovirt-vm.c
+++ b/govirt/ovirt-vm.c
@@ -320,7 +320,7 @@ ovirt_vm_action(OvirtVm *vm, OvirtProxy *proxy, const char *action,
     function = ovirt_utils_strip_api_base_dir(function);
     g_return_val_if_fail(function != NULL, FALSE);
 
-    call = REST_PROXY_CALL(ovirt_rest_call_new(REST_PROXY(proxy)));
+    call = REST_PROXY_CALL(ovirt_action_rest_call_new(REST_PROXY(proxy)));
     rest_proxy_call_set_method(call, "POST");
     rest_proxy_call_set_function(call, function);
     rest_proxy_call_add_param(call, "async", "false");


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