#include <stdio.h>
-#include <rest/rest/rest-proxy.h>
+#include <libsoup/soup.h>
#include <glib.h>
-#include <unistd.h>
-
-int main(int argc, char **argv) {
-
- RestProxy *proxy;
- RestProxyCall *call;
- const gchar *payload;
- const char *text;
- gssize len;
-
- g_thread_init(NULL);
- g_type_init();
-
- if (argc > 1) {
- text = argv[1];
- } else {
- text = "Hola Mundo";
- }
-
- proxy = rest_proxy_new(
- "http://ajax.googleapis.com/ajax/services/language/translate",
- FALSE);
- call = rest_proxy_new_call(proxy);
-perror("");
- rest_proxy_call_add_params(call,
- "v", "1.0",
- "q", text,
- "langpair","es|en",
- NULL);
- rest_proxy_call_run(call, NULL, NULL);
-
- payload = rest_proxy_call_get_payload(call);
- len = rest_proxy_call_get_payload_length(call);
- write(1, payload, len);
- printf("\n");
- g_object_unref(call);
- g_object_unref(proxy);
+
+#define URL_BASE "http://ajax.googleapis.com/ajax/services/\
+language/translate?v=%s&q=%s&langpair=%s"
+#define VERSION "1.0"
+#define LANGPAIR "es|en"
+
+static gchar*
+get_query_uri (const gchar *text)
+{
+ gchar *query = g_strdup_printf (URL_BASE, VERSION, text, LANGPAIR);
+
+ g_message ("%s\n", query);
+
+ return query;
+}
+
+static void process_response (SoupSession *session, SoupMessage *message,
+ gpointer user_data)
+{
+ if (!SOUP_STATUS_IS_SUCCESSFUL (message->status_code)
+ || message->response_body->length <= 0) {
+
+ g_print ("%s\n", message->reason_phrase);
+ } else {
+
+ const gchar *mime = soup_message_headers_get_content_type (
+ message->response_headers, NULL);
+
+ g_message ("Mime type: %s\n", mime);
+ g_print ("%s\n", message->response_body->data);
+ }
+
+ GMainLoop *loop = (GMainLoop *) user_data;
+
+ g_main_loop_quit (loop);
+}
+
+int main (int argc, char **argv)
+{
+ SoupSession *session = NULL;
+ SoupMessage *message = NULL;
+ GMainLoop *loop = NULL;
+ const gchar *text = NULL;
+ gchar *query = NULL;
+
+ g_thread_init (NULL);
+ g_type_init ();
+
+ loop = g_main_loop_new (NULL, FALSE);
+
+ if (argc > 1) {
+ text = argv[1];
+ } else {
+ text = "Hola Mundo";
+ }
+
+ query = get_query_uri (text);
+
+ session = soup_session_async_new ();
+ message = soup_message_new ("GET", query);
+
+ if (message)
+ soup_session_queue_message (session, message,
+ process_response,
+ loop);
+
+ g_main_loop_run (loop);
+
+ g_free (query);
+ g_main_loop_unref (loop);
+ g_object_unref (session);
}