From 4283dc44524420bdf9e47a6fca4b2b7c39d48b43 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 14 Dec 2015 16:25:23 +0100 Subject: [PATCH] Add the matrix_http_api_gen_parameters() convenience method --- .../matrix-glib/matrix-glib-sections.txt | 1 + src/matrix-http-api.c | 40 +++++++++++++++++++ src/matrix-http-api.h | 1 + 3 files changed, 42 insertions(+) diff --git a/docs/reference/matrix-glib/matrix-glib-sections.txt b/docs/reference/matrix-glib/matrix-glib-sections.txt index d7afec1..a4cc035 100644 --- a/docs/reference/matrix-glib/matrix-glib-sections.txt +++ b/docs/reference/matrix-glib/matrix-glib-sections.txt @@ -57,6 +57,7 @@ matrix_http_api_new matrix_http_api_get_validate_certificate matrix_http_api_set_validate_certificate matrix_http_api_get_base_url +matrix_http_api_gen_parameters MatrixHTTPAPI MatrixHTTPAPIClass diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index 956f053..a80e781 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -306,6 +306,46 @@ matrix_http_api_get_validate_certificate(MatrixHTTPAPI *api) return priv->validate_certificate; } +/** + * matrix_http_api_gen_parameters: + * @param1_name: name of the first parameter + * @...: value of the first parameter, followed by name/value pairs, + * terminated by a NULL value as a parameter name + * + * Generate a GHashTable suitable as the parameters argument for + * different API calls. + * + * Returns: (transfer full): a #GHashTable with the specified + * parameter table + */ +GHashTable * +matrix_http_api_gen_parameters(const gchar *param1_name, ...) +{ + GHashTable *table; + va_list var_args; + gchar *name; + + g_return_val_if_fail(param1_name != NULL, NULL); + + table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + + va_start(var_args, param1_name); + + name = (gchar *)param1_name; + + while (name) { + gchar *value = va_arg(var_args, gchar *); + + g_hash_table_insert(table, name, value); + + name = va_arg(var_args, gchar *); + } + + va_end(var_args); + + return table; +} + /** * matrix_http_api_get_base_url: * @api: a #MatrixHTTPAPI implementation diff --git a/src/matrix-http-api.h b/src/matrix-http-api.h index c70c21c..78e0dcb 100644 --- a/src/matrix-http-api.h +++ b/src/matrix-http-api.h @@ -50,6 +50,7 @@ void matrix_http_api_set_validate_certificate(MatrixHTTPAPI *api, gboolean matrix_http_api_get_validate_certificate(MatrixHTTPAPI *api); const gchar *matrix_http_api_get_base_url(MatrixHTTPAPI *api); +GHashTable *matrix_http_api_gen_parameters(const gchar *param1_name, ...); MatrixHTTPAPI *matrix_http_api_new(const gchar *base_url, const gchar *token); G_END_DECLS