summaryrefslogtreecommitdiff
path: root/rest/rest-proxy.c
blob: 64b02399203454fca1ce75d19e30646dccf9c559 (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
#include <libsoup/soup.h>

#include "rest-proxy.h"

G_DEFINE_TYPE (RestProxy, rest_proxy, G_TYPE_OBJECT)

#define GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), REST_TYPE_PROXY, RestProxyPrivate))

typedef struct _RestProxyPrivate RestProxyPrivate;

struct _RestProxyPrivate {
  gchar *url_format;
  gchar *url;
  gboolean binding_required;
  SoupSession *session;
};

enum
{
  PROP0 = 0,
  PROP_URL_FORMAT,
  PROP_BINDING_REQUIRED
};

static void
rest_proxy_get_property (GObject *object, guint property_id,
                              GValue *value, GParamSpec *pspec)
{
  RestProxyPrivate *priv = GET_PRIVATE (object);

  switch (property_id) {
    case PROP_URL_FORMAT:
      g_value_set_string (value, priv->url_format);
      break;
    case PROP_BINDING_REQUIRED:
      g_value_set_boolean (value, priv->binding_required);
      break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
rest_proxy_set_property (GObject *object, guint property_id,
                              const GValue *value, GParamSpec *pspec)
{
  RestProxyPrivate *priv = GET_PRIVATE (object);

  switch (property_id) {
    case PROP_URL_FORMAT:
      g_free (priv->url_format);
      priv->url_format = g_value_dup_string (value);

      /* Clear the cached url */
      g_free (priv->url);
      priv->url = NULL;
      break;
    case PROP_BINDING_REQUIRED:
      priv->binding_required = g_value_get_boolean (value);

      /* Clear cached url */
      g_free (priv->url);
      priv->url;
      break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
rest_proxy_dispose (GObject *object)
{
  if (G_OBJECT_CLASS (rest_proxy_parent_class)->dispose)
    G_OBJECT_CLASS (rest_proxy_parent_class)->dispose (object);
}

static void
rest_proxy_finalize (GObject *object)
{
  if (G_OBJECT_CLASS (rest_proxy_parent_class)->finalize)
    G_OBJECT_CLASS (rest_proxy_parent_class)->finalize (object);
}

static void
rest_proxy_class_init (RestProxyClass *klass)
{
  GParamSpec *pspec;
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (RestProxyPrivate));

  object_class->get_property = rest_proxy_get_property;
  object_class->set_property = rest_proxy_set_property;
  object_class->dispose = rest_proxy_dispose;
  object_class->finalize = rest_proxy_finalize;

  pspec = g_param_spec_string ("url-format", 
      "url-format",
      "Format string for the RESTful url for this proxy",
      NULL,
      G_PARAM_READWRITE);
  g_object_class_install_property (object_class, 
      PROP_URL_FORMAT,
      pspec);

  pspec = g_param_spec_boolean ("binding-required",
      "binding-required",
      "Whether the URL format string requires binding",
      FALSE,
      G_PARAM_READWRITE);
  g_object_class_install_property (object_class,
      PROP_BINDING_REQUIRED,
      pspec);
}

static void
rest_proxy_init (RestProxy *self)
{
  RestProxyPrivate *priv = GET_PRIVATE (self);

  /* TODO: Use proxy settings, etc etc... */
  priv->session = soup_session_async_new ();
}

RestProxy *
rest_proxy_new (const gchar *url_format, 
                gboolean binding_required)
{
  return g_object_new (REST_TYPE_PROXY, 
      "url-format", url_format,
      "binding-required", binding_required,
      NULL);
}

gboolean
rest_proxy_bind (RestProxy *proxy, ...)
{
  RestProxyPrivate *priv = GET_PRIVATE (proxy);
  va_list params;

  g_return_val_if_fail (proxy != NULL, FALSE);
  g_return_val_if_fail (priv->url_format != NULL, FALSE);
  g_return_val_if_fail (priv->binding_required != TRUE, FALSE);

  g_free (priv->url);
  priv->url = g_strdup_vprintf (priv->url_format, params);
}

typedef struct {
  RestProxy *proxy;
  RestProxyCallRawCallback callback;
  GObject *weak_object;
  gpointer userdata;
  SoupMessage *message;
} RestProxyCallRawAsyncClosure;

static void _call_raw_async_weak_notify_cb (gpointer *data, 
                                            GObject *dead_object);
static void _call_raw_async_finished_cb (SoupMessage *message,
                                         gpointer userdata);

static GHashTable *
_populate_headers_hash_table (const gchar *name,
                              const gchar *value,
                              gpointer userdata)
{
  GHashTable *headers = (GHashTable *)userdata;

  g_hash_table_insert (headers, g_strdup (name), g_strdup (value));
}

static void
_call_raw_async_finished_cb (SoupMessage *message,
                             gpointer userdata)
{
  RestProxyCallRawAsyncClosure *closure;
  GHashTable *headers;

  closure = (RestProxyCallRawAsyncClosure *)userdata;

  headers = g_hash_table_new_full (g_str_hash, 
      g_str_equal,
      g_free,
      g_free);

  soup_message_headers_foreach (message->response_headers,
      (SoupMessageHeadersForeachFunc)_populate_headers_hash_table,
      headers);

  closure->callback (closure->proxy,
      message->status_code,
      message->reason_phrase,
      headers,
      message->response_body->data,
      message->response_body->length,
      closure->weak_object,
      closure->userdata);

  if (closure->weak_object)
  {
    g_object_weak_unref (closure->weak_object, 
        (GWeakNotify)_call_raw_async_weak_notify_cb,
        closure);
  }

  g_object_unref (closure->proxy);
  g_free (closure);
}

static void 
_call_raw_async_weak_notify_cb (gpointer *data,
                                GObject *dead_object)
{
  RestProxyCallRawAsyncClosure *closure;

  closure = (RestProxyCallRawAsyncClosure *)data;

  g_signal_handlers_disconnect_by_func (closure->message,
      _call_raw_async_finished_cb,
      closure);
  g_object_unref (closure->proxy);
  g_free (closure);
}

gboolean
rest_proxy_call_raw_async (RestProxy *proxy,
                           const gchar *function,
                           const gchar *method,
                           RestProxyCallRawCallback callback,
                           GObject *weak_object,
                           gpointer userdata,
                           GError *error,
                           const gchar *first_field_name,
                           ...)
{
  RestProxyPrivate *priv = GET_PRIVATE (proxy);
  va_list params;
  SoupMessage *message = NULL;
  gchar *url = NULL;
  RestProxyCallRawAsyncClosure *closure;

  if (priv->binding_required && !priv->url)
  {
    g_critical (G_STRLOC ": URL requires binding and is unbound");
    /* FIXME: Return a GError */
    return FALSE;
  }

  if (!priv->binding_required)
  {
    priv->url = g_strdup (priv->url_format);
  }

  if (function)
  {
    if (g_str_has_suffix (priv->url, "/"))
    {
      url = g_strdup_printf ("%s%s", priv->url, function);
    } else {
      url = g_strdup_printf ("%s/%s", priv->url, function);
    }
  } else {
    url = g_strdup (priv->url);
  }

  if (first_field_name)
  {
    message = soup_form_request_new (method,
        url,
        first_field_name,
        params);
  } else {
    message = soup_message_new (method, url);
  }

  closure = g_new0 (RestProxyCallRawAsyncClosure, 1);
  closure->proxy = g_object_ref (proxy);
  closure->callback = callback;
  closure->weak_object = weak_object;
  closure->message = message;

  if (closure->weak_object)
  {
    g_object_weak_ref (closure->weak_object, 
        (GWeakNotify)_call_raw_async_weak_notify_cb, 
        closure);
  }

  g_signal_connect (message,
      "finished", 
      (GCallback)_call_raw_async_finished_cb,
      closure);

  /* TODO: Should we do something in this callback ? */
  soup_session_queue_message (priv->session,
      message,
      NULL,
      NULL);

  return TRUE;
}