/soc/2015/nakulgulati/main: 96fc8bc4f89c: hangouts: api- refacto...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Mon Jul 27 05:38:50 EDT 2015


Changeset: 96fc8bc4f89c70e9bde2dfd62163a05035022228
Author:	 Nakul Gulati
Date:	 2015-07-27 17:37 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/96fc8bc4f89c

Description:

hangouts: api- refactor and sendchatmessage api

diffstat:

 libpurple/protocols/hangouts/api.c |  193 +++++++++++++++++++++++++-----------
 libpurple/protocols/hangouts/api.h |   45 ++++++-
 2 files changed, 171 insertions(+), 67 deletions(-)

diffs (truncated from 319 to 300 lines):

diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -22,10 +22,15 @@
 #include "api.h"
 #include "auth.h"
 
-//TODO get data api ready
+//TODO api call backs
 
 G_DEFINE_TYPE (HangoutsApi, hangouts_api, G_TYPE_OBJECT);
 
+enum _HangoutsConversationOffTheRecordStatus
+{
+  HANGOUTS_OFF_THE_RECORD = 1, HANGOUTS_ON_THE_RECORD = 2
+};
+
 struct _HangoutsApiPrivate
 {
   PurpleConnection *gc;
@@ -41,6 +46,11 @@ struct _HangoutsApiPrivate
   GTimeVal *last_sync_timestamp;
 };
 
+struct _HangoutsApiHttpInfo
+{
+  PurpleHttpCallback callback;
+};
+
 static void
 hangouts_api_dispose (GObject *obj)
 {
@@ -48,7 +58,7 @@ hangouts_api_dispose (GObject *obj)
 
   if (G_LIKELY (priv->gc != NULL))
     {
-      purple_http_conn_cancel_all (gc);
+      purple_http_conn_cancel_all (priv->gc);
     }
 
   hangouts_auth_cookies_free (priv->cookies);
@@ -95,30 +105,6 @@ hangouts_api_new (PurpleConnection *gc)
   return api;
 }
 
-void
-hangouts_api_request (HangoutsApi *api, gchar *url_endpoint, gboolean use_json,
-		      gchar *request_body)
-{
-
-  HangoutsApiPrivate *priv = api->priv;
-  gchar *url = g_strconcat (HANGOUTS_API_BASE_URL, url_endpoint, "?alt=json");
-  PurpleHttpRequest *request = purple_http_request_new (url);
-  gchar *content_type = "application/json+protobuf";
-
-  if (use_json)
-    {
-      content_type = "application/json";
-    }
-
-  purple_http_request_header_add (request, "content-type", content_type);
-  purple_http_request_header_add (
-      request, "Authorization",
-      hangouts_auth_get_authorization_header (priv->access_token));
-  purple_http_request_set_method (request, "POST");
-  purple_http_request_set_contents (request, request_body);
-  //TODO: complete and implement callback
-}
-
 gchar *
 hangouts_api_get_request_header (HangoutsApi *api)
 {
@@ -138,59 +124,150 @@ hangouts_api_get_request_header (Hangout
 }
 
 void
+hangouts_api_request (HangoutsApi *api, const HangoutsApiHttpInfo *info,
+		      gchar *url_endpoint, gboolean use_json,
+		      gchar *request_body)
+{
+
+  HangoutsApiPrivate *priv = api->priv;
+  gchar *url = g_strconcat (HANGOUTS_API_BASE_URL, url_endpoint, "?alt=json");
+  PurpleHttpRequest *request;
+
+  request = purple_http_request_new (url);
+  purple_http_request_set_max_len (request, -1);
+
+  gchar *content_type = "application/json+protobuf";
+
+  if (use_json)
+    {
+      content_type = "application/json";
+    }
+
+  purple_http_request_header_add (request, "content-type", content_type);
+  purple_http_request_header_add (
+      request, "Authorization",
+      hangouts_auth_get_authorization_header (priv->access_token));
+  purple_http_request_set_method (request, "POST");
+  purple_http_request_set_contents (request, request_body);
+  purple_http_request (priv->gc, request, info->callback, api);
+}
+
+void
 hangouts_api_getselfinfo (HangoutsApi *api)
 {
-  gchar *header = hangouts_api_get_request_header (api);
-  gchar *url_endpoint = "contacts/selfinfo";
-  gchar *request_body = g_strdup_printf ("[%s,[], []]", header);
+  static const HangoutsApiHttpInfo *info =
+    {
 
-  hangouts_api_request (api, url_endpoint, TRUE, request_body);
+    };
+
+  gchar *url_endpoint, *header, *body;
+
+  url_endpoint = "contacts/selfinfo";
+  header = hangouts_api_get_request_header (api);
+  body = g_strdup_printf ("[%s,[], []]", header);
+
+  hangouts_api_request (api, info, url_endpoint, TRUE, body);
 }
 
 void
 hangouts_api_syncallevents (HangoutsApi *api)
 {
-  gchar *header = hangouts_api_get_request_header (api);
-  GTimeVal *time_stamp = api->priv->last_sync_timestamp;
-  gchar *url_endpoint = "conversations/syncallnewevents";
-  gchar *request_body = g_strdup_printf ("["
-					 "%s, "
-					 "%s,"
-					 "[], "
-					 "None, "
-					 "[], "
-					 "False, "
-					 "[], "
-					 "1048576"
-					 "]",
-					 header,
-					 g_time_val_to_iso8601 (time_stamp));
+  static const HangoutsApiHttpInfo *info =
+    {
 
-  hangouts_api_request (api, url_endpoint, TRUE, request_body);
+    };
+
+  gchar *url_endpoint, *header, *body;
+  GTimeVal *time_stamp;
+
+  time_stamp = api->priv->last_sync_timestamp;
+  url_endpoint = "conversations/syncallnewevents";
+  header = hangouts_api_get_request_header (api);
+  body = g_strdup_printf ("["
+			  "%s, "
+			  "%s,"
+			  "[], "
+			  "None, "
+			  "[], "
+			  "False, "
+			  "[], "
+			  "1048576"
+			  "]",
+			  header, g_time_val_to_iso8601 (time_stamp));
+
+  hangouts_api_request (api, info, url_endpoint, FALSE, body);
+}
+
+void
+hangouts_api_sendchatmessage (HangoutsApi *api, gchar *conversation_id,
+			      gchar *segments, gchar *image_id,
+			      HangoutsOffTheRecordStatus otr_status)
+{
+  //TODO: segments should be in the protojson format
+  static const HangoutsApiHttpInfo *info =
+    {
+
+    };
+
+  guint32 client_generated_id;
+  gchar *url_endpoint, *image_info, *body, *header;
+
+  client_generated_id = g_random_int_range (0, G_MAXINT32);
+  url_endpoint = "conversations/sendchatmessage";
+  header = hangouts_api_get_request_header (api);
+  image_info = "None";
+
+  if (image_id != NULL)
+    {
+      image_info = g_strdup_printf ("[[%s, False]]", image_id);
+    }
+
+  body = g_strdup_printf ("["
+			  "%s"
+			  ",None, None, None, [], "
+			  "[%s, []], "
+			  "%s,"
+			  "[[%s],"
+			  "%d,"
+			  "%d,],"
+			  "None, None, None, "
+			  "[]]",
+			  header, segments, image_info, conversation_id,
+			  client_generated_id, otr_status);
+
+  hangouts_api_request (api, info, url_endpoint, FALSE, body);
 }
 
 void
 hangouts_api_setactiveclient (HangoutsApi *api, gboolean is_active,
 			      gchar *timeout_sec)
 {
-  gchar *header = hangouts_api_get_request_header (api);
-  HangoutsApiPrivate *priv = api->priv;
-  gchar *url_endpoint = "clients/setactiveclient";
-  gchar *is_active_flag = "False";
+  static const HangoutsApiHttpInfo *info =
+    {
+
+    };
+
+  gchar *url_endpoint, *is_active_flag, *body, *header;
+  HangoutsApiPrivate *priv;
+
+  url_endpoint = "clients/setactiveclient";
+  header = hangouts_api_get_request_header (api);
+  priv = api->priv;
+  is_active_flag = "False";
 
   if (is_active)
     {
       is_active_flag = "False";
     }
 
-  gchar *request_body = g_strdup_printf ("["
-					 "%s, "
-					 "%s, "
-					 "%s\/%s, "
-					 "%s]",
-					 header, is_active_flag, priv->email,
-					 priv->client_id, timeout_sec);
+  body = g_strdup_printf ("["
+			  "%s, "
+			  "%s, "
+			  "%s\/%s, "
+			  "%s]",
+			  header, is_active_flag, priv->email, priv->client_id,
+			  timeout_sec);
 
-  hangouts_api_request (api, url_endpoint, TRUE, request_body);
+  hangouts_api_request (api, info, url_endpoint, FALSE, body);
 }
 
diff --git a/libpurple/protocols/hangouts/api.h b/libpurple/protocols/hangouts/api.h
--- a/libpurple/protocols/hangouts/api.h
+++ b/libpurple/protocols/hangouts/api.h
@@ -42,24 +42,51 @@
 #define HANGOUTS_IS_API_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass), HANGOUTS_TYPE_API))
 #define HANGOUTS_API_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), HANGOUTS_TYPE_API, HangoutsApiClass))
 
+typedef enum _HangoutsConversationOffTheRecordStatus HangoutsOffTheRecordStatus;
+
 typedef struct _HangoutsApi HangoutsApi;
-typedef struct _hangouts_api_data HangoutsApiData;
+typedef struct _HangoutsApiClass HangoutsApiClass;
+typedef struct _HangoutsApiPrivate HangoutsApiPrivate;
+typedef struct _HangoutsApiHttpInfo HangoutsApiHttpInfo;
 
-struct _HangoutsApi{
-	GObject parent;
-	HangoutsApiData *api_data;
+struct _HangoutsApi
+{
+  GObject parent;
+  HangoutsApiPrivate *priv;
+};
+
+struct _HangoutsApiClass
+{
+  GObject parent;
 };
 
 GType
-hangouts_api_get_type(void);
+hangouts_api_get_type (void);
 
-void hangouts_api_getselfinfo(HangoutsApi *hangoutsApi);
-void hangouts_api_syncallevents(HangoutsApi *hangoutsApi);
-void hangouts_api_sendchatmessage(HangoutsApi *hangoutsApi, gchar *conversation_id, gchar *segments, gchar *image_id);
-void hangouts_api_setactiveclient(HangoutsApi *hangoutsApi, gboolean is_active, gchar *timeout_sec);
+HangoutsApi *
+hangouts_api_new (PurpleConnection *gc);
 
+gchar *
+hangouts_api_get_request_header (HangoutsApi *api);
 
+void
+hangouts_api_request (HangoutsApi *api, const HangoutsApiHttpInfo *info,



More information about the Commits mailing list