Debian Codegraph @

rest-extras/lastfm-proxy.c

raw
1 /*
2 * librest - RESTful web services access
3 * Copyright (c) 2010 Intel Corporation.
4 *
5 * Authors: Rob Bradford <[email protected]>
6 * Ross Burton <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU Lesser General Public License,
10 * version 2.1, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <rest/rest-proxy.h>
27 #include <libsoup/soup.h>
28 #include "lastfm-proxy.h"
29 #include "lastfm-proxy-call.h"
30
31 typedef struct {
32 char *api_key;
33 char *secret;
34 char *session_key;
35 } LastfmProxyPrivate;
36
37 G_DEFINE_TYPE_WITH_PRIVATE (LastfmProxy, lastfm_proxy, REST_TYPE_PROXY)
38
39 enum {
40 PROP_0,
41 PROP_API_KEY,
42 PROP_SECRET,
43 PROP_SESSION_KEY,
44 N_PROPS,
45 };
46
47 static GParamSpec *properties [N_PROPS];
48
49 G_DEFINE_QUARK (rest-lastfm-proxy-error-quark, lastfm_proxy_error)
50
51 static RestProxyCall *
52 _new_call (RestProxy *proxy)
53 {
54 RestProxyCall *call;
55
56 call = g_object_new (LASTFM_TYPE_PROXY_CALL,
57 "proxy", proxy,
58 NULL);
59
60 return call;
61 }
62
63 static void
64 lastfm_proxy_get_property (GObject *object,
65 guint property_id,
66 GValue *value,
67 GParamSpec *pspec)
68 {
69 LastfmProxy *self = LASTFM_PROXY (object);
70 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
71
72 switch (property_id) {
73 case PROP_API_KEY:
74 g_value_set_string (value, priv->api_key);
75 break;
76 case PROP_SECRET:
77 g_value_set_string (value, priv->secret);
78 break;
79 case PROP_SESSION_KEY:
80 g_value_set_string (value, priv->session_key);
81 break;
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84 }
85 }
86
87 static void
88 lastfm_proxy_set_property (GObject *object, guint property_id,
89 const GValue *value, GParamSpec *pspec)
90 {
91 LastfmProxy *self = LASTFM_PROXY (object);
92 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
93
94 switch (property_id) {
95 case PROP_API_KEY:
96 if (priv->api_key)
97 g_free (priv->api_key);
98 priv->api_key = g_value_dup_string (value);
99 break;
100 case PROP_SECRET:
101 if (priv->secret)
102 g_free (priv->secret);
103 priv->secret = g_value_dup_string (value);
104 break;
105 case PROP_SESSION_KEY:
106 if (priv->session_key)
107 g_free (priv->session_key);
108 priv->session_key = g_value_dup_string (value);
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 }
113 }
114
115 static void
116 lastfm_proxy_finalize (GObject *object)
117 {
118 LastfmProxy *self = LASTFM_PROXY (object);
119 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
120
121 g_free (priv->api_key);
122 g_free (priv->secret);
123 g_free (priv->session_key);
124
125 G_OBJECT_CLASS (lastfm_proxy_parent_class)->finalize (object);
126 }
127
128 static void
129 lastfm_proxy_class_init (LastfmProxyClass *klass)
130 {
131 GObjectClass *object_class = G_OBJECT_CLASS (klass);
132 RestProxyClass *proxy_class = REST_PROXY_CLASS (klass);
133
134 object_class->get_property = lastfm_proxy_get_property;
135 object_class->set_property = lastfm_proxy_set_property;
136 object_class->finalize = lastfm_proxy_finalize;
137
138 proxy_class->new_call = _new_call;
139
140 properties [PROP_API_KEY] =
141 g_param_spec_string ("api-key",
142 "api-key",
143 "The API key",
144 NULL,
145 (G_PARAM_READWRITE |
146 G_PARAM_CONSTRUCT_ONLY |
147 G_PARAM_STATIC_STRINGS));
148
149 properties [PROP_SECRET] =
150 g_param_spec_string ("secret",
151 "secret",
152 "The API key secret",
153 NULL,
154 (G_PARAM_READWRITE |
155 G_PARAM_CONSTRUCT_ONLY |
156 G_PARAM_STATIC_STRINGS));
157
158 properties [PROP_SESSION_KEY] =
159 g_param_spec_string ("session-key",
160 "session-key",
161 "The session key",
162 NULL,
163 (G_PARAM_READWRITE |
164 G_PARAM_STATIC_STRINGS));
165
166 g_object_class_install_properties (object_class, N_PROPS, properties);
167 }
168
169 static void
170 lastfm_proxy_init (LastfmProxy *self)
171 {
172 }
173
174 RestProxy *
175 lastfm_proxy_new (const char *api_key,
176 const char *secret)
177 {
178 return lastfm_proxy_new_with_session (api_key,
179 secret,
180 NULL);
181 }
182
183 RestProxy *
184 lastfm_proxy_new_with_session (const char *api_key,
185 const char *secret,
186 const char *session_key)
187 {
188 return g_object_new (LASTFM_TYPE_PROXY,
189 "api-key", api_key,
190 "secret", secret,
191 "session-key", session_key,
192 "url-format", "http://ws.audioscrobbler.com/2.0/",
193 "binding-required", FALSE,
194 NULL);
195 }
196
197 /**
198 * lastfm_proxy_get_api_key:
199 * @proxy: an #LastfmProxy
200 *
201 * Get the API key.
202 *
203 * Returns: the API key. This string is owned by #LastfmProxy and should not be
204 * freed.
205 */
206 const char *
207 lastfm_proxy_get_api_key (LastfmProxy *self)
208 {
209 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
210
211 g_return_val_if_fail (LASTFM_IS_PROXY (self), NULL);
212
213 return priv->api_key;
214 }
215
216 /**
217 * lastfm_proxy_get_secret:
218 * @proxy: an #LastfmProxy
219 *
220 * Get the secret for authentication.
221 *
222 * Returns: the secret. This string is owned by #LastfmProxy and should not be
223 * freed.
224 */
225 const char *
226 lastfm_proxy_get_secret (LastfmProxy *self)
227 {
228 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
229
230 g_return_val_if_fail (LASTFM_IS_PROXY (self), NULL);
231
232 return priv->secret;
233 }
234
235 /**
236 * lastfm_proxy_get_session_key:
237 * @proxy: an #LastfmProxy
238 *
239 * Get the current session key.
240 *
241 * Returns: (nullable): the session key, or %NULL if there is no session key yet.
242 * This string is owned by #LastfmProxy and should not be freed.
243 */
244 const char *
245 lastfm_proxy_get_session_key (LastfmProxy *self)
246 {
247 LastfmProxyPrivate *priv = lastfm_proxy_get_instance_private (self);
248
249 g_return_val_if_fail (LASTFM_IS_PROXY (self), NULL);
250
251 return priv->session_key;
252 }
253
254 /**
255 * lastfm_proxy_set_session_key:
256 * @proxy: an #LastfmProxy
257 * @session_key: the access session_key
258 *
259 * Set the session key.
260 */
261 void
262 lastfm_proxy_set_session_key (LastfmProxy *self,
263 const char *session_key)
264 {
265 LastfmProxyPrivate *priv;
266
267 g_return_if_fail (LASTFM_IS_PROXY (self));
268
269 priv = lastfm_proxy_get_instance_private (self);
270
271 if (priv->session_key)
272 g_free (priv->session_key);
273
274 priv->session_key = g_strdup (session_key);
275 }
276
277 /**
278 * lastfm_proxy_sign:
279 * @proxy: an #LastfmProxy
280 * @params: (element-type utf8 utf8): the request parameters
281 *
282 * Get the md5 checksum of the request.
283 *
284 * Returns: The md5 checksum of the request
285 */
286 char *
287 lastfm_proxy_sign (LastfmProxy *self,
288 GHashTable *params)
289 {
290 LastfmProxyPrivate *priv;
291 GString *s;
292 GList *keys;
293 char *md5;
294
295 g_return_val_if_fail (LASTFM_IS_PROXY (self), NULL);
296 g_return_val_if_fail (params, NULL);
297
298 priv = lastfm_proxy_get_instance_private (self);
299
300 s = g_string_new (NULL);
301
302 keys = g_hash_table_get_keys (params);
303 keys = g_list_sort (keys, (GCompareFunc)strcmp);
304
305 while (keys) {
306 const char *key;
307 const char *value;
308
309 key = keys->data;
310 value = g_hash_table_lookup (params, key);
311
312 g_string_append_printf (s, "%s%s", key, value);
313
314 keys = g_list_delete_link (keys, keys);
315 }
316
317 g_string_append (s, priv->secret);
318
319 md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, s->str, s->len);
320
321 g_string_free (s, TRUE);
322
323 return md5;
324 }
325
326 char *
327 lastfm_proxy_build_login_url (LastfmProxy *self,
328 const char *token)
329 {
330 LastfmProxyPrivate *priv;
331
332 g_return_val_if_fail (LASTFM_IS_PROXY (self), NULL);
333 g_return_val_if_fail (token, NULL);
334
335 priv = lastfm_proxy_get_instance_private (self);
336
337 return g_strdup_printf ("http://www.last.fm/api/auth/?api_key=%s&token=%s",
338 priv->api_key,
339 token);
340 }
341
342 /**
343 * lastfm_proxy_is_successful:
344 * @root: The root node of a parsed Lastfm response
345 * @error: #GError to set if the response was an error
346 *
347 * Examines the Lastfm response and if it not a successful reply, set @error and
348 * return FALSE.
349 *
350 * Returns: %TRUE if this response is successful, %FALSE otherwise.
351 */
352 gboolean
353 lastfm_proxy_is_successful (RestXmlNode *root,
354 GError **error)
355 {
356 RestXmlNode *node;
357
358 g_return_val_if_fail (root, FALSE);
359
360 if (strcmp (root->name, "lfm") != 0) {
361 g_set_error (error, LASTFM_PROXY_ERROR, 0,
362 "Unexpected response from Lastfm (root node %s)",
363 root->name);
364 return FALSE;
365 }
366
367 if (strcmp (rest_xml_node_get_attr (root, "status"), "ok") != 0) {
368 node = rest_xml_node_find (root, "error");
369 g_set_error_literal (error,LASTFM_PROXY_ERROR,
370 atoi (rest_xml_node_get_attr (node, "code")),
371 node->content);
372 return FALSE;
373 }
374
375 return TRUE;
376 }
377
378 #if BUILD_TESTS
379 void
380 test_lastfm_error (void)
381 {
382 RestXmlParser *parser;
383 RestXmlNode *root;
384 GError *error;
385 const char test_1[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
386 "<foobar/>";
387 const char test_2[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
388 "<lfm status=\"failed\"><error code=\"10\">Invalid API Key</error></lfm>";
389 const char test_3[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
390 "<lfm status=\"ok\">some data</lfm>";
391
392 parser = rest_xml_parser_new ();
393
394 error = NULL;
395 root = rest_xml_parser_parse_from_data (parser, test_1, sizeof (test_1) - 1);
396 lastfm_proxy_is_successful (root, &error);
397 g_assert_error (error, LASTFM_PROXY_ERROR, 0);
398 g_error_free (error);
399 rest_xml_node_unref (root);
400
401 root = rest_xml_parser_parse_from_data (parser, test_2, sizeof (test_2) - 1);
402 error = NULL;
403 lastfm_proxy_is_successful (root, &error);
404 g_assert_error (error, LASTFM_PROXY_ERROR, 10);
405 rest_xml_node_unref (root);
406
407 error = NULL;
408 root = rest_xml_parser_parse_from_data (parser, test_3, sizeof (test_3) - 1);
409 lastfm_proxy_is_successful (root, &error);
410 g_assert_no_error (error);
411 g_error_free (error);
412 rest_xml_node_unref (root);
413 }
414 #endif