diff options
author | Timm Bäder <mail@baedert.org> | 2016-06-17 17:44:15 +0200 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2016-06-20 12:39:44 +0200 |
commit | bb67d53e061277bc9a83acd9a52ed35d458ca391 (patch) | |
tree | 3441a2a31b41774fc5459f968e3cab516faa4fa4 /rest | |
parent | a4add5c205bb903c05aa6487251e47debfb8cd54 (diff) | |
download | librest-bb67d53e061277bc9a83acd9a52ed35d458ca391.tar.gz |
RestProxyCall: Use GTask
Diffstat (limited to 'rest')
-rw-r--r-- | rest/oauth-proxy.c | 73 | ||||
-rw-r--r-- | rest/rest-proxy-call.c | 180 | ||||
-rw-r--r-- | rest/rest-proxy-call.h | 10 | ||||
-rw-r--r-- | rest/rest-proxy.c | 2 |
4 files changed, 37 insertions, 228 deletions
diff --git a/rest/oauth-proxy.c b/rest/oauth-proxy.c index 20a3308..cd76a41 100644 --- a/rest/oauth-proxy.c +++ b/rest/oauth-proxy.c @@ -311,10 +311,11 @@ oauth_proxy_request_token (OAuthProxy *proxy, if (callback_uri) rest_proxy_call_add_param (call, "oauth_callback", callback_uri); - if (!rest_proxy_call_run (call, NULL, error)) { - g_object_unref (call); - return FALSE; - } + if (!rest_proxy_call_sync (call, error)) + { + g_object_unref (call); + return FALSE; + } /* TODO: sanity check response */ oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call)); @@ -325,26 +326,24 @@ oauth_proxy_request_token (OAuthProxy *proxy, } static void -request_token_cb (RestProxyCall *call, - const GError *error, - GObject *weak_object, - gpointer user_data) +request_token_cb (GObject *source_object, + GAsyncResult *result, + gpointer user_data) { - OAuthProxy *proxy = NULL; GTask *task = G_TASK (user_data); + RestProxyCall *call = REST_PROXY_CALL (source_object); + GError *error = NULL; + gboolean call_status; - g_object_get (call, "proxy", &proxy, NULL); - g_assert (proxy); + call_status = rest_proxy_call_invoke_finish (call, result, &error); if (error != NULL) { - g_task_return_error (task, g_error_copy (error)); + g_task_return_error (task, error); } else { oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call)); - g_task_return_boolean (task, TRUE); + g_task_return_boolean (task, call_status); } - g_object_unref (call); - g_object_unref (proxy); g_object_unref (task); } @@ -379,7 +378,6 @@ oauth_proxy_request_token_async (OAuthProxy *proxy, { RestProxyCall *call; GTask *task; - GError *error = NULL; call = rest_proxy_new_call (REST_PROXY (proxy)); rest_proxy_call_set_function (call, function ? function : "request_token"); @@ -390,10 +388,9 @@ oauth_proxy_request_token_async (OAuthProxy *proxy, task = g_task_new (proxy, cancellable, callback, user_data); - rest_proxy_call_async (call, request_token_cb, NULL, task, &error); - if (error != NULL) { - g_task_return_error (task, error); - } + rest_proxy_call_invoke_async (call, cancellable, request_token_cb, task); + + g_object_unref (call); } gboolean @@ -439,10 +436,11 @@ oauth_proxy_access_token (OAuthProxy *proxy, if (verifier) rest_proxy_call_add_param (call, "oauth_verifier", verifier); - if (!rest_proxy_call_run (call, NULL, error)) { - g_object_unref (call); - return FALSE; - } + if (!rest_proxy_call_sync (call, error)) + { + g_object_unref (call); + return FALSE; + } /* TODO: sanity check response */ oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call)); @@ -453,26 +451,24 @@ oauth_proxy_access_token (OAuthProxy *proxy, } static void -access_token_cb (RestProxyCall *call, - const GError *error, - GObject *weak_object, - gpointer user_data) +access_token_cb (GObject *source_object, + GAsyncResult *result, + gpointer user_data) { - OAuthProxy *proxy = NULL; GTask *task = G_TASK (user_data); + RestProxyCall *call = REST_PROXY_CALL (source_object); + GError *error = NULL; + gboolean call_status; - g_object_get (call, "proxy", &proxy, NULL); - g_assert (proxy); + call_status = rest_proxy_call_invoke_finish (call, result, &error); if (error != NULL) { - g_task_return_error (task, g_error_copy (error)); + g_task_return_error (task, error); } else { oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call)); - g_task_return_boolean (task, TRUE); + g_task_return_boolean (task, call_status); } - g_object_unref (call); - g_object_unref (proxy); g_object_unref (task); } @@ -510,7 +506,6 @@ oauth_proxy_access_token_async (OAuthProxy *proxy, { RestProxyCall *call; GTask *task; - GError *error = NULL; call = rest_proxy_new_call (REST_PROXY (proxy)); rest_proxy_call_set_function (call, function ? function : "access_token"); @@ -521,10 +516,8 @@ oauth_proxy_access_token_async (OAuthProxy *proxy, 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); - } + rest_proxy_call_invoke_async (call, cancellable, access_token_cb, task); + g_object_unref (call); } gboolean diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c index ed81040..09584eb 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -581,10 +581,6 @@ rest_proxy_call_get_params (RestProxyCall *call) static void _call_async_weak_notify_cb (gpointer *data, GObject *dead_object); -static void _call_message_completed_cb (SoupSession *session, - SoupMessage *message, - gpointer userdata); - static void _populate_headers_hash_table (const gchar *name, const gchar *value, @@ -670,43 +666,6 @@ finish_call (RestProxyCall *call, SoupMessage *message, GError **error) } static void -_call_message_completed_cb (SoupSession *session, - SoupMessage *message, - gpointer userdata) -{ - RestProxyCallAsyncClosure *closure; - RestProxyCall *call; - RestProxyCallPrivate *priv; - GError *error = NULL; - - closure = (RestProxyCallAsyncClosure *)userdata; - call = closure->call; - priv = GET_PRIVATE (call); - - finish_call (call, message, &error); - - closure->callback (closure->call, - error, - closure->weak_object, - closure->userdata); - - g_clear_error (&error); - - /* Success. We don't need the weak reference any more */ - if (closure->weak_object) - { - g_object_weak_unref (closure->weak_object, - (GWeakNotify)_call_async_weak_notify_cb, - closure); - } - - priv->cur_call_closure = NULL; - g_object_unref (closure->call); - g_slice_free (RestProxyCallAsyncClosure, closure); -} - - -static void _continuous_call_message_completed_cb (SoupSession *session, SoupMessage *message, gpointer userdata) @@ -938,72 +897,6 @@ prepare_message (RestProxyCall *call, GError **error_out) return message; } -/** - * rest_proxy_call_async: (skip) - * @call: The #RestProxyCall - * @callback: a #RestProxyCallAsyncCallback to invoke on completion of the call - * @weak_object: The #GObject to weakly reference and tie the lifecycle too - * @userdata: data to pass to @callback - * @error: a #GError, or %NULL - * - * Asynchronously invoke @call. - * - * When the call has finished, @callback will be called. If @weak_object is - * disposed during the call then this call will be cancelled. If the call is - * cancelled then the callback will be invoked with an error state. - * - * You may unref the call after calling this function since there is an - * internal reference, or you may unref in the callback. - */ -gboolean -rest_proxy_call_async (RestProxyCall *call, - RestProxyCallAsyncCallback callback, - GObject *weak_object, - gpointer userdata, - GError **error) -{ - RestProxyCallPrivate *priv; - SoupMessage *message; - RestProxyCallAsyncClosure *closure; - - g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE); - priv = GET_PRIVATE (call); - g_assert (priv->proxy); - - if (priv->cur_call_closure) - { - g_warning (G_STRLOC ": re-use of RestProxyCall %p, don't do this", call); - return FALSE; - } - - message = prepare_message (call, error); - if (message == NULL) - return FALSE; - - closure = g_slice_new0 (RestProxyCallAsyncClosure); - closure->call = g_object_ref (call); - closure->callback = callback; - closure->weak_object = weak_object; - closure->message = message; - closure->userdata = userdata; - - priv->cur_call_closure = closure; - - /* Weakly reference this object. We remove our callback if it goes away. */ - if (closure->weak_object) - { - g_object_weak_ref (closure->weak_object, - (GWeakNotify)_call_async_weak_notify_cb, - closure); - } - - _rest_proxy_queue_message (priv->proxy, - message, - _call_message_completed_cb, - closure); - return TRUE; -} - static void _call_message_call_cancelled_cb (GCancellable *cancellable, RestProxyCall *call) @@ -1029,7 +922,6 @@ _call_message_call_completed_cb (SoupSession *session, else g_task_return_boolean (task, TRUE); - g_object_unref (call); g_object_unref (task); } @@ -1040,8 +932,6 @@ _call_message_call_completed_cb (SoupSession *session, * cancel the call, or %NULL * @callback: (scope async): callback to call when the async call is finished * @user_data: (closure): user data for the callback - * - * A GIO-style version of rest_proxy_call_async(). */ void rest_proxy_call_invoke_async (RestProxyCall *call, @@ -1049,13 +939,13 @@ rest_proxy_call_invoke_async (RestProxyCall *call, GAsyncReadyCallback callback, gpointer user_data) { - RestProxyCallPrivate *priv = rest_proxy_call_get_instance_private (call); + RestProxyCallPrivate *priv = GET_PRIVATE (call); GTask *task; SoupMessage *message; GError *error = NULL; g_return_if_fail (REST_IS_PROXY_CALL (call)); - g_return_if_fail (callback == NULL || G_IS_CANCELLABLE (cancellable)); + g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); g_assert (priv->proxy); message = prepare_message (call, &error); @@ -1092,8 +982,6 @@ rest_proxy_call_invoke_finish (RestProxyCall *call, GAsyncResult *result, GError **error) { - - g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE); g_return_val_if_fail (g_task_is_valid (result, call), FALSE); @@ -1127,7 +1015,7 @@ _continuous_call_message_got_chunk_cb (SoupMessage *msg, * rest_proxy_call_get_payload() * * When there is data @callback will be called and when the connection is - * closed or the stream ends @callback will also be called. + * closed or the stream ends @callback will also be called. * * If @weak_object is disposed during the call then this call will be * cancelled. If the call is cancelled then the callback will be invoked with @@ -1370,68 +1258,6 @@ typedef struct GError *error; } RestProxyCallRunClosure; -static void -_rest_proxy_call_async_cb (RestProxyCall *call, - const GError *error, - GObject *weak_object, - gpointer userdata) -{ - RestProxyCallRunClosure *closure = (RestProxyCallRunClosure *)userdata; - - /* *duplicate* not propagate the error */ - if (error) - closure->error = g_error_copy (error); - - g_main_loop_quit (closure->loop); -} - -gboolean -rest_proxy_call_run (RestProxyCall *call, - GMainLoop **loop_out, - GError **error_out) -{ - gboolean res = TRUE; - GError *error = NULL; - RestProxyCallRunClosure closure = { NULL, NULL}; - - g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE); - - closure.loop = g_main_loop_new (NULL, FALSE); - - if (loop_out) - *loop_out = closure.loop; - - res = rest_proxy_call_async (call, - _rest_proxy_call_async_cb, - NULL, - &closure, - &error); - - if (!res) - { - g_propagate_error (error_out, error); - goto error; - } - - g_main_loop_run (closure.loop); - - if (closure.error) - { - /* If the caller has asked for the error then propagate else free it */ - if (error_out) - { - g_propagate_error (error_out, closure.error); - } else { - g_clear_error (&(closure.error)); - } - res = FALSE; - } - -error: - g_main_loop_unref (closure.loop); - return res; -} - gboolean rest_proxy_call_sync (RestProxyCall *call, GError **error_out) diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h index d78bb59..9671664 100644 --- a/rest/rest-proxy-call.h +++ b/rest/rest-proxy-call.h @@ -150,21 +150,11 @@ void rest_proxy_call_remove_param (RestProxyCall *call, RestParams *rest_proxy_call_get_params (RestProxyCall *call); -gboolean rest_proxy_call_run (RestProxyCall *call, - GMainLoop **loop, - GError **error); - typedef void (*RestProxyCallAsyncCallback)(RestProxyCall *call, const GError *error, GObject *weak_object, gpointer userdata); -gboolean rest_proxy_call_async (RestProxyCall *call, - RestProxyCallAsyncCallback callback, - GObject *weak_object, - gpointer userdata, - GError **error); - void rest_proxy_call_invoke_async (RestProxyCall *call, GCancellable *cancellable, GAsyncReadyCallback callback, diff --git a/rest/rest-proxy.c b/rest/rest-proxy.c index 81d487d..2a6b08c 100644 --- a/rest/rest-proxy.c +++ b/rest/rest-proxy.c @@ -676,7 +676,7 @@ _rest_proxy_simple_run_valist (RestProxy *proxy, rest_proxy_call_add_params_from_valist (call, params); - ret = rest_proxy_call_run (call, NULL, error); + ret = rest_proxy_call_sync (call, error); if (ret) { *payload = g_strdup (rest_proxy_call_get_payload (call)); if (len) *len = rest_proxy_call_get_payload_length (call); |