diff options
author | Ross Burton <ross@linux.intel.com> | 2010-05-14 15:27:21 +0100 |
---|---|---|
committer | Ross Burton <ross@linux.intel.com> | 2010-05-14 15:27:35 +0100 |
commit | 15b49f963f3431054bf47b6201f2836bccd2a271 (patch) | |
tree | b030d45d914dea3dd14f363a275e3a703652cea5 /examples | |
parent | f1e2ac78eaa187d1ceabbb70883b2b9458580877 (diff) | |
download | librest-15b49f963f3431054bf47b6201f2836bccd2a271.tar.gz |
examples: add an example to sent a user a shout on Last.fm
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 3 | ||||
-rw-r--r-- | examples/lastfm-shout.c | 146 |
2 files changed, 148 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index e26dbce..67d52e9 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 +noinst_PROGRAMS = test-raw test-xml dump-xml get-fireeagle-location post-twitter get-flickr-favorites lastfm-shout AM_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) -I$(top_srcdir) AM_LDFLAGS = $(GLIB_LIBS) $(SOUP_LIBS) ../rest/librest-@API_VERSION@.la ../rest-extras/librest-extras-@API_VERSION@.la @@ -9,3 +9,4 @@ get_fireeagle_location_SOURCES = get-fireeagle-location.c dump_xml_SOURCES = dump-xml.c post_twitter_SOURCES = post-twitter.c get_flickr_favorites_SOURCES = get-flickr-favorites.c +lastfm_shout_SOURCES = lastfm-shout.c diff --git a/examples/lastfm-shout.c b/examples/lastfm-shout.c new file mode 100644 index 0000000..471b045 --- /dev/null +++ b/examples/lastfm-shout.c @@ -0,0 +1,146 @@ +/* + * librest - RESTful web services access + * Copyright (c) 2010 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 <config.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <rest-extras/lastfm-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) +{ + RestXmlParser *parser; + RestXmlNode *root; + GError *error = 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 (!lastfm_proxy_is_successful (root, &error)) + g_error (error->message); + + g_object_unref (call); + g_object_unref (parser); + + return root; +} + +static void +shout (RestProxy *proxy, const char *username, const char *message) +{ + RestProxyCall *call; + RestXmlNode *root, *node; + GError *error = NULL; + + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_method (call, "POST"); + rest_proxy_call_set_function (call, "user.shout"); + rest_proxy_call_add_param (call, "user", username); + rest_proxy_call_add_param (call, "message", message); + + if (!rest_proxy_call_sync (call, &error)) + g_error ("Cannot shout: %s", error->message); + + rest_xml_node_unref (get_xml (call)); +} + +int +main (int argc, char **argv) +{ + GError *error = NULL; + GOptionContext *context; + RestProxy *proxy; + RestProxyCall *call; + RestXmlNode *root; + char *token, *url, *userid = NULL, *session = NULL;; + GOptionEntry entries[] = { + { "session", 's', 0, G_OPTION_ARG_STRING, &session, "Session key (optional)", "KEY" }, + { "user", 'u', 0, G_OPTION_ARG_STRING, &userid, "User to send a message to", "USERNAME" }, + { NULL } + }; + + g_thread_init (NULL); + g_type_init (); + + context = g_option_context_new ("- send a shout to a Last.fm user"); + g_option_context_add_main_entries (context, entries, NULL); + if (!g_option_context_parse (context, &argc, &argv, &error)) { + g_print ("Option parsing failed: %s\n", error->message); + return 1; + } + + if (userid == NULL) { + g_print ("Need a user ID to send a shout out to\n\n"); + g_print (g_option_context_get_help (context, TRUE, NULL)); + return 1; + } + + proxy = lastfm_proxy_new ("aa581f6505fd3ea79073ddcc2215cbc7", + "7db227a36b3154e3a3306a23754de1d7"); + + if (session) { + lastfm_proxy_set_session_key (LASTFM_PROXY (proxy), argv[1]); + } else { + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "auth.getToken"); + + 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); + g_debug ("got token %s", token); + rest_xml_node_unref (root); + + url = lastfm_proxy_build_login_url (LASTFM_PROXY (proxy), token); + 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, "auth.getSession"); + rest_proxy_call_add_param (call, "token", token); + if (!rest_proxy_call_sync (call, NULL)) + g_error ("Cannot get session"); + + root = get_xml (call); + session = rest_xml_node_find (root, "key")->content; + g_print ("Got session key %s\n", session); + g_print ("Got username %s\n", rest_xml_node_find (root, "name")->content); + lastfm_proxy_set_session_key (LASTFM_PROXY (proxy), session); + rest_xml_node_unref (root); + } + + shout (proxy, userid, "Hello from librest!"); + + g_object_unref (proxy); + + return 0; +} |