From 10b6296565a187208045f427b87400caee889b70 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 14 Dec 2015 16:29:22 +0100 Subject: [PATCH] Create a test client --- .gitignore | 1 + src/Makefile.am | 5 +++ src/test-client.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/test-client.c diff --git a/.gitignore b/.gitignore index 88b5010..8475651 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ Makefile.in /src/*.la /src/*.gir /src/*.typelib +/src/test-client /aclocal.m4 /ar-lib /autom4te.cache diff --git a/src/Makefile.am b/src/Makefile.am index 2cd5539..6da1310 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,7 @@ AM_CPPFLAGS = -DG_LOG_DOMAIN=\"Matrix-GLib\" lib_LTLIBRARIES = libmatrix-glib-0.0.la +bin_PROGRAMS = test-client INST_H_SRC_FILES = \ matrix-client.h \ @@ -23,6 +24,10 @@ libmatrix_glib_0_0_la_CFLAGS = $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) $(SOUP_CFLAGS) $ libmatrix_glib_0_0_la_LIBADD = $(GLIB_LIBS) $(GOBJECT_LIBS) $(SOUP_LIBS) $(JSON_LIBS) libmatrix_glib_0_0_la_DEPENDENCIES = +test_client_SOURCES = test-client.c $(libmatrix_glib_0_0_la_SOURCES) +test_client_CFLAGS = $(libmatrix_glib_0_0_la_CFLAGS) +test_client_LDADD = $(libmatrix_glib_0_0_la_LIBADD) + CLEANFILES = EXTRA_DIST = $(INST_H_SRC_FILES) diff --git a/src/test-client.c b/src/test-client.c new file mode 100644 index 0000000..97482a3 --- /dev/null +++ b/src/test-client.c @@ -0,0 +1,105 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that 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 matrix-glib-sdk. If not, see + * . + */ + +#include + +#include "matrix-http-api.h" + +#define LOG_DOMAIN "Matrix-Test-Client" + +static gchar *user; +static gchar *password; + +static GOptionEntry entries[] = { + {"user", 'u', 0, G_OPTION_ARG_STRING, &user}, + {"password", 'p', 0, G_OPTION_ARG_STRING, &password}, +}; + +static void +login_finished(MatrixAPI *api, JsonNode *content, gpointer data) +{ + JsonPath *path = json_path_new(); + JsonNode *result; + + json_path_compile(path, "$.user_id", NULL); + + if ((result = json_path_match(path, content)) != NULL) { + JsonArray *array = json_node_get_array(result); + const gchar *user_id; + + if (json_array_get_length(array) != 1) { + g_printf("Result is not one long?\n"); + } + + user_id = json_array_get_string_element(array, 0); + + g_printf("Node type: %s\n", json_node_type_name(result)); + + g_printf("Logged in as %s\n", user_id); + } else { + g_printf("Login unsuccessful!\n"); + } +} + +int +main(int argc, char *argv[]) +{ + MatrixHTTPAPI *api; + GHashTable *params; + GMainLoop *loop = g_main_loop_new(NULL, FALSE); + GOptionContext *opts; + GError *err = NULL; + gchar *url; + + opts = g_option_context_new(NULL); + g_option_context_add_main_entries(opts, entries, NULL); + + if (!g_option_context_parse(opts, &argc, &argv, &err)) { + g_printerr("Could not parse arguments: %s\n", err->message); + g_printerr("%s", g_option_context_get_help(opts, TRUE, NULL)); + + return 1; + } + + if (argc != 2) { + g_printerr("%s", g_option_context_get_help(opts, TRUE, NULL)); + + return 1; + } + + g_option_context_free(opts); + + url = argv[1]; + + g_info("Starting up: %s with %s:%s", url, user, password); + + api = matrix_http_api_new(url, NULL); + params = matrix_http_api_gen_parameters( + "user", user, + "password", password, + NULL); + matrix_http_api_login(MATRIX_API(api), + login_finished, NULL, + "m.login.password", + params); + + g_info("Entering main loop"); + g_main_loop_run(loop); + + return 0; +}