[librest/goa: 3/4] examples: add Tweet example using GOA



commit 7ae13273281ec619992373e87dc5835ca2b1b67c
Author: Ross Burton <ross linux intel com>
Date:   Fri Jan 13 12:38:58 2012 +0000

    examples: add Tweet example using GOA

 examples/Makefile.am |    6 ++-
 examples/goa-tweet.c |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 112 insertions(+), 1 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index a58a054..945b00b 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-noinst_PROGRAMS = test-raw test-xml dump-xml get-fireeagle-location post-twitter get-flickr-favorites lastfm-shout continuous-twitter
+noinst_PROGRAMS = test-raw test-xml dump-xml get-fireeagle-location post-twitter get-flickr-favorites lastfm-shout continuous-twitter goa-tweet
 
 AM_CFLAGS = $(GLIB_CFLAGS) $(GTHREAD_CFLAGS) $(SOUP_CFLAGS) -I$(top_srcdir)
 AM_LDFLAGS = $(GLIB_LIBS) $(GTHREAD_LIBS) $(SOUP_LIBS) ../rest/librest- API_VERSION@.la ../rest-extras/librest-extras- API_VERSION@.la
@@ -11,3 +11,7 @@ post_twitter_SOURCES = post-twitter.c
 get_flickr_favorites_SOURCES = get-flickr-favorites.c
 lastfm_shout_SOURCES = lastfm-shout.c
 continuous_twitter_SOURCES = continuous-twitter.c
+
+goa_tweet_SOURCES = goa-tweet.c
+goa_tweet_CFLAGS = $(AM_CFLAGS) $(GOA_CFLAGS)
+goa_tweet_LDFLAGS = $(AM_LDFLAGS) $(GOA_LIBS)
diff --git a/examples/goa-tweet.c b/examples/goa-tweet.c
new file mode 100644
index 0000000..b024f58
--- /dev/null
+++ b/examples/goa-tweet.c
@@ -0,0 +1,107 @@
+/*
+ * librest - RESTful web services access
+ * Copyright (c) 2008, 2009, Intel Corporation.
+ *
+ * Authors: Rob Bradford <rob linux intel com>
+ *          Ross Burton <ross linux intel com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <rest-extras/rest-goa.h>
+#include <goa/goa.h>
+
+static GoaObject *
+find_twitter (void)
+{
+  GError *error = NULL;
+  GoaClient *client;
+  GList *accounts, *l;
+  GoaObject *found = NULL;
+
+  client = goa_client_new_sync (NULL, &error);
+  if (!client) {
+    g_error ("Could not create GoaClient: %s", error->message);
+    return NULL;
+  }
+
+  accounts = goa_client_get_accounts (client);
+
+  for (l = accounts; l != NULL; l = l->next) {
+    GoaObject *object = GOA_OBJECT (l->data);
+    GoaAccount *account;
+
+    account = goa_object_peek_account (object);
+    if (g_strcmp0 (goa_account_get_provider_type (account), "twitter") == 0) {
+      /* Incremenet reference count for our returned object */
+      found = g_object_ref (object);
+      break;
+    }
+  }
+
+  g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
+
+  return found;
+}
+
+int
+main (int argc, char **argv)
+{
+  GoaObject *twitter;
+  RestProxy *proxy;
+  RestProxyCall *call;
+  GError *error = NULL;
+  gboolean authenticated = FALSE;
+
+  g_thread_init (NULL);
+  g_type_init ();
+
+  if (argc != 2) {
+    g_printerr ("$ post-twitter \"message\"\n");
+    return 1;
+  }
+
+  twitter = find_twitter ();
+  if (twitter == NULL) {
+    g_print ("Cannot find a Twitter account\n");
+    return 1;
+  }
+
+  proxy = rest_goa_proxy_from_account (twitter,
+                                       "https://api.twitter.com/";, FALSE,
+                                       &authenticated);
+
+  if (!authenticated) {
+    g_print ("Found Twitter account but not authenticated\n");
+    return 1;
+  }
+
+  /* Post the status message */
+  call = rest_proxy_new_call (proxy);
+  rest_proxy_call_set_function (call, "1/statuses/update.xml");
+  rest_proxy_call_set_method (call, "POST");
+  rest_proxy_call_add_param (call, "status", argv[1]);
+
+  if (!rest_proxy_call_sync (call, &error))
+    g_error ("Cannot make call: %s", error->message);
+
+  /* TODO: parse the XML and print something useful */
+  g_print ("%s\n", rest_proxy_call_get_payload (call));
+
+  g_object_unref (call);
+  g_object_unref (proxy);
+
+  return 0;
+}



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