[libgdata/wip/rishi/drive: 6/7] core: Add support for JSON to GDataAccessHandler



commit bba303d1850b28b452f71cd8645922200b75358f
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Apr 16 13:23:15 2015 +0200

    core: Add support for JSON to GDataAccessHandler
    
    This includes initial code for detection of the Content Type of
    received messages, and parsing JSON or XML depending on that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/gdata-access-handler.c |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
---
diff --git a/gdata/gdata-access-handler.c b/gdata/gdata-access-handler.c
index a119510..a802914 100644
--- a/gdata/gdata-access-handler.c
+++ b/gdata/gdata-access-handler.c
@@ -2,6 +2,7 @@
 /*
  * GData Client
  * Copyright (C) Philip Withnall 2009–2010 <philip tecnocode co uk>
+ * Copyright (C) Red Hat, Inc. 2015
  *
  * GData Client is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -88,6 +89,8 @@ _gdata_access_handler_get_rules (GDataAccessHandler *self, GDataService *service
        GDataFeed *feed;
        GDataLink *_link;
        SoupMessage *message;
+       SoupMessageHeaders *headers;
+       const gchar *content_type;
 
        _link = gdata_entry_look_up_link (GDATA_ENTRY (self), GDATA_LINK_ACCESS_CONTROL_LIST);
        g_assert (_link != NULL);
@@ -103,8 +106,23 @@ _gdata_access_handler_get_rules (GDataAccessHandler *self, GDataService *service
        }
 
        g_assert (message->response_body->data != NULL);
-       feed = _gdata_feed_new_from_xml (GDATA_TYPE_FEED, message->response_body->data, 
message->response_body->length, GDATA_TYPE_ACCESS_RULE,
-                                        progress_callback, progress_user_data, error);
+
+       headers = message->response_headers;
+       content_type = soup_message_headers_get_content_type (headers, NULL);
+
+       if (g_strcmp0 (content_type, "application/json") == 0) {
+               /* Definitely JSON. */
+               g_debug("JSON content type detected.");
+               feed = _gdata_feed_new_from_json (GDATA_TYPE_FEED, message->response_body->data, 
message->response_body->length, GDATA_TYPE_ACCESS_RULE,
+                                                 progress_callback, progress_user_data, error);
+       } else {
+               /* Potentially XML. Don't bother checking the Content-Type, since the parser
+                * will fail gracefully if the response body is not valid XML. */
+               g_debug("XML content type detected.");
+               feed = _gdata_feed_new_from_xml (GDATA_TYPE_FEED, message->response_body->data, 
message->response_body->length, GDATA_TYPE_ACCESS_RULE,
+                                                progress_callback, progress_user_data, error);
+       }
+
        g_object_unref (message);
 
        return feed;


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