summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoss Burton <ross@linux.intel.com>2009-08-04 09:14:23 +0100
committerRoss Burton <ross@linux.intel.com>2009-08-04 09:20:48 +0100
commit0aa3862319b348f98ed584623cebef47c512d2c3 (patch)
tree938aa12fea34bcdf8f777dc4c364022debfcfec2 /examples
parent15cc3e4cfabb6aba8e68b0f5bdff01ee553eeb09 (diff)
downloadlibrest-0aa3862319b348f98ed584623cebef47c512d2c3.tar.gz
Add better Flickr example
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am4
-rw-r--r--examples/get-flickr-favorites.c (renamed from examples/test-flickr.c)103
2 files changed, 78 insertions, 29 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index f714b01..ce728f5 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-noinst_PROGRAMS = test-raw test-xml dump-xml test-flickr test-facebook get-fireeagle-location post-twitter
+noinst_PROGRAMS = test-raw test-xml dump-xml test-facebook get-fireeagle-location post-twitter get-flickr-favorites
AM_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) -I$(top_srcdir)
AM_LDFLAGS = $(GLIB_LIBS) $(SOUP_LIBS) ../rest/librest.la
@@ -7,6 +7,6 @@ test_raw_SOURCES = test-raw.c
test_xml_SOURCES = test-xml.c
get_fireeagle_location_SOURCES = get-fireeagle-location.c
dump_xml_SOURCES = dump-xml.c
-test_flickr_SOURCES = test-flickr.c
test_facebook_SOURCES = test-facebook.c
post_twitter_SOURCES = post-twitter.c
+get_flickr_favorites_SOURCES = get-flickr-favorites.c
diff --git a/examples/test-flickr.c b/examples/get-flickr-favorites.c
index 63a4d6d..0cce1b0 100644
--- a/examples/test-flickr.c
+++ b/examples/get-flickr-favorites.c
@@ -1,39 +1,97 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <rest/flickr-proxy.h>
#include <rest/rest-xml-parser.h>
+/*
+ * Parse the payload and either return a RestXmlNode, or error. As a side-effect
+ * the call is unreffed.
+ */
static RestXmlNode *
get_xml (RestProxyCall *call)
{
- static RestXmlParser *parser = NULL;
+ RestXmlParser *parser;
RestXmlNode *root;
- if (parser == NULL)
- parser = rest_xml_parser_new ();
+ parser = rest_xml_parser_new ();
root = rest_xml_parser_parse_from_data (parser,
rest_proxy_call_get_payload (call),
rest_proxy_call_get_payload_length (call));
- if (strcmp (root->name, "rsp") != 0)
- g_error ("Unexpected response from Flickr");
+ if (strcmp (root->name, "rsp") != 0) {
+ g_error ("Unexpected response from Flickr:\n%s",
+ rest_proxy_call_get_payload (call));
+ }
- if (strcmp (rest_xml_node_get_attr (root, "stat"), "ok") != 0)
- g_error ("Error from Flickr");
+ if (strcmp (rest_xml_node_get_attr (root, "stat"), "ok") != 0) {
+ root = rest_xml_node_find (root, "err");
+ g_error ("Error %s from Flickr: %s",
+ rest_xml_node_get_attr (root, "code"),
+ rest_xml_node_get_attr (root, "msg"));
+ }
g_object_unref (call);
+ g_object_unref (parser);
+
return root;
}
+static void
+print_user_name (RestProxy *proxy)
+{
+ RestProxyCall *call;
+ RestXmlNode *root, *node;
+
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "flickr.auth.checkToken");
+
+ if (!rest_proxy_call_sync (call, NULL))
+ g_error ("Cannot check token");
+
+ root = get_xml (call);
+ node = rest_xml_node_find (root, "user");
+ g_print ("Logged in as %s\n",
+ rest_xml_node_get_attr (node, "fullname")
+ ?: rest_xml_node_get_attr (node, "username"));
+ rest_xml_node_unref (root);
+}
+
+static void
+print_favourites (RestProxy *proxy)
+{
+ RestProxyCall *call;
+ RestXmlNode *root, *node;
+
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "flickr.favorites.getList");
+ rest_proxy_call_add_param (call, "extras", "owner_name");
+ rest_proxy_call_add_param (call, "per_page", "10");
+
+ if (!rest_proxy_call_sync (call, NULL))
+ g_error ("Cannot get favourites");
+
+ root = get_xml (call);
+
+ for (node = rest_xml_node_find (root, "photo"); node; node = node->next) {
+ g_print ("%s by %s\n",
+ rest_xml_node_get_attr (node, "title"),
+ rest_xml_node_get_attr (node, "ownername"));
+ }
+
+ rest_xml_node_unref (root);
+}
+
int
main (int argc, char **argv)
{
RestProxy *proxy;
RestProxyCall *call;
- RestXmlNode *root, *node;
- char *frob, *url, *token;
+ RestXmlNode *root;
+ char *frob, *url;
+ const char *token;
g_thread_init (NULL);
g_type_init ();
@@ -46,45 +104,36 @@ main (int argc, char **argv)
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_function (call, "flickr.auth.getFrob");
- if (!rest_proxy_call_run (call, NULL, NULL))
+ if (!rest_proxy_call_sync (call, NULL))
g_error ("Cannot get frob");
root = get_xml (call);
frob = g_strdup (rest_xml_node_find (root, "frob")->content);
- g_print ("got frob %s\n", frob);
+ rest_xml_node_unref (root);
url = flickr_proxy_build_login_url (FLICKR_PROXY (proxy), frob);
-
- g_print ("Login URL %s\n", url);
-
+ g_print ("Go to %s to authorise me and then press any key.\n", url);
getchar ();
call = rest_proxy_new_call (proxy);
rest_proxy_call_set_function (call, "flickr.auth.getToken");
rest_proxy_call_add_param (call, "frob", frob);
- if (!rest_proxy_call_run (call, NULL, NULL))
+ if (!rest_proxy_call_sync (call, NULL))
g_error ("Cannot get token");
root = get_xml (call);
- token = g_strdup (rest_xml_node_find (root, "token")->content);
+ token = rest_xml_node_find (root, "token")->content;
g_print ("Got token %s\n", token);
-
flickr_proxy_set_token (FLICKR_PROXY (proxy), token);
+ rest_xml_node_unref (root);
}
- /* Make an authenticated call */
- call = rest_proxy_new_call (proxy);
- rest_proxy_call_set_function (call, "flickr.auth.checkToken");
+ print_user_name (proxy);
- if (!rest_proxy_call_run (call, NULL, NULL))
- g_error ("Cannot check token");
+ print_favourites (proxy);
- root = get_xml (call);
- node = rest_xml_node_find (root, "user");
- g_print ("Logged in as %s\n",
- rest_xml_node_get_attr (node, "fullname")
- ?: rest_xml_node_get_attr (node, "username"));
+ g_object_unref (proxy);
return 0;
}