[librest/wip/teuf/gtask: 36/37] fixup! RestProxyCall: Use GTask



commit 559ee1fe9699cbd284f975bb977e46e8df891b72
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Thu Jun 16 18:03:50 2016 +0200

    fixup! RestProxyCall: Use GTask

 rest/oauth-proxy.c  |   90 ++++++++++++++++++++++++++++----------------------
 rest/oauth-proxy.h  |   45 ++++++++++---------------
 tests/oauth-async.c |   64 ++++++++++++------------------------
 3 files changed, 89 insertions(+), 110 deletions(-)
---
diff --git a/rest/oauth-proxy.c b/rest/oauth-proxy.c
index 5801cd6..b0df950 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 {
-  GAsyncReadyCallback callback;
-  gpointer user_data;
-} AuthData;
-
 /**
  * oauth_proxy_request_token:
  * @proxy: an #OAuthProxy
@@ -335,20 +330,20 @@ request_token_cb (GObject      *source_object,
                   GAsyncResult *result,
                   gpointer      user_data)
 {
-  AuthData *data = user_data;
+  GTask *task = G_TASK (user_data);
   RestProxyCall *call = REST_PROXY_CALL (source_object);
-  OAuthProxy *proxy = NULL;
-
-  g_object_get (call, "proxy", &proxy, NULL);
-  g_assert (proxy);
+  GError *error = NULL;
 
-  oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
+  rest_proxy_call_invoke_finish (call, result, &error);
 
-  data->callback (source_object, result, data->user_data);
+  if (error != NULL) {
+    g_task_return_error (task, error);
+  } else {
+    oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
+    g_task_return_boolean (task, TRUE);
+  }
 
-  g_slice_free (AuthData, data);
-  g_object_unref (call);
-  g_object_unref (proxy);
+  g_object_unref (task);
 }
 
 /**
@@ -372,7 +367,7 @@ request_token_cb (GObject      *source_object,
  * Returns: %TRUE if the method was successfully queued, or %FALSE on
  * failure. On failure @error is set.
  */
-RestProxyCall *
+void
 oauth_proxy_request_token_async (OAuthProxy          *proxy,
                                  const char          *function,
                                  const char          *callback_uri,
@@ -381,7 +376,7 @@ oauth_proxy_request_token_async (OAuthProxy          *proxy,
                                  gpointer             user_data)
 {
   RestProxyCall *call;
-  AuthData *data;
+  GTask *task;
 
   call = rest_proxy_new_call (REST_PROXY (proxy));
   rest_proxy_call_set_function (call, function ? function : "request_token");
@@ -390,13 +385,22 @@ 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->user_data = user_data;
-  data->callback = callback;
+  task = g_task_new (proxy, cancellable, callback, user_data);
 
-  rest_proxy_call_invoke_async (call, cancellable, request_token_cb, data);
+  rest_proxy_call_invoke_async (call, cancellable, request_token_cb, task);
 
-  return call;
+  g_object_unref (call);
+}
+
+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);
 }
 
 /**
@@ -450,22 +454,20 @@ access_token_cb (GObject      *source_object,
                  GAsyncResult *result,
                  gpointer      user_data)
 {
-  AuthData *data = user_data;
+  GTask *task = G_TASK (user_data);
   RestProxyCall *call = REST_PROXY_CALL (source_object);
-  OAuthProxy *proxy = NULL;
+  GError *error = NULL;
 
-  g_object_get (call, "proxy", &proxy, NULL);
-  g_assert (proxy);
+  rest_proxy_call_invoke_finish (call, result, &error);
 
-  /*if (!error) {*/
+  if (error != NULL) {
+    g_task_return_error (task, error);
+  } else {
     oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
-  /*}*/
-
-  data->callback (source_object, result, data->user_data);
+    g_task_return_boolean (task, TRUE);
+  }
 
-  g_slice_free (AuthData, data);
-  g_object_unref (call);
-  g_object_unref (proxy);
+  g_object_unref (task);
 }
 
 /**
@@ -492,7 +494,7 @@ access_token_cb (GObject      *source_object,
  * Returns: %TRUE if the method was successfully queued, or %FALSE on
  * failure. On failure @error is set.
  */
-RestProxyCall *
+void
 oauth_proxy_access_token_async (OAuthProxy          *proxy,
                                 const char          *function,
                                 const char          *verifier,
@@ -500,8 +502,8 @@ oauth_proxy_access_token_async (OAuthProxy          *proxy,
                                 GAsyncReadyCallback  callback,
                                 gpointer             user_data)
 {
-  AuthData *data;
   RestProxyCall *call;
+  GTask *task;
 
   call = rest_proxy_new_call (REST_PROXY (proxy));
   rest_proxy_call_set_function (call, function ? function : "access_token");
@@ -510,13 +512,21 @@ oauth_proxy_access_token_async (OAuthProxy          *proxy,
   if (verifier)
     rest_proxy_call_add_param (call, "oauth_verifier", verifier);
 
-  data = g_slice_new0 (AuthData);
-  data->user_data = user_data;
-  data->callback = callback;
+  task = g_task_new (proxy, cancellable, callback, user_data);
 
-  rest_proxy_call_invoke_async (call, cancellable, access_token_cb, data);
+  rest_proxy_call_invoke_async (call, cancellable, access_token_cb, task);
+  g_object_unref (call);
+}
 
-  return call;
+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 g_task_propagate_boolean (G_TASK (result), error);
 }
 
 /**
diff --git a/rest/oauth-proxy.h b/rest/oauth-proxy.h
index 4b9f700..fa1a89e 100644
--- a/rest/oauth-proxy.h
+++ b/rest/oauth-proxy.h
@@ -91,32 +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);
 
-RestProxyCall *oauth_proxy_request_token_async (OAuthProxy          *proxy,
-                                                const char          *function,
-                                                const char          *callback_uri,
-                                                GCancellable        *cancellable,
-                                                GAsyncReadyCallback  callback,
-                                                gpointer             user_data);
+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);
 
@@ -125,12 +113,15 @@ gboolean oauth_proxy_access_token (OAuthProxy *proxy,
                                    const char *verifier,
                                    GError    **error);
 
-RestProxyCall *oauth_proxy_access_token_async (OAuthProxy          *proxy,
-                                               const char          *function,
-                                               const char          *verifier,
-                                               GCancellable        *cancellable,
-                                               GAsyncReadyCallback  callback,
-                                               gpointer             user_data);
+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 d933f7b..6f696d0 100644
--- a/tests/oauth-async.c
+++ b/tests/oauth-async.c
@@ -67,23 +67,20 @@ access_token_cb2 (GObject      *source_object,
                   GAsyncResult *result,
                   gpointer      user_data)
 {
-  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
-  OAuthProxy *proxy;
+  OAuthProxy *proxy = OAUTH_PROXY (source_object);
   const gchar *token, *token_secret;
   GError *error = NULL;
   GMainLoop *loop = user_data;
 
-  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  oauth_proxy_access_token_finish (proxy, result, &error);
   g_assert_no_error (error);
 
-  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
-
-  token = oauth_proxy_get_token (OAUTH_PROXY (proxy));
+  token = oauth_proxy_get_token (proxy);
   g_assert_cmpstr (token, ==, "accesskey");
-  token_secret = oauth_proxy_get_token_secret (OAUTH_PROXY (proxy));
+  token_secret = oauth_proxy_get_token_secret (proxy);
   g_assert_cmpstr (token_secret, ==, "accesssecret");
 
-  make_calls (OAUTH_PROXY (proxy), loop);
+  make_calls (proxy, loop);
 }
 
 
@@ -93,21 +90,17 @@ access_token_cb1 (GObject      *source_object,
                   GAsyncResult *result,
                   gpointer      user_data)
 {
-  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
-  OAuthProxy *proxy;
+  OAuthProxy *proxy = OAUTH_PROXY (source_object);
   const gchar *token, *token_secret;
   GError *error = NULL;
   GMainLoop *loop = user_data;
 
-  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  oauth_proxy_access_token_finish (proxy, result, &error);
   g_assert_no_error (error);
 
-  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
-  g_assert (OAUTH_IS_PROXY (proxy));
-
-  token = oauth_proxy_get_token (OAUTH_PROXY (proxy));
+  token = oauth_proxy_get_token (proxy);
   g_assert_cmpstr (token, ==, "accesskey");
-  token_secret = oauth_proxy_get_token_secret (OAUTH_PROXY (proxy));
+  token_secret = oauth_proxy_get_token_secret (proxy);
   g_assert_cmpstr (token_secret, ==, "accesssecret");
 
   g_main_loop_quit (loop);
@@ -118,22 +111,17 @@ request_token_cb3 (GObject      *source_object,
                    GAsyncResult *result,
                    gpointer      user_data)
 {
-  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
+  OAuthProxy *proxy = OAUTH_PROXY (source_object);
   GMainLoop *loop = user_data;
   const char *token, *token_secret;
-  OAuthProxy *proxy;
   GError *error = NULL;
 
-  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  oauth_proxy_request_token_finish (proxy, result, &error);
   g_assert_no_error (error);
 
-  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
-
-  g_assert (OAUTH_IS_PROXY (proxy));
-
-  token = oauth_proxy_get_token (OAUTH_PROXY (proxy));
+  token = oauth_proxy_get_token (proxy);
   g_assert_cmpstr (token, ==, "requestkey");
-  token_secret = oauth_proxy_get_token_secret (OAUTH_PROXY (proxy));
+  token_secret = oauth_proxy_get_token_secret (proxy);
   g_assert_cmpstr (token_secret, ==, "requestsecret");
 
   /* Second stage authentication, this gets an access token */
@@ -146,22 +134,17 @@ request_token_cb2 (GObject      *source_object,
                    GAsyncResult *result,
                    gpointer      user_data)
 {
-  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
+  OAuthProxy *proxy = OAUTH_PROXY (source_object);
   GMainLoop *loop = user_data;
   const char *token, *token_secret;
-  OAuthProxy *proxy;
   GError *error = NULL;
 
-  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  oauth_proxy_request_token_finish (proxy, result, &error);
   g_assert_no_error (error);
 
-  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
-
-  g_assert (OAUTH_IS_PROXY (proxy));
-
-  token = oauth_proxy_get_token (OAUTH_PROXY (proxy));
+  token = oauth_proxy_get_token (proxy);
   g_assert_cmpstr (token, ==, "requestkey");
-  token_secret = oauth_proxy_get_token_secret (OAUTH_PROXY (proxy));
+  token_secret = oauth_proxy_get_token_secret (proxy);
   g_assert_cmpstr (token_secret, ==, "requestsecret");
 
   /* Second stage authentication, this gets an access token */
@@ -174,20 +157,15 @@ request_token_cb1 (GObject      *source_object,
                    GAsyncResult *result,
                    gpointer      user_data)
 {
-  OAuthProxyCall *call = OAUTH_PROXY_CALL (source_object);
+  OAuthProxy *proxy = OAUTH_PROXY (source_object);
   GMainLoop *loop = user_data;
-  OAuthProxy *proxy;
   GError *error = NULL;
 
-  rest_proxy_call_invoke_finish (REST_PROXY_CALL (call), result, &error);
+  oauth_proxy_request_token_finish (proxy, result, &error);
   g_assert_no_error (error);
 
-  g_object_get (G_OBJECT (call), "proxy", &proxy, NULL);
-
-  g_assert (OAUTH_IS_PROXY (proxy));
-
-  g_assert_cmpstr (oauth_proxy_get_token (OAUTH_PROXY (proxy)), ==, "requestkey");
-  g_assert_cmpstr (oauth_proxy_get_token_secret (OAUTH_PROXY (proxy)), ==, "requestsecret");
+  g_assert_cmpstr (oauth_proxy_get_token (proxy), ==, "requestkey");
+  g_assert_cmpstr (oauth_proxy_get_token_secret (proxy), ==, "requestsecret");
 
   g_main_loop_quit (loop);
 }


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