/soc/2015/nakulgulati/main: c2cc0f9ab9d0: hangouts: api - refact...
Nakul at rock.pidgin.im
Nakul at rock.pidgin.im
Mon Aug 17 21:05:27 EDT 2015
Changeset: c2cc0f9ab9d0c8049f56d5e4eec5d9c87e1771fe
Author: Nakul Gulati
Date: 2015-08-18 06:30 +0530
Branch: hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/c2cc0f9ab9d0
Description:
hangouts: api - refactor using json wrapper and signals
diffstat:
libpurple/protocols/hangouts/api.c | 112 ++++++++++++++++++--------------
libpurple/protocols/hangouts/hangouts.c | 8 ++
libpurple/protocols/hangouts/json.c | 4 +-
libpurple/protocols/hangouts/json.h | 3 +-
4 files changed, 74 insertions(+), 53 deletions(-)
diffs (281 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
@@ -27,6 +27,7 @@
#include "api.h"
#include "channel.h"
+#include "json.h"
#include "util.h"
//TODO api call backs
@@ -106,8 +107,14 @@ static void
hangouts_api_class_init (HangoutsApiClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+ gobject_class->dispose = hangouts_api_dispose;
g_type_class_add_private (klass, sizeof(HangoutsApiPrivate));
- gobject_class->dispose = hangouts_api_dispose;
+
+ g_signal_new ("auth", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0,
+ NULL,
+ NULL, g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ NULL);
}
static void
@@ -286,44 +293,41 @@ hangouts_api_get_galx_cookie (HangoutsAp
static void
hangouts_api_auth_with_code_cb (PurpleHttpConnection *http_conn,
PurpleHttpResponse *response,
- gpointer *user_data)
+ gpointer user_data)
{
- JsonParser *parser;
- JsonObject *json_ob;
HangoutsApi *api;
HangoutsApiPrivate *priv;
+ JsonNode *node;
PurpleAccount *acct;
- gchar *error, *access_token, *refresh_token;
+ const gchar *raw_response;
api = user_data;
priv = api->priv;
acct = purple_connection_get_account (priv->gc);
- gchar *raw_res = purple_http_response_get_data (response, NULL);
- purple_debug_misc ("hangouts-prpl", "raw:\n%s", raw_res);
+ raw_response = purple_http_response_get_data (response, NULL);
- parser = json_parser_new ();
- json_parser_load_from_data (parser, raw_res, -1, NULL);
- json_ob = json_node_get_object (json_parser_get_root (parser));
+ node = hangouts_json_node_new (raw_response, -1, FALSE);
- if (json_object_has_member (json_ob, "error"))
- error = json_object_get_string_member (json_ob, "error");
+ if (purple_http_response_is_successful (response))
+ {
+ priv->access_token = hangouts_json_node_get_str (node,
+ "$['access_token']",
+ NULL);
+ priv->refresh_token = hangouts_json_node_get_str (node,
+ "$['refresh_token']",
+ NULL);
- if (json_object_has_member (json_ob, "access_token"))
+ purple_account_set_remember_password (acct, TRUE);
+ purple_account_set_password (acct, priv->refresh_token, NULL, NULL);
+
+ g_signal_emit_by_name (api, "auth");
+ }
+ else
{
- priv->access_token = g_strdup (
- json_object_get_string_member (json_ob, "access_token"));
- hangouts_channel_set_access_token (priv->channel, priv->access_token);
+ //TODO handle error
}
- if (json_object_has_member (json_ob, "refresh_token"))
- {
- priv->refresh_token = g_strdup (
- json_object_get_string_member (json_ob, "refresh_token"));
- purple_debug_misc ("hangouts-prpl", "refresh_token: %s",
- priv->refresh_token);
- purple_account_set_remember_password (acct, TRUE);
- purple_account_set_password (acct, priv->refresh_token, NULL, NULL);
- }
+ json_node_free (node);
}
void
@@ -360,26 +364,29 @@ hangouts_api_oauth_refresh_token_cb (Pur
PurpleHttpResponse *response,
gpointer user_data)
{
- JsonParser *parser;
- JsonObject *json_ob;
HangoutsApi *api;
- gchar *raw_res;
- gchar *error;
+ HangoutsApiPrivate *priv;
+ JsonNode *node;
+ const gchar *raw_response;
api = user_data;
- raw_res = purple_http_response_get_data (response, NULL);
+ priv = api->priv;
+ raw_response = purple_http_response_get_data (response, NULL);
+ node = hangouts_json_node_new (raw_response, -1, FALSE);
- parser = json_parser_new ();
- json_parser_load_from_data (parser, raw_res, -1, NULL);
- json_ob = json_node_get_object (json_parser_get_root (parser));
+ if (purple_http_response_is_successful (response))
+ {
+ priv->access_token = hangouts_json_node_get_str (node,
+ "$['access_token']",
+ NULL);
+ g_signal_emit_by_name (api, "auth");
+ }
+ else
+ {
+ //TODO: handle refresh token request error
+ }
- if (json_object_has_member (json_ob, "error"))
- /*TODO Throw error*/
- error = json_object_get_string_member (json_ob, "error");
-
- if (json_object_has_member (json_ob, "access_token"))
- api->priv->access_token = g_strdup (
- json_object_get_string_member (json_ob, "access_token"));
+ json_node_free (node);
}
void
@@ -576,7 +583,7 @@ hangouts_api_get_request_header (Hangout
gchar *header = g_strdup_printf ("["
"[6, 3, %s, %s],"
"[%s, %s],"
- "None,"
+ "null,"
"\"en\""
"]",
priv->header_version, priv->header_date,
@@ -591,20 +598,24 @@ hangouts_api_request (HangoutsApi *api,
{
HangoutsApiPrivate *priv = api->priv;
- gchar *url = g_strconcat (HANGOUTS_API_BASE_URL, info->endpoint, "?alt=json");
+ gchar *url;
+ gchar *content_type = "application/json+protobuf";
PurpleHttpRequest *request;
+ url = g_strconcat (HANGOUTS_API_BASE_URL, info->endpoint, "?alt=json",
+ NULL);
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",
+ g_strdup_printf ("Bearer %s", priv->access_token));
purple_http_request_set_method (request, info->method);
purple_http_request_set_contents (request, request_body, -1);
purple_http_request (priv->gc, request, info->callback, api);
@@ -653,7 +664,7 @@ hangouts_api_syncallevents (HangoutsApi
"%s, "
"%s,"
"[], "
- "None, "
+ "null, "
"[], "
"False, "
"[], "
@@ -669,7 +680,10 @@ hangouts_api_sendchatmessage_cb (PurpleH
PurpleHttpResponse *response,
gpointer user_data)
{
+ gchar *raw;
+ raw = purple_http_response_get_data (response, NULL);
+ purple_debug_misc ("hangouts-api-send", "response: %s", raw);
}
void
@@ -686,7 +700,7 @@ hangouts_api_sendchatmessage (HangoutsAp
client_generated_id = g_random_int_range (0, G_MAXINT32);
header = hangouts_api_get_request_header (api);
- image_info = "None";
+ image_info = "null";
if (image_id != NULL)
{
@@ -695,18 +709,18 @@ hangouts_api_sendchatmessage (HangoutsAp
body = g_strdup_printf ("["
"%s"
- ",None, None, None, [], "
+ ",null, null, null, [], "
"[%s, []], "
"%s,"
"[[%s],"
"%d,"
"%d,],"
- "None, None, None, "
+ "null, null, null, "
"[]]",
header, segments, image_info, conversation_id,
client_generated_id, otr_status);
- hangouts_api_request (api, &info, FALSE, body);
+ hangouts_api_request (api, &info, FALSE, g_strcompress (body));
}
static void
diff --git a/libpurple/protocols/hangouts/hangouts.c b/libpurple/protocols/hangouts/hangouts.c
--- a/libpurple/protocols/hangouts/hangouts.c
+++ b/libpurple/protocols/hangouts/hangouts.c
@@ -34,6 +34,12 @@
#include "hangouts.h"
#include "api.h"
+void
+hangouts_cb_api_auth (HangoutsApi *api, gpointer *data)
+{
+ purple_debug_misc ("hangouts-prpl", "auth done");
+}
+
static PurpleProtocol *_hangouts_protocol = NULL;
static void
@@ -57,6 +63,8 @@ hangouts_login (PurpleAccount *acct)
password = purple_connection_get_password (gc);
api = hangouts_api_new (gc);
+ g_signal_connect(api, "auth", G_CALLBACK(hangouts_cb_api_auth), NULL);
+
if (password != NULL)
{
hangouts_api_set_refresh_token (api, password);
diff --git a/libpurple/protocols/hangouts/json.c b/libpurple/protocols/hangouts/json.c
--- a/libpurple/protocols/hangouts/json.c
+++ b/libpurple/protocols/hangouts/json.c
@@ -19,12 +19,10 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
-#include "debug.h"
-
#include "json.h"
JsonNode *
-hangouts_json_node_new (gchar *data, gssize length, gboolean empty_values)
+hangouts_json_node_new (const gchar *data, gssize length, gboolean empty_values)
{
JsonNode *root;
JsonParser *parser;
diff --git a/libpurple/protocols/hangouts/json.h b/libpurple/protocols/hangouts/json.h
--- a/libpurple/protocols/hangouts/json.h
+++ b/libpurple/protocols/hangouts/json.h
@@ -26,7 +26,8 @@
#include <json-glib/json-glib.h>
JsonNode *
-hangouts_json_node_new (gchar *data, gssize length, gboolean empty_values);
+hangouts_json_node_new (const gchar *data, gssize length,
+ gboolean empty_values);
JsonNode *
hangouts_json_node_get (JsonNode *root, const gchar *expr, GError **error);
More information about the Commits
mailing list