Yet Another Check-Out System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 lines
2.7 KiB

#include "yacos_api.h"
struct _YacosApi {
gchar *url_base;
gchar *token;
SoupSession *soup_session;
};
typedef struct _YacosApiCallbackData {
YacosApiCallback cb;
gpointer user_data;
} YacosApiCallbackData;
G_DEFINE_TYPE(YacosApi, yacos_api, G_TYPE_OBJECT)
static void yacos_api_finalize(GObject *gobject)
{
YacosApi *self = yacos_api_get_instance_private(YACOS_API(gobject));
g_object_unref(self->soup_session);
g_free(self->url_base);
g_free(self->token);
G_OBJECT_CLASS(yacos_api_parent_class)->finalize(gobject);
}
static void yacos_api_class_init(YacosApiClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = yacos_api_finalize;
}
static void yacos_api_init(YacosApi *self)
{
self->url_base = g_strdup("http://localhost:8080/api.php");
self->token = g_strdup("123456");
self->soup_session = soup_session_new();
}
YacosApi *yacos_api_new(void)
{
return g_object_new(YACOS_TYPE_API, 0);
}
static void yacos_api_fetch_meta_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
YacosApiCallbackData *callback_data = user_data;
JsonParser *parser = NULL;
parser = json_parser_new();
json_parser_load_from_data(parser,
msg->response_body->data,
msg->response_body->length,
NULL);
callback_data->cb(msg->status_code, parser, callback_data->user_data);
g_object_unref(parser);
free(callback_data);
}
void yacos_api_fetch_meta(YacosApi *api,
yacos_tag_id tag_id,
YacosApiCallback cb,
gpointer user_data)
{
SoupMessage *msg;
gchar *url;
gchar *id_str;
YacosApiCallbackData *callback_data;
callback_data = malloc(sizeof(callback_data));
callback_data->cb = cb;
callback_data->user_data = user_data;
id_str = yacos_tag_id_to_str(tag_id);
url = g_strdup_printf("%s?meta&id=%s", api->url_base, id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", api->token);
g_free(url);
g_free(id_str);
soup_session_queue_message(api->soup_session,
msg,
yacos_api_fetch_meta_cb,
callback_data);
// callback_data will be freed in the callback
// TODO: The soup 's probably memory-leaking all over the place. Check this some time..
}
void yacos_api_fetch_image(YacosApi *api,
yacos_tag_id tag_id,
SoupSessionCallback cb,
gpointer user_data)
{
SoupMessage *msg;
gchar *url;
gchar *id_str;
id_str = yacos_tag_id_to_str(tag_id);
url = g_strdup_printf("%s?image&id=%s", api->url_base, id_str);
g_free(id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", api->token);
g_free(url);
soup_session_queue_message(api->soup_session,
msg,
cb,
user_data);
/* TODO: More memory leaks? */
}