summaryrefslogtreecommitdiff
path: root/rest-extras/lastfm-proxy.c
blob: b9fc7139ca27c93c68441db954afcaf40cc3aa6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * 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 <stdlib.h>
#include <string.h>
#include <rest/rest-proxy.h>
#include <libsoup/soup.h>
#include "lastfm-proxy.h"
#include "lastfm-proxy-private.h"
#include "lastfm-proxy-call.h"

G_DEFINE_TYPE (LastfmProxy, lastfm_proxy, REST_TYPE_PROXY)

enum {
  PROP_0,
  PROP_API_KEY,
  PROP_SECRET,
  PROP_SESSION_KEY,
};

GQuark
lastfm_proxy_error_quark (void)
{
  return g_quark_from_static_string ("rest-lastfm-proxy");
}

static RestProxyCall *
_new_call (RestProxy *proxy)
{
  RestProxyCall *call;

  call = g_object_new (LASTFM_TYPE_PROXY_CALL,
                       "proxy", proxy,
                       NULL);

  return call;
}

static void
lastfm_proxy_get_property (GObject *object, guint property_id,
                           GValue *value, GParamSpec *pspec)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (object);

  switch (property_id) {
  case PROP_API_KEY:
    g_value_set_string (value, priv->api_key);
    break;
  case PROP_SECRET:
    g_value_set_string (value, priv->secret);
    break;
  case PROP_SESSION_KEY:
    g_value_set_string (value, priv->session_key);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
lastfm_proxy_set_property (GObject *object, guint property_id,
                           const GValue *value, GParamSpec *pspec)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (object);

  switch (property_id) {
  case PROP_API_KEY:
    if (priv->api_key)
      g_free (priv->api_key);
    priv->api_key = g_value_dup_string (value);
    break;
  case PROP_SECRET:
    if (priv->secret)
      g_free (priv->secret);
    priv->secret = g_value_dup_string (value);
    break;
  case PROP_SESSION_KEY:
    if (priv->session_key)
      g_free (priv->session_key);
    priv->session_key = g_value_dup_string (value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
lastfm_proxy_finalize (GObject *object)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (object);

  g_free (priv->api_key);
  g_free (priv->secret);
  g_free (priv->session_key);

  G_OBJECT_CLASS (lastfm_proxy_parent_class)->finalize (object);
}

#ifndef G_PARAM_STATIC_STRINGS
#define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
#endif

static void
lastfm_proxy_class_init (LastfmProxyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  RestProxyClass *proxy_class = REST_PROXY_CLASS (klass);
  GParamSpec *pspec;

  g_type_class_add_private (klass, sizeof (LastfmProxyPrivate));

  object_class->get_property = lastfm_proxy_get_property;
  object_class->set_property = lastfm_proxy_set_property;
  object_class->finalize = lastfm_proxy_finalize;

  proxy_class->new_call = _new_call;

  pspec = g_param_spec_string ("api-key", "api-key",
                               "The API key", NULL,
                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class,
                                   PROP_API_KEY,
                                   pspec);

  pspec = g_param_spec_string ("secret", "secret",
                               "The API key secret", NULL,
                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class,
                                   PROP_SECRET,
                                   pspec);

  pspec = g_param_spec_string ("session-key", "session-key",
                               "The session key", NULL,
                               G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class,
                                   PROP_SESSION_KEY,
                                   pspec);
}

static void
lastfm_proxy_init (LastfmProxy *self)
{
  self->priv = LASTFM_PROXY_GET_PRIVATE (self);
}

RestProxy *
lastfm_proxy_new (const char *api_key,
                  const char *secret)
{
  return lastfm_proxy_new_with_session (api_key,
                                        secret,
                                        NULL);
}

RestProxy *
lastfm_proxy_new_with_session (const char *api_key,
                               const char *secret,
                               const char *session_key)
{
  return g_object_new (LASTFM_TYPE_PROXY,
                       "api-key", api_key,
                       "secret", secret,
                       "session-key", session_key,
                       "url-format", "http://ws.audioscrobbler.com/2.0/",
                       "binding-required", FALSE,
                       NULL);
}

/**
 * lastfm_proxy_get_api_key:
 * @proxy: an #LastfmProxy
 *
 * Get the API key.
 *
 * Returns: the API key. This string is owned by #LastfmProxy and should not be
 * freed.
 */
const char *
lastfm_proxy_get_api_key (LastfmProxy *proxy)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (proxy);
  return priv->api_key;
}

/**
 * lastfm_proxy_get_secret:
 * @proxy: an #LastfmProxy
 *
 * Get the secret for authentication.
 *
 * Returns: the secret. This string is owned by #LastfmProxy and should not be
 * freed.
 */
const char *
lastfm_proxy_get_secret (LastfmProxy *proxy)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (proxy);
  return priv->secret;
}

/**
 * lastfm_proxy_get_session_key:
 * @proxy: an #LastfmProxy
 *
 * Get the current session key.
 *
 * Returns: the session key, or %NULL if there is no session key yet.  This string is owned
 * by #LastfmProxy and should not be freed.
 */
const char *
lastfm_proxy_get_session_key (LastfmProxy *proxy)
{
  LastfmProxyPrivate *priv = LASTFM_PROXY_GET_PRIVATE (proxy);
  return priv->session_key;
}

/**
 * lastfm_proxy_set_session_key:
 * @proxy: an #LastfmProxy
 * @session_key: the access session_key
 *
 * Set the session key.
 */
void
lastfm_proxy_set_session_key (LastfmProxy *proxy, const char *session_key)
{
  LastfmProxyPrivate *priv;

  g_return_if_fail (LASTFM_IS_PROXY (proxy));
  priv = LASTFM_PROXY_GET_PRIVATE (proxy);

  if (priv->session_key)
    g_free (priv->session_key);

  priv->session_key = g_strdup (session_key);
}

char *
lastfm_proxy_sign (LastfmProxy *proxy, GHashTable *params)
{
  LastfmProxyPrivate *priv;
  GString *s;
  GList *keys;
  char *md5;

  g_return_val_if_fail (LASTFM_IS_PROXY (proxy), NULL);
  g_return_val_if_fail (params, NULL);

  priv = LASTFM_PROXY_GET_PRIVATE (proxy);

  s = g_string_new (NULL);

  keys = g_hash_table_get_keys (params);
  keys = g_list_sort (keys, (GCompareFunc)strcmp);

  while (keys) {
    const char *key;
    const char *value;

    key = keys->data;
    value = g_hash_table_lookup (params, key);

    g_string_append_printf (s, "%s%s", key, value);

    keys = g_list_delete_link (keys, keys);
  }

  g_string_append (s, priv->secret);

  md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, s->str, s->len);

  g_string_free (s, TRUE);

  return md5;
}

char *
lastfm_proxy_build_login_url (LastfmProxy *proxy, const char *token)
{
  g_return_val_if_fail (LASTFM_IS_PROXY (proxy), NULL);
  g_return_val_if_fail (token, NULL);

  return g_strdup_printf ("http://www.last.fm/api/auth/?api_key=%s&token=%s",
                          proxy->priv->api_key,
                          token);
}

/**
 * lastfm_proxy_is_successful:
 * @root: The root node of a parsed Lastfm response
 * @error: #GError to set if the response was an error
 *
 * Examines the Lastfm response and if it not a successful reply, set @error and
 * return FALSE.
 *
 * Returns: %TRUE if this response is successful, %FALSE otherwise.
 */
gboolean
lastfm_proxy_is_successful (RestXmlNode *root, GError **error)
{
  RestXmlNode *node;

  g_return_val_if_fail (root, FALSE);

  if (strcmp (root->name, "lfm") != 0) {
    g_set_error (error, LASTFM_PROXY_ERROR, 0,
                 "Unexpected response from Lastfm (root node %s)",
                 root->name);
    return FALSE;
  }

  if (strcmp (rest_xml_node_get_attr (root, "status"), "ok") != 0) {
    node = rest_xml_node_find (root, "error");
    g_set_error_literal (error,LASTFM_PROXY_ERROR,
                         atoi (rest_xml_node_get_attr (node, "code")),
                         node->content);
    return FALSE;
  }

  return TRUE;
}

#if BUILD_TESTS
void
test_lastfm_error (void)
{
  RestXmlParser *parser;
  RestXmlNode *root;
  GError *error;
  const char test_1[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<foobar/>";
  const char test_2[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<lfm status=\"failed\"><error code=\"10\">Invalid API Key</error></lfm>";
  const char test_3[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<lfm status=\"ok\">some data</lfm>";

  parser = rest_xml_parser_new ();

  error = NULL;
  root = rest_xml_parser_parse_from_data (parser, test_1, sizeof (test_1) - 1);
  lastfm_proxy_is_successful (root, &error);
  g_assert_error (error, LASTFM_PROXY_ERROR, 0);
  g_error_free (error);
  rest_xml_node_unref (root);

  root = rest_xml_parser_parse_from_data (parser, test_2, sizeof (test_2) - 1);
  error = NULL;
  lastfm_proxy_is_successful (root, &error);
  g_assert_error (error, LASTFM_PROXY_ERROR, 10);
  rest_xml_node_unref (root);

  error = NULL;
  root = rest_xml_parser_parse_from_data (parser, test_3, sizeof (test_3) - 1);
  lastfm_proxy_is_successful (root, &error);
  g_assert_no_error (error);
  g_error_free (error);
  rest_xml_node_unref (root);
}
#endif