Move API to own object

This commit is contained in:
Markus Koch 2019-07-28 12:35:22 +02:00
parent f075b6c83f
commit 333e4fc03e
7 changed files with 177 additions and 62 deletions

View File

@ -18,12 +18,7 @@ typedef struct _YacosCartItemPriv {
yacos_cart_item_status status;
struct api {
gchar *url_base;
gchar *token;
SoupSession *soup_session_meta;
} api;
YacosApi *api;
} YacosCartItemPriv;
struct _YacosCartItem {
@ -50,10 +45,7 @@ static void yacos_cart_item_finalize(GObject *gobject)
{
YacosCartItem *self = yacos_cart_item_get_instance_private(YACOS_CART_ITEM(gobject));
g_free(self->priv.api.url_base);
g_free(self->priv.api.token);
G_OBJECT_CLASS(yacos_cart_item_parent_class)->constructed(gobject);
G_OBJECT_CLASS(yacos_cart_item_parent_class)->finalize(gobject);
}
static void yacos_cart_item_class_init(YacosCartItemClass *klass)
@ -91,11 +83,6 @@ static void yacos_cart_item_init(YacosCartItem *self)
self->priv.stack_image_loader = GTK_STACK(gtk_builder_get_object(builder, "stack_image_loader"));
self->priv.api.url_base = g_strdup("http://localhost:8080/api.php");
self->priv.api.token = g_strdup("123456");
self->priv.api.soup_session_meta = soup_session_new();
g_signal_connect(self->priv.button_delete,
"clicked",
(GCallback) yacos_cart_item_button_delete_cb,
@ -141,16 +128,15 @@ void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id)
g_free(id_str2);
}
static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
static void yacos_cart_item_fetch_meta_cb (guint status, JsonParser *parser, gpointer user_data)
{
YacosCartItem *self = YACOS_CART_ITEM(user_data);
const char *temp;
JsonParser *parser;
JsonReader *reader;
const static GdkRGBA color_error = {.red = 1.0, .green = 0.0, .blue = 0.0, .alpha = 1.0};
if (msg->status_code != 200) {
g_warning("Received error code %d from server.", msg->status_code);
if (status != 200) {
g_warning("Received error code %d from server.", status);
gtk_label_set_text(GTK_LABEL(self->priv.label_title), "Unknown article");
gtk_widget_override_color(self->priv.label_title, GTK_STATE_FLAG_NORMAL, &color_error);
gtk_label_set_text(GTK_LABEL(self->priv.label_description), "This article will be ignored on checkout.");
@ -159,12 +145,6 @@ static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *ms
return;
}
parser = json_parser_new ();
json_parser_load_from_data (parser,
msg->response_body->data,
msg->response_body->length,
NULL);
reader = json_reader_new (json_parser_get_root (parser));
json_reader_read_member (reader, "title");
@ -187,7 +167,6 @@ static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *ms
json_reader_end_member (reader);
g_object_unref (reader);
g_object_unref (parser);
gtk_editable_set_editable(GTK_EDITABLE(self->priv.entry_comment), TRUE);
self->priv.status = YACOS_CART_ITEM_STATUS_VALID;
@ -195,29 +174,11 @@ static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *ms
void yacos_cart_item_fetch_meta(YacosCartItem *self)
{
SoupMessage *msg;
gchar *url;
gchar *id_str;
gtk_label_set_text(GTK_LABEL(self->priv.label_title), "Loading ...");
gtk_label_set_text(GTK_LABEL(self->priv.label_description), "");
gtk_entry_set_text(GTK_ENTRY(self->priv.entry_comment), "");
id_str = yacos_tag_id_to_str(self->priv.tag_id);
url = g_strdup_printf("%s?meta&id=%s", self->priv.api.url_base, id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", self->priv.api.token);
g_free(url);
g_free(id_str);
soup_session_queue_message(self->priv.api.soup_session_meta,
msg,
yacos_cart_item_fetch_meta_cb,
self);
// TODO: The soup 's probably memory-leaking all over the place. Check this some time..
yacos_api_fetch_meta(self->priv.api, self->priv.tag_id, yacos_cart_item_fetch_meta_cb, self);
}
static void yacos_cart_item_fetch_image_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
@ -244,26 +205,15 @@ static void yacos_cart_item_fetch_image_cb (SoupSession *session, SoupMessage *m
void yacos_cart_item_fetch_image(YacosCartItem *self)
{
SoupMessage *msg;
gchar *url;
gchar *id_str;
id_str = yacos_tag_id_to_str(self->priv.tag_id);
url = g_strdup_printf("%s?image&id=%s", self->priv.api.url_base, id_str);
g_free(id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", self->priv.api.token);
g_free(url);
soup_session_queue_message(self->priv.api.soup_session_meta,
msg,
yacos_cart_item_fetch_image_cb,
self);
/* TODO: More memory leaks? */
yacos_api_fetch_image(self->priv.api, self->priv.tag_id, yacos_cart_item_fetch_image_cb, self);
}
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self)
{
return self->priv.tag_id;
}
void yacos_cart_item_set_yacos_api(YacosCartItem *self, YacosApi *api)
{
self->priv.api = api;
}

View File

@ -4,6 +4,7 @@
#include <gtk/gtk.h>
#include <libsoup/soup.h>
#include "scanner.h"
#include "yacos_api.h"
G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(YacosCartItem, yacos_cart_item, YACOS, CART_ITEM, GtkListBoxRow)
@ -21,6 +22,7 @@ void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id);
void yacos_cart_item_fetch_meta(YacosCartItem *self);
void yacos_cart_item_fetch_image(YacosCartItem *self);
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self);
void yacos_cart_item_set_yacos_api(YacosCartItem *self, YacosApi *api);
G_END_DECLS

View File

@ -12,6 +12,7 @@ static void activate(GtkApplication* app, gpointer user_data)
struct mainWindow **mainWindow = &(app_state->mainWindow);
YacosScanner *scanner;
YacosApi *api;
*mainWindow = mainWindow_new(app);
if (!(*mainWindow)) {
@ -22,6 +23,9 @@ static void activate(GtkApplication* app, gpointer user_data)
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW((*mainWindow)->main_window));
gtk_widget_show_all((*mainWindow)->main_window);
api = yacos_api_new();
mainWindow_set_yacos_api(*mainWindow, api);
scanner = yacos_scanner_new();
if (!scanner) {
g_error("Error initializing scanner.");

View File

@ -48,6 +48,8 @@ void mainWindow_scan_tags(YacosScanner *scanner, struct mainWindow *mainWindow)
if (found)
continue;
le = yacos_cart_item_new();
yacos_cart_item_set_yacos_api(YACOS_CART_ITEM(le),
mainWindow->api);
yacos_cart_item_set_id(YACOS_CART_ITEM(le), tag_id);
yacos_cart_item_fetch_meta(YACOS_CART_ITEM(le));
yacos_cart_item_fetch_image(YACOS_CART_ITEM(le));
@ -55,3 +57,8 @@ void mainWindow_scan_tags(YacosScanner *scanner, struct mainWindow *mainWindow)
gtk_widget_show(le);
}
}
void mainWindow_set_yacos_api(struct mainWindow *mainWindow, YacosApi *api)
{
mainWindow->api = api;
}

View File

@ -4,10 +4,12 @@
#include <gtk/gtk.h>
#include "scanner.h"
#include "cart_item.h"
#include "yacos_api.h"
struct mainWindow {
GtkWidget *main_window;
YacosApi *api;
GtkStack *stack_main;
struct page_intro {
@ -23,6 +25,7 @@ struct mainWindow {
struct mainWindow *mainWindow_new();
void mainWindow_set_yacos_api(struct mainWindow *mainWindow, YacosApi *api);
void mainWindow_scan_tags(YacosScanner *scanner, struct mainWindow *mainWindow);
#endif // MAINWINDOW_H

121
client/src/yacos_api.c Normal file
View File

@ -0,0 +1,121 @@
#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? */
}

28
client/src/yacos_api.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef YACOS_API_H
#define YACOS_API_H
#include <glib-object.h>
#include <libsoup/soup.h>
#include <json-glib/json-glib.h>
#include "scanner.h"
G_BEGIN_DECLS
#define YACOS_TYPE_API yacos_api_get_type()
G_DECLARE_FINAL_TYPE(YacosApi, yacos_api, YACOS, API, GObject)
typedef void (*YacosApiCallback) (guint status, JsonParser *parser, gpointer user_data);
YacosApi *yacos_api_new(void);
void yacos_api_fetch_meta(YacosApi *api,
yacos_tag_id tag_id,
YacosApiCallback cb,
gpointer user_data);
void yacos_api_fetch_image(YacosApi *api,
yacos_tag_id tag_id,
SoupSessionCallback cb,
gpointer user_data);
G_END_DECLS
#endif // YACOS_API_H