/soc/2015/nakulgulati/main: ebe266a487af: hangouts: api.c

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Mon Jul 27 03:57:31 EDT 2015


Changeset: ebe266a487af34cd31ad0c32108b887cb43994f6
Author:	 Nakul Gulati
Date:	 2015-07-27 15:57 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/ebe266a487af

Description:

hangouts: api.c

diffstat:

 libpurple/protocols/hangouts/api.c |  219 +++++++++++++++++++++++-------------
 1 files changed, 139 insertions(+), 80 deletions(-)

diffs (257 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
@@ -24,114 +24,173 @@
 
 //TODO get data api ready
 
-G_DEFINE_TYPE(HangoutsApi, hangouts_api, G_TYPE_OBJECT);
+G_DEFINE_TYPE (HangoutsApi, hangouts_api, G_TYPE_OBJECT);
 
-typedef struct {
-	hangouts_oauth_cookies *cookies;
-	gchar *api_key;
-	gchar *access_token;
-	gchar *header_date;
-	gchar *header_version;
-	gchar *header_id;
-	gchar *client_id;
-	gchar *email;
-	GTimeVal *last_sync_timestamp;
+struct _HangoutsApiPrivate
+{
+  PurpleConnection *gc;
+  HangoutsAuthCookies *cookies;
 
-} _hangouts_api_data;
+  gchar *api_key;
+  gchar *access_token;
+  gchar *header_date;
+  gchar *header_version;
+  gchar *header_id;
+  gchar *client_id;
+  gchar *email;
+  GTimeVal *last_sync_timestamp;
+};
 
-void hangouts_api_request(HangoutsApi *hangoutsApi, gchar *url_endpoint, gboolean use_protojson, gchar *request_body) {
+static void
+hangouts_api_dispose (GObject *obj)
+{
+  HangoutsApiPrivate *priv = HANGOUTS_API(obj)->priv;
 
-	HangoutsApiData *data = hangoutsApi->api_data;
-	gchar *url = g_strconcat(HANGOUTS_API_BASE_URL, url_endpoint,"?alt=json");
-	PurpleHttpRequest *request = purple_http_request_new(url);
-	PurpleHttpConnection *ret;
-	gchar *content_type;
+  if (G_LIKELY (priv->gc != NULL))
+    {
+      purple_http_conn_cancel_all (gc);
+    }
 
-	if(use_protojson){
-		content_type = "application/json+protobuf";
-		purple_http_request_header_add(request, "content-type", content_type);
-	}
+  hangouts_auth_cookies_free (priv->cookies);
 
-	purple_http_request_header_add(request, "Authorization", hangouts_auth_get_authorization_header(data->access_token));
-	purple_http_request_set_method(request, "POST");
-	purple_http_request_set_contents(request, request_body);
-	//TODO: complete and implement callback
+  g_free (priv->access_token);
+  g_free (priv->api_key);
+  g_free (priv->client_id);
+  g_free (priv->email);
+  g_free (priv->header_date);
+  g_free (priv->header_id);
+  g_free (priv->header_version);
+  g_free (priv->last_sync_timestamp);
+}
+
+static void
+hangouts_api_class_init (HangoutsApiClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  g_type_class_add_private (klass, sizeof(HangoutsApiPrivate));
+  gobject_class->dispose = hangouts_api_dispose;
+}
+
+static void
+hangouts_api_init (HangoutsApi *api)
+{
+  HangoutsApiPrivate *priv;
+
+  priv = G_TYPE_INSTANCE_GET_PRIVATE (api, HANGOUTS_TYPE_API,
+				      HangoutsApiPrivate);
+  api->priv = priv;
+}
+
+HangoutsApi *
+hangouts_api_new (PurpleConnection *gc)
+{
+  HangoutsApi *api;
+  HangoutsApiPrivate *priv;
+
+  api = g_object_new (HANGOUTS_TYPE_API, NULL);
+  priv = api->priv;
+
+  priv->gc = 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 *hangoutsApi) {
+hangouts_api_get_request_header (HangoutsApi *api)
+{
 
-	HangoutsApiData *data = hangoutsApi->api_data;
+  HangoutsApiPrivate *priv = api->priv;
 
-	gchar *header = g_strdup_printf("["
-			"[6, 3, %s, %s],"
-			"[%s, %s],"
-			"None,"
-			"\"en\""
-			"]",
-			data->header_version,
-			data->header_date,
-			data->client_id,
-			data->header_id);
+  gchar *header = g_strdup_printf ("["
+				   "[6, 3, %s, %s],"
+				   "[%s, %s],"
+				   "None,"
+				   "\"en\""
+				   "]",
+				   priv->header_version, priv->header_date,
+				   priv->client_id, priv->header_id);
 
-	return header;
+  return header;
 }
 
 void
-hangouts_api_getselfinfo(HangoutsApi *hangoutsApi)
+hangouts_api_getselfinfo (HangoutsApi *api)
 {
-	gchar *header = hangouts_api_get_request_header(hangoutsApi);
-	gchar *url_endpoint = "contacts/selfinfo";
-	gchar *request_body = g_strdup_printf("[%s,[], []]",header);
+  gchar *header = hangouts_api_get_request_header (api);
+  gchar *url_endpoint = "contacts/selfinfo";
+  gchar *request_body = g_strdup_printf ("[%s,[], []]", header);
 
-	hangouts_api_request(hangoutsApi, url_endpoint, TRUE, request_body);
+  hangouts_api_request (api, url_endpoint, TRUE, request_body);
 }
 
 void
-hangouts_api_syncallevents(HangoutsApi *hangoutsApi)
+hangouts_api_syncallevents (HangoutsApi *api)
 {
-	gchar *header = hangouts_api_get_request_header(hangoutsApi);
-	GTimeVal *time_stamp = hangoutsApi->api_data->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));
+  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));
 
-
-	hangouts_api_request(hangoutsApi, url_endpoint, TRUE, request_body);
+  hangouts_api_request (api, url_endpoint, TRUE, request_body);
 }
 
 void
-hangouts_api_setactiveclient(HangoutsApi *hangoutsApi, gboolean is_active, gchar *timeout_sec)
+hangouts_api_setactiveclient (HangoutsApi *api, gboolean is_active,
+			      gchar *timeout_sec)
 {
-	gchar *header = hangouts_api_get_request_header(hangoutsApi);
-	HangoutsApiData *data = hangoutsApi->api_data;
-	gchar *url_endpoint = "clients/setactiveclient";
-	gchar *is_active_flag = "False";
+  gchar *header = hangouts_api_get_request_header (api);
+  HangoutsApiPrivate *priv = api->priv;
+  gchar *url_endpoint = "clients/setactiveclient";
+  gchar *is_active_flag = "False";
 
-	if(is_active){
-		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,
-			data->email,
-			data->client_id,
-			timeout_sec);
+  gchar *request_body = g_strdup_printf ("["
+					 "%s, "
+					 "%s, "
+					 "%s\/%s, "
+					 "%s]",
+					 header, is_active_flag, priv->email,
+					 priv->client_id, timeout_sec);
 
-	hangouts_api_request(hangoutsApi, url_endpoint, TRUE, request_body);
+  hangouts_api_request (api, url_endpoint, TRUE, request_body);
 }
 



More information about the Commits mailing list