[librest] proxy-call: Allow customisation of data serialization (BMC#13746)



commit 9545fef5381120b77d7c5341a95733228b641c0f
Author: Michal Mhr <michal mhr gmail com>
Date:   Wed Apr 13 16:41:57 2011 +0100

    proxy-call: Allow customisation of data serialization (BMC#13746)

 rest/rest-proxy-call.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++-
 rest/rest-proxy-call.h |   15 ++++++++++++-
 2 files changed, 65 insertions(+), 2 deletions(-)
---
diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c
index 22272f8..6ffa56c 100644
--- a/rest/rest-proxy-call.c
+++ b/rest/rest-proxy-call.c
@@ -745,7 +745,24 @@ prepare_message (RestProxyCall *call, GError **error_out)
     }
   }
 
-  if (rest_params_are_strings (priv->params)) {
+  if (call_class->serialize_params) {
+    gchar *content;
+    gchar *content_type;
+    gsize content_len;
+
+    if (!call_class->serialize_params (call, &content_type, 
+                                       &content, &content_len, &error))
+    {
+      g_propagate_error (error_out, error);
+      return NULL;
+    }
+
+    message = soup_message_new (priv->method, priv->url);
+    soup_message_set_request (message, content_type, 
+                              SOUP_MEMORY_TAKE, content, content_len);
+
+    g_free (content_type);
+  } else if (rest_params_are_strings (priv->params)) {
     GHashTable *hash;
 
     hash = rest_params_as_string_hash_table (priv->params);
@@ -1228,3 +1245,36 @@ rest_proxy_call_get_status_message (RestProxyCall *call)
 
   return priv->status_message;
 }
+
+/**
+ * rest_proxy_call_serialize_params:
+ * @call: The #RestProxyCall
+ * @content_type: (out): Content type of the payload
+ * @content: (out): The payload
+ * @content_len: (out): Length of the payload data
+ * @error: a #GError, or %NULL
+ *
+ * Invoker for a virtual method to serialize the parameters for this
+ * #RestProxyCall.
+ *
+ * Returns: TRUE if the serialization was successful, FALSE otherwise.
+ */
+gboolean
+rest_proxy_call_serialize_params (RestProxyCall *call,
+                                  gchar **content_type,
+                                  gchar **content,
+                                  gsize *content_len,
+                                  GError **error)
+{
+  RestProxyCallClass *call_class;
+
+  call_class = REST_PROXY_CALL_GET_CLASS (call);
+
+  if (call_class->serialize_params)
+  {
+    return call_class->serialize_params (call, content_type,
+                                         content, content_len, error);
+  }
+
+  return FALSE;
+}
diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h
index 6f65914..918b35c 100644
--- a/rest/rest-proxy-call.h
+++ b/rest/rest-proxy-call.h
@@ -61,6 +61,8 @@ typedef struct {
  * RestProxyCallClass:
  * @prepare: Virtual function called before making the request, This allows the
  * call to be modified, for example to add a signature.
+ * @serialize_params: Virtual function allowing custom serialization of the
+ * parameters, for example when the API doesn't expect standard form content.
  *
  * Class structure for #RestProxyCall for subclasses to implement specialised
  * behaviour.
@@ -70,10 +72,15 @@ typedef struct {
   GObjectClass parent_class;
   /*< public >*/
   gboolean (*prepare)(RestProxyCall *call, GError **error);
+  gboolean (*serialize_params) (RestProxyCall *call,
+                                gchar **content_type,
+                                gchar **content,
+                                gsize *content_len,
+                                GError **error);
 
   /*< private >*/
   /* padding for future expansion */
-  gpointer _padding_dummy[8];
+  gpointer _padding_dummy[7];
 } RestProxyCallClass;
 
 #define REST_PROXY_CALL_ERROR rest_proxy_call_error_quark ()
@@ -184,6 +191,12 @@ goffset rest_proxy_call_get_payload_length (RestProxyCall *call);
 const gchar *rest_proxy_call_get_payload (RestProxyCall *call);
 guint rest_proxy_call_get_status_code (RestProxyCall *call);
 const gchar *rest_proxy_call_get_status_message (RestProxyCall *call);
+gboolean rest_proxy_call_serialize_params (RestProxyCall *call,
+                                           gchar        **content_type,
+                                           gchar        **content,
+                                           gsize         *content_len,
+                                           GError       **error);
+
 
 G_END_DECLS
 



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