From 18ab75eb99e9e581582d263545778b43f6e23666 Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Tue, 19 May 2009 17:52:03 +0100 Subject: Add Flickr test case --- .gitignore | 1 + examples/Makefile.am | 3 +- examples/test-flickr.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 examples/test-flickr.c diff --git a/.gitignore b/.gitignore index 4fb87c4..a18db60 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ examples/test-oauth examples/test-raw examples/test-xml examples/dump-xml +examples/test-flickr tests/oauth tests/proxy rest/test-runner diff --git a/examples/Makefile.am b/examples/Makefile.am index 1955524..c857ce2 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = test-raw test-xml test-oauth dump-xml +noinst_PROGRAMS = test-raw test-xml test-oauth dump-xml test-flickr AM_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) -I$(top_srcdir) AM_LDFLAGS = $(GLIB_LIBS) $(SOUP_LIBS) ../rest/librest.la @@ -7,3 +7,4 @@ test_raw_SOURCES = test-raw.c test_xml_SOURCES = test-xml.c test_oauth_SOURCES = test-oauth.c dump_xml_SOURCES = dump-xml.c +test_flickr_SOURCES = test-flickr.c diff --git a/examples/test-flickr.c b/examples/test-flickr.c new file mode 100644 index 0000000..63a4d6d --- /dev/null +++ b/examples/test-flickr.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +static RestXmlNode * +get_xml (RestProxyCall *call) +{ + static RestXmlParser *parser = NULL; + RestXmlNode *root; + + if (parser == NULL) + 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 (rest_xml_node_get_attr (root, "stat"), "ok") != 0) + g_error ("Error from Flickr"); + + g_object_unref (call); + + return root; +} + +int +main (int argc, char **argv) +{ + RestProxy *proxy; + RestProxyCall *call; + RestXmlNode *root, *node; + char *frob, *url, *token; + + g_thread_init (NULL); + g_type_init (); + + proxy = flickr_proxy_new ("cf4e02fc57240a9b07346ad26e291080", "cdfa2329cb206e50"); + + if (argc > 1) { + flickr_proxy_set_token (FLICKR_PROXY (proxy), argv[1]); + } else { + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "flickr.auth.getFrob"); + + if (!rest_proxy_call_run (call, NULL, 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); + + url = flickr_proxy_build_login_url (FLICKR_PROXY (proxy), frob); + + g_print ("Login URL %s\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)) + g_error ("Cannot get token"); + + root = get_xml (call); + token = g_strdup (rest_xml_node_find (root, "token")->content); + g_print ("Got token %s\n", token); + + flickr_proxy_set_token (FLICKR_PROXY (proxy), token); + } + + /* Make an authenticated call */ + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "flickr.auth.checkToken"); + + if (!rest_proxy_call_run (call, NULL, 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")); + + return 0; +} -- cgit v1.2.3