Debian Codegraph @

rest-extras/flickr-proxy-call.c

raw
1 /*
2 * librest - RESTful web services access
3 * Copyright (c) 2008, 2009, Intel Corporation.
4 *
5 * Authors: Rob Bradford <[email protected]>
6 * Ross Burton <[email protected]>
7 * Günther Wagner <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU Lesser General Public License,
11 * version 2.1, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24 #include <string.h>
25 #include <libsoup/soup.h>
26 #include <rest/rest-proxy-call.h>
27 #include "flickr-proxy-call.h"
28 #include "flickr-proxy.h"
29 #include "rest/sha1.h"
30
31 typedef struct {
32 gboolean upload;
33 } FlickrProxyCallPrivate;
34
35 G_DEFINE_TYPE_WITH_PRIVATE (FlickrProxyCall, flickr_proxy_call, REST_TYPE_PROXY_CALL)
36
37 enum {
38 PROP_0,
39 PROP_UPLOAD,
40 N_PROPS
41 };
42
43 static GParamSpec *properties [N_PROPS];
44
45 static void
46 flickr_proxy_call_set_property (GObject *object,
47 guint property_id,
48 const GValue *value,
49 GParamSpec *pspec)
50 {
51 FlickrProxyCall *self = FLICKR_PROXY_CALL (object);
52 FlickrProxyCallPrivate *priv = flickr_proxy_call_get_instance_private (self);
53
54 switch (property_id)
55 {
56 case PROP_UPLOAD:
57 priv->upload = g_value_get_boolean (value);
58 break;
59 default:
60 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
61 }
62 }
63
64 static gboolean
65 _prepare (RestProxyCall *call,
66 GError **error)
67 {
68 FlickrProxyCall *self = (FlickrProxyCall *)call;
69 FlickrProxyCallPrivate *priv = flickr_proxy_call_get_instance_private (self);
70
71 FlickrProxy *proxy = NULL;
72 const gchar *token = NULL;
73 GHashTable *params;
74 char *s;
75
76 g_object_get (self, "proxy", &proxy, NULL);
77
78 if (priv->upload) {
79 rest_proxy_bind (REST_PROXY(proxy), "up", "upload");
80 rest_proxy_call_set_function (call, NULL);
81 } else {
82 rest_proxy_bind (REST_PROXY(proxy), "api", "rest");
83 rest_proxy_call_add_param (call, "method",
84 rest_proxy_call_get_function (call));
85 /* We need to reset the function because Flickr puts the function in the
86 * parameters, not in the base URL */
87 rest_proxy_call_set_function (call, NULL);
88 }
89
90 rest_proxy_call_add_param (call, "api_key", flickr_proxy_get_api_key (proxy));
91 token = flickr_proxy_get_token (proxy);
92
93 if (token)
94 rest_proxy_call_add_param (call, "auth_token", token);
95
96 /* Get the string params as a hash for signing */
97 params = rest_params_as_string_hash_table (rest_proxy_call_get_params (call));
98 s = flickr_proxy_sign (proxy, params);
99 g_hash_table_unref (params);
100
101 rest_proxy_call_add_param (call, "api_sig", s);
102 g_free (s);
103
104 g_object_unref (proxy);
105
106 return TRUE;
107 }
108
109 static void
110 flickr_proxy_call_class_init (FlickrProxyCallClass *klass)
111 {
112 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 RestProxyCallClass *call_class = REST_PROXY_CALL_CLASS (klass);
114
115 call_class->prepare = _prepare;
116 object_class->set_property = flickr_proxy_call_set_property;
117
118 /**
119 * FlickrProxyCall:upload:
120 *
121 * Set if the call should be sent to the photo upload endpoint and not the
122 * general-purpose endpoint.
123 */
124 properties [PROP_UPLOAD] =
125 g_param_spec_boolean ("upload",
126 "upload",
127 "upload",
128 FALSE,
129 (G_PARAM_WRITABLE |
130 G_PARAM_CONSTRUCT_ONLY |
131 G_PARAM_STATIC_STRINGS));
132
133 g_object_class_install_properties (object_class, N_PROPS, properties);
134 }
135
136 static void
137 flickr_proxy_call_init (FlickrProxyCall *self)
138 {
139 }