Add separate media URI

This commit is contained in:
Gergely Polonkai 2016-01-15 13:13:23 +01:00
parent 97a169959c
commit 0b0be1d460

View File

@ -49,10 +49,12 @@
*/ */
#define API_ENDPOINT "/_matrix/client/api/v1/" #define API_ENDPOINT "/_matrix/client/api/v1/"
#define MEDIA_ENDPOINT "/_matrix/media/v1/"
typedef struct _MatrixHTTPAPIPrivate { typedef struct _MatrixHTTPAPIPrivate {
SoupSession *soup_session; SoupSession *soup_session;
SoupURI *uri; SoupURI *uri;
SoupURI *media_uri;
gchar *token; gchar *token;
gchar *refresh_token; gchar *refresh_token;
gchar *user_id; gchar *user_id;
@ -70,12 +72,18 @@ enum {
N_PROPERTIES N_PROPERTIES
}; };
typedef enum {
CALL_API,
CALL_MEDIA
} CallType;
typedef struct { typedef struct {
MatrixHTTPAPI *api; MatrixHTTPAPI *api;
JsonNode *request_content; JsonNode *request_content;
MatrixAPICallback callback; MatrixAPICallback callback;
gpointer callback_data; gpointer callback_data;
gboolean accept_non_json; gboolean accept_non_json;
CallType call_type;
} MatrixHTTPAPIRequest; } MatrixHTTPAPIRequest;
GParamSpec *obj_properties[N_PROPERTIES] = {NULL,}; GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
@ -152,7 +160,7 @@ matrix_http_api_set_property(GObject *gobject,
{ {
const gchar *base_url; const gchar *base_url;
gchar *last_occurence; gchar *last_occurence;
SoupURI *api_uri; SoupURI *api_uri, *media_uri;
base_url = g_value_get_string(value); base_url = g_value_get_string(value);
@ -170,16 +178,21 @@ matrix_http_api_set_property(GObject *gobject,
} }
_set_url(&api_uri, base_url, API_ENDPOINT); _set_url(&api_uri, base_url, API_ENDPOINT);
_set_url(&media_uri, base_url, MEDIA_ENDPOINT);
if (api_uri) { if (api_uri && media_uri) {
gchar *api_url; gchar *api_url, *media_url;
if (priv->uri) { if (priv->uri) {
soup_uri_free(priv->uri); soup_uri_free(priv->uri);
} }
if (priv->media_uri) {
soup_uri_free(priv->media_uri);
}
priv->uri = api_uri; priv->uri = api_uri;
priv->media_uri = media_uri;
// Free all tokens and IDs, as they wont be valid for // Free all tokens and IDs, as they wont be valid for
// the new server // the new server
@ -193,15 +206,21 @@ matrix_http_api_set_property(GObject *gobject,
priv->user_id = NULL; priv->user_id = NULL;
api_url = soup_uri_to_string(api_uri, FALSE); api_url = soup_uri_to_string(api_uri, FALSE);
media_url = soup_uri_to_string(media_uri, FALSE);
g_debug("API URL: %s", api_url); g_debug("API URL: %s", api_url);
g_debug("Media URL: %s", media_url);
g_free(api_url); g_free(api_url);
g_free(media_url);
} else { } else {
if (api_uri) { if (api_uri) {
soup_uri_free(api_uri); soup_uri_free(api_uri);
} }
if (media_uri) {
soup_uri_free(media_uri);
}
g_warning("Invalid URL: %s", base_url); g_warning("Invalid URL: %s", base_url);
} }
@ -470,9 +489,22 @@ _response_callback(SoupSession *session,
gsize datalen; gsize datalen;
JsonParser *parser; JsonParser *parser;
SoupURI *request_uri = soup_message_get_uri(msg); SoupURI *request_uri = soup_message_get_uri(msg);
const gchar *request_url = soup_uri_get_path(request_uri) const gchar *request_url;
switch (request->call_type) {
case CALL_API:
request_url = soup_uri_get_path(request_uri)
+ strlen(API_ENDPOINT); + strlen(API_ENDPOINT);
break;
case CALL_MEDIA:
request_url = soup_uri_get_path(request_uri)
+ strlen(MEDIA_ENDPOINT);
break;
}
buffer = soup_message_body_flatten(msg->response_body); buffer = soup_message_body_flatten(msg->response_body);
soup_buffer_get_data(buffer, &data, &datalen); soup_buffer_get_data(buffer, &data, &datalen);
@ -662,6 +694,7 @@ static void
_send(MatrixHTTPAPI *api, _send(MatrixHTTPAPI *api,
MatrixAPICallback callback, MatrixAPICallback callback,
gpointer user_data, gpointer user_data,
CallType call_type,
const gchar *method, const gchar *method,
const gchar *path, const gchar *path,
GHashTable *params, GHashTable *params,
@ -699,8 +732,18 @@ _send(MatrixHTTPAPI *api,
return; return;
} }
switch (call_type) {
case CALL_API:
request_path = soup_uri_new_with_base(priv->uri, path); request_path = soup_uri_new_with_base(priv->uri, path);
break;
case CALL_MEDIA:
request_path = soup_uri_new_with_base(priv->media_uri, path);
break;
}
if (!params) { if (!params) {
params = create_query_params(); params = create_query_params();
} }
@ -745,6 +788,7 @@ _send(MatrixHTTPAPI *api,
request->callback = callback; request->callback = callback;
request->callback_data = user_data; request->callback_data = user_data;
request->accept_non_json = accept_non_json; request->accept_non_json = accept_non_json;
request->call_type = call_type;
soup_session_queue_message(priv->soup_session, soup_session_queue_message(priv->soup_session,
message, message,
@ -769,6 +813,7 @@ i_login(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"POST", "login", NULL, body, "POST", "login", NULL, body,
FALSE, error); FALSE, error);
} }
@ -902,6 +947,7 @@ i_create_room(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"POST", "createRoom", NULL, body, "POST", "createRoom", NULL, body,
FALSE, error); FALSE, error);
} }
@ -928,6 +974,7 @@ i_initial_sync(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"GET", "initialSync", params, NULL, "GET", "initialSync", params, NULL,
FALSE, err); FALSE, err);
} }
@ -955,6 +1002,7 @@ i_event_stream(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"GET", "events", params, NULL, "GET", "events", params, NULL,
FALSE, err); FALSE, err);
} }
@ -974,6 +1022,7 @@ i_leave_room(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"POST", path, NULL, NULL, "POST", path, NULL, NULL,
FALSE, error); FALSE, error);
g_free(path); g_free(path);
@ -987,6 +1036,7 @@ i_list_public_rooms(MatrixAPI *api,
{ {
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"GET", "publicRooms", NULL, NULL, "GET", "publicRooms", NULL, NULL,
FALSE, error); FALSE, error);
} }
@ -1015,6 +1065,7 @@ i_join_room(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"POST", path, NULL, NULL, "POST", path, NULL, NULL,
FALSE, error); FALSE, error);
g_free(path); g_free(path);
@ -1036,6 +1087,7 @@ i_get_presence_list(MatrixAPI *api,
_send(MATRIX_HTTP_API(api), _send(MATRIX_HTTP_API(api),
callback, user_data, callback, user_data,
CALL_API,
"GET", path, NULL, NULL, "GET", path, NULL, NULL,
FALSE, error); FALSE, error);
g_free(path); g_free(path);