[librest/wip/teuf/gtask: 3/28] OAuthProxy: Use GTask
- From: Christophe Fergeau <teuf src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librest/wip/teuf/gtask: 3/28] OAuthProxy: Use GTask
- Date: Mon, 20 Jun 2016 10:41:25 +0000 (UTC)
commit a4add5c205bb903c05aa6487251e47debfb8cd54
Author: Christophe Fergeau <cfergeau redhat com>
Date: Fri Jun 17 17:17:40 2016 +0200
OAuthProxy: Use GTask
Based on initial work from Timm Bäder <mail baedert org>
rest/oauth-proxy.c | 109 ++++++++++++++++++++++++++++++---------------------
rest/oauth-proxy.h | 47 ++++++++-------------
tests/oauth-async.c | 50 +++++++++++-------------
3 files changed, 105 insertions(+), 101 deletions(-)
---
diff --git a/rest/oauth-proxy.c b/rest/oauth-proxy.c
index dff54b4..20a3308 100644
--- a/rest/oauth-proxy.c
+++ b/rest/oauth-proxy.c
@@ -279,11 +279,6 @@ oauth_proxy_new_with_token (const char *consumer_key,
NULL);
}
-typedef struct {
- OAuthProxyAuthCallback callback;
- gpointer user_data;
-} AuthData;
-
/**
* oauth_proxy_request_token:
* @proxy: an #OAuthProxy
@@ -335,32 +330,31 @@ request_token_cb (RestProxyCall *call,
GObject *weak_object,
gpointer user_data)
{
- AuthData *data = user_data;
OAuthProxy *proxy = NULL;
+ GTask *task = G_TASK (user_data);
g_object_get (call, "proxy", &proxy, NULL);
g_assert (proxy);
- if (!error) {
+ if (error != NULL) {
+ g_task_return_error (task, g_error_copy (error));
+ } else {
oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
+ g_task_return_boolean (task, TRUE);
}
- data->callback (proxy, error, weak_object, data->user_data);
-
- g_slice_free (AuthData, data);
g_object_unref (call);
g_object_unref (proxy);
+ g_object_unref (task);
}
/**
* oauth_proxy_request_token_async:
* @proxy: an #OAuthProxy
- * @function: the function name to invoke
- * @callback_uri: the callback URI
+ * @function: (nullable): the function name to invoke
+ * @callback_uri: (nullable): the callback URI
* @callback: (scope async): a #OAuthProxyAuthCallback to invoke on completion
- * @weak_object: #GObject to weakly reference and tie the lifecycle of the method call too
* @user_data: user data to pass to @callback
- * @error: a #GError, or %NULL
*
* Perform the Request Token phase of OAuth, invoking @function (defaulting to
* "request_token" if @function is NULL).
@@ -375,17 +369,17 @@ request_token_cb (RestProxyCall *call,
* Returns: %TRUE if the method was successfully queued, or %FALSE on
* failure. On failure @error is set.
*/
-gboolean
-oauth_proxy_request_token_async (OAuthProxy *proxy,
- const char *function,
- const char *callback_uri,
- OAuthProxyAuthCallback callback,
- GObject *weak_object,
- gpointer user_data,
- GError **error)
+void
+oauth_proxy_request_token_async (OAuthProxy *proxy,
+ const char *function,
+ const char *callback_uri,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
RestProxyCall *call;
- AuthData *data;
+ GTask *task;
+ GError *error = NULL;
call = rest_proxy_new_call (REST_PROXY (proxy));
rest_proxy_call_set_function (call, function ? function : "request_token");
@@ -394,11 +388,23 @@ oauth_proxy_request_token_async (OAuthProxy *proxy,
if (callback_uri)
rest_proxy_call_add_param (call, "oauth_callback", callback_uri);
- data = g_slice_new0 (AuthData);
- data->callback = callback;
- data->user_data = user_data;
+ task = g_task_new (proxy, cancellable, callback, user_data);
- return rest_proxy_call_async (call, request_token_cb, weak_object, data, error);
+ rest_proxy_call_async (call, request_token_cb, NULL, task, &error);
+ if (error != NULL) {
+ g_task_return_error (task, error);
+ }
+}
+
+gboolean
+oauth_proxy_request_token_finish (OAuthProxy *proxy,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (OAUTH_IS_PROXY (proxy), FALSE);
+ g_return_val_if_fail (g_task_is_valid (result, proxy), FALSE);
+
+ return g_task_propagate_boolean (G_TASK (result), error);
}
/**
@@ -452,21 +458,22 @@ access_token_cb (RestProxyCall *call,
GObject *weak_object,
gpointer user_data)
{
- AuthData *data = user_data;
OAuthProxy *proxy = NULL;
+ GTask *task = G_TASK (user_data);
g_object_get (call, "proxy", &proxy, NULL);
g_assert (proxy);
- if (!error) {
+ if (error != NULL) {
+ g_task_return_error (task, g_error_copy (error));
+ } else {
oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
+ g_task_return_boolean (task, TRUE);
}
- data->callback (proxy, error, weak_object, data->user_data);
-
- g_slice_free (AuthData, data);
g_object_unref (call);
g_object_unref (proxy);
+ g_object_unref (task);
}
/**
@@ -493,17 +500,17 @@ access_token_cb (RestProxyCall *call,
* Returns: %TRUE if the method was successfully queued, or %FALSE on
* failure. On failure @error is set.
*/
-gboolean
-oauth_proxy_access_token_async (OAuthProxy *proxy,
- const char *function,
- const char *verifier,
- OAuthProxyAuthCallback callback,
- GObject *weak_object,
- gpointer user_data,
- GError **error)
+void
+oauth_proxy_access_token_async (OAuthProxy *proxy,
+ const char *function,
+ const char *verifier,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
RestProxyCall *call;
- AuthData *data;
+ GTask *task;
+ GError *error = NULL;
call = rest_proxy_new_call (REST_PROXY (proxy));
rest_proxy_call_set_function (call, function ? function : "access_token");
@@ -512,11 +519,23 @@ oauth_proxy_access_token_async (OAuthProxy *proxy,
if (verifier)
rest_proxy_call_add_param (call, "oauth_verifier", verifier);
- data = g_slice_new0 (AuthData);
- data->callback = callback;
- data->user_data = user_data;
+ task = g_task_new (proxy, cancellable, callback, user_data);
+
+ rest_proxy_call_async (call, access_token_cb, NULL, task, &error);
+ if (error != NULL) {
+ g_task_return_error (task, error);
+ }
+}
+
+gboolean
+oauth_proxy_access_token_finish (OAuthProxy *proxy,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (OAUTH_IS_PROXY (proxy), FALSE);
+ g_return_val_if_fail (g_task_is_valid (result, proxy), FALSE);
- return rest_proxy_call_async (call, access_token_cb, weak_object, data, error);
+ return g_task_propagate_boolean (G_TASK (result), error);
}
/**
diff --git a/rest/oauth-proxy.h b/rest/oauth-proxy.h
index 782216e..fa1a89e 100644
--- a/rest/oauth-proxy.h
+++ b/rest/oauth-proxy.h
@@ -91,33 +91,20 @@ RestProxy* oauth_proxy_new_with_token (const char *consumer_key,
const gchar *url_format,
gboolean binding_required);
-/**
- * OAuthProxyAuthCallback:
- * @proxy: the #OAuthProxy
- * @error: a #GError if the authentication failed, otherwise %NULL
- * @weak_object: the weak object passed to the caller
- * @userdata: the user data passed to the caller
- *
- * Callback from oauth_proxy_request_token_async() and
- * oauth_proxy_access_token_async().
- */
-typedef void (*OAuthProxyAuthCallback)(OAuthProxy *proxy,
- const GError *error,
- GObject *weak_object,
- gpointer userdata);
-
gboolean oauth_proxy_request_token (OAuthProxy *proxy,
const char *function,
const char *callback_uri,
GError **error);
-gboolean oauth_proxy_request_token_async (OAuthProxy *proxy,
- const char *function,
- const char *callback_uri,
- OAuthProxyAuthCallback callback,
- GObject *weak_object,
- gpointer user_data,
- GError **error);
+void oauth_proxy_request_token_async (OAuthProxy *proxy,
+ const char *function,
+ const char *callback_uri,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean oauth_proxy_request_token_finish (OAuthProxy *proxy,
+ GAsyncResult *result,
+ GError **error);
gboolean oauth_proxy_is_oauth10a (OAuthProxy *proxy);
@@ -126,13 +113,15 @@ gboolean oauth_proxy_access_token (OAuthProxy *proxy,
const char *verifier,
GError **error);
-gboolean oauth_proxy_access_token_async (OAuthProxy *proxy,
- const char *function,
- const char *verifier,
- OAuthProxyAuthCallback callback,
- GObject *weak_object,
- gpointer user_data,
- GError **error);
+void oauth_proxy_access_token_async (OAuthProxy *proxy,
+ const char *function,
+ const char *verifier,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean oauth_proxy_access_token_finish (OAuthProxy *proxy,
+ GAsyncResult *result,
+ GError **error);
const char * oauth_proxy_get_token (OAuthProxy *proxy);
diff --git a/tests/oauth-async.c b/tests/oauth-async.c
index c7c6143..b9a63b9 100644
--- a/tests/oauth-async.c
+++ b/tests/oauth-async.c
@@ -21,6 +21,7 @@
*/
#include <rest/oauth-proxy.h>
+#include <rest/oauth-proxy-call.h>
#include <rest/oauth-proxy-private.h>
#include <stdio.h>
#include <stdlib.h>
@@ -37,24 +38,24 @@ make_calls (OAuthProxy *oproxy, GMainLoop *loop)
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_function (call, "echo");
rest_proxy_call_add_param (call, "foo", "bar");
- if (!rest_proxy_call_run (call, NULL, &error))
- g_error ("Cannot make call: %s", error->message);
+ rest_proxy_call_sync (call, &error);
+ g_assert_no_error (error);
g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "foo=bar");
g_object_unref (call);
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_function (call, "echo");
rest_proxy_call_add_param (call, "numbers", "1234567890");
- if (!rest_proxy_call_run (call, NULL, &error))
- g_error ("Cannot make call: %s", error->message);
+ rest_proxy_call_sync (call, &error);
+ g_assert_no_error (error);
g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "numbers=1234567890");
g_object_unref (call);
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_function (call, "echo");
rest_proxy_call_add_param (call, "escape", "!£$%^&*()");
- if (!rest_proxy_call_run (call, NULL, &error))
- g_error ("Cannot make call: %s", error->message);
+ rest_proxy_call_sync (call, &error);
+ g_assert_no_error (error);
g_assert_cmpstr (rest_proxy_call_get_payload (call), ==, "escape=%21%C2%A3%24%25%5E%26%2A%28%29");
g_object_unref (call);
@@ -62,15 +63,18 @@ make_calls (OAuthProxy *oproxy, GMainLoop *loop)
}
static void
-access_token_cb (OAuthProxy *proxy,
- const GError *error,
- GObject *weak_object,
+access_token_cb (GObject *source_object,
+ GAsyncResult *result,
gpointer user_data)
{
+ OAuthProxy *proxy = OAUTH_PROXY (source_object);
OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
- g_assert_no_error ((GError *)error);
+ GError *error = NULL;
GMainLoop *loop = user_data;
+ oauth_proxy_access_token_finish (proxy, result, &error);
+ g_assert_no_error (error);
+
g_assert_cmpstr (priv->token, ==, "accesskey");
g_assert_cmpstr (priv->token_secret, ==, "accesssecret");
@@ -78,30 +82,24 @@ access_token_cb (OAuthProxy *proxy,
}
static void
-request_token_cb (OAuthProxy *proxy,
- const GError *error,
- GObject *weak_object,
+request_token_cb (GObject *source_object,
+ GAsyncResult *result,
gpointer user_data)
{
+ OAuthProxy *proxy = OAUTH_PROXY (source_object);
OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
- GError *err = NULL;
GMainLoop *loop = user_data;
+ GError *error = NULL;
- if (error != NULL && g_error_matches (error, REST_PROXY_ERROR, REST_PROXY_ERROR_CONNECTION))
- {
- g_main_loop_quit (loop);
- return;
- };
-
- g_assert_no_error ((GError *)error);
+ oauth_proxy_request_token_finish (proxy, result, &error);
+ g_assert_no_error (error);
g_assert_cmpstr (priv->token, ==, "requestkey");
g_assert_cmpstr (priv->token_secret, ==, "requestsecret");
/* Second stage authentication, this gets an access token */
oauth_proxy_access_token_async (proxy, "access-token", NULL,
- access_token_cb, NULL, loop, &err);
- g_assert_no_error (err);
+ NULL, access_token_cb, loop);
}
static gboolean
@@ -118,7 +116,6 @@ main (int argc, char **argv)
GMainLoop *loop = g_main_loop_new (NULL, TRUE);
RestProxy *proxy;
OAuthProxy *oproxy;
- GError *error = NULL;
/* Install a timeout so that we don't hang or infinite loop */
g_timeout_add_seconds (10, on_timeout, NULL);
@@ -133,9 +130,8 @@ main (int argc, char **argv)
g_assert (oproxy);
/* First stage authentication, this gets a request token */
- oauth_proxy_request_token_async (oproxy, "request-token", NULL,
- request_token_cb, NULL, loop, &error);
- g_assert_no_error (error);
+ oauth_proxy_request_token_async (OAUTH_PROXY (proxy), "request-token", NULL,
+ NULL, request_token_cb, loop);
g_main_loop_run (loop);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]