aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2015-10-20 10:58:25 +0100
committerRichard van der Hoff <richard@matrix.org>2015-10-20 10:58:25 +0100
commit9d691407057959418d3c23f1ec896eed5efa5fcb (patch)
treef495d6d7df9e0a46ecbe593a2439b35f6b4b41e4
parente8c15ca56f5a2951d97aa49ac0e8aea8c93b1320 (diff)
downloadpurple-matrix-9d691407057959418d3c23f1ec896eed5efa5fcb.tar.gz
Refactor API callbacks
Factor out the handling of errors from API calls, to make it easier to add new ones.
-rw-r--r--matrix-api.c67
-rw-r--r--matrix-api.h37
-rw-r--r--matrix-sync.c35
3 files changed, 89 insertions, 50 deletions
diff --git a/matrix-api.c b/matrix-api.c
index 34021af..7d2676a 100644
--- a/matrix-api.c
+++ b/matrix-api.c
@@ -87,6 +87,55 @@ static const gchar *matrix_api_parse_body(const gchar *ret_data,
return body_pointer;
}
+
+/**
+ * Default callback if there was an error calling the API. We just put the
+ * connection into the "error" state.
+ *
+ * @param account The MatrixAccount passed into the api method
+ * @param user_data The user data that your code passed into the api
+ * method.
+ * @param error_message a descriptive error message
+ *
+ */
+void matrix_api_error(MatrixAccount *ma, gpointer user_data,
+ const gchar *error_message)
+{
+ purple_debug_info("matrixprpl", "Error calling API: %s\n",
+ error_message);
+ purple_connection_error_reason(ma->pc,
+ PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error_message);
+}
+
+/**
+ * Default callback if the API returns a non-200 response. We just put the
+ * connection into the "error" state.
+ *
+ * @param account The MatrixAccount passed into the api method
+ * @param user_data The user data that your code passed into the api
+ * method.
+ * @param http_response HTTP response code.
+ * @param http_response_msg message from the HTTP response line.
+ * @param body_start NULL if there was no body in the response;
+ * otherwise a pointer to the start of the body
+ * in the response
+ * @param json_root NULL if there was no body, or it could not be
+ * parsed as JSON; otherwise the root of the JSON
+ * tree in the response
+ */
+void matrix_api_bad_response(MatrixAccount *ma, gpointer user_data,
+ int http_response_code, const gchar *http_response_msg,
+ const gchar *body_start, struct _JsonNode *json_root)
+{
+ purple_debug_info("matrixprpl", "API gave response %i (%s): %s\n",
+ http_response_code, http_response_msg, body_start);
+ purple_connection_error_reason(ma->pc,
+ PURPLE_CONNECTION_ERROR_OTHER_ERROR,
+ http_response_msg);
+}
+
+
+
/**
* The callback we give to purple_util_fetch_url_request - does some
* initial processing of the response
@@ -100,8 +149,8 @@ static void matrix_api_complete(PurpleUtilFetchUrlData *url_data,
MatrixApiRequestData *data = (MatrixApiRequestData *)user_data;
int response_code = -1;
gchar *response_message = NULL;
- JsonParser *parser;
- JsonNode *root;
+ JsonParser *parser = NULL;
+ JsonNode *root = NULL;
const gchar *body_start;
if (!error_message) {
@@ -135,9 +184,15 @@ static void matrix_api_complete(PurpleUtilFetchUrlData *url_data,
root = json_parser_get_root(parser);
}
- (data->callback)(data->account, data->user_data,
- response_code, response_message,
- body_start, root, error_message);
+ if (error_message) {
+ matrix_api_error(data->account, data->user_data, error_message);
+ } else if(response_code >= 300) {
+ matrix_api_bad_response(data->account, data->user_data,
+ response_code, response_message, body_start, root);
+ } else {
+ (data->callback)(data->account, data->user_data,
+ body_start, root);
+ }
/* free the JSON parser, and all of the node structures */
if(parser)
@@ -173,7 +228,7 @@ static PurpleUtilFetchUrlData *matrix_api_start(const gchar *url,
}
-PurpleUtilFetchUrlData *matrix_sync(MatrixAccount *account,
+PurpleUtilFetchUrlData *matrix_api_sync(MatrixAccount *account,
const gchar *since,
MatrixApiCallback callback,
gpointer user_data)
diff --git a/matrix-api.h b/matrix-api.h
index 85adea6..125568d 100644
--- a/matrix-api.h
+++ b/matrix-api.h
@@ -33,25 +33,34 @@ struct _JsonNode;
* @param account The MatrixAccount passed into the api method
* @param user_data The user data that your code passed into the api
* method.
- * @param http_response -1 on error, otherwise this is the HTTP response code.
- * @param http_response_msg NULL on error, otherwise this is the message from
- * the HTTP response line.
- * @param body_start NULL on error, or if there was no body in the response;
+ * @param body_start NULL if there was no body in the response;
* otherwise a pointer to the start of the body in the
* response
- * @param json_root NULL on error, or if there was no body in the response;
- * otherwise the root of the JSON tree in the response
- * @param error_message If something went wrong then this will contain
- * a descriptive error message, and ret_text will be
- * NULL and ret_len will be 0.
+ * @param json_root NULL if there was no body, or it could not be
+ * parsed as JSON; otherwise the root of the JSON
+ * tree in the response
*/
typedef void (*MatrixApiCallback)(MatrixAccount *account,
gpointer user_data,
- int http_response_code,
- const gchar *http_response_msg,
const gchar *body_start,
- struct _JsonNode *json_root,
- const gchar *error_message);
+ struct _JsonNode *json_root);
+
+
+/**
+ * call the /login API
+ *
+ * @param account The MatrixAccount for which to make the request
+ * @param username user id to pass in request
+ * @param password password to pass in request
+ * @param callback Function to be called when the request completes
+ * @param user_data Opaque data to be passed to the callback
+ */
+PurpleUtilFetchUrlData *matrix_api_password_login(MatrixAccount *account,
+ const gchar *username,
+ const gchar *password,
+ MatrixApiCallback callback,
+ gpointer user_data);
+
/**
@@ -62,7 +71,7 @@ typedef void (*MatrixApiCallback)(MatrixAccount *account,
* @param callback Function to be called when the request completes
* @param user_data Opaque data to be passed to the callback
*/
-PurpleUtilFetchUrlData *matrix_sync(MatrixAccount *account,
+PurpleUtilFetchUrlData *matrix_api_sync(MatrixAccount *account,
const gchar *since,
MatrixApiCallback callback,
gpointer user_data);
diff --git a/matrix-sync.c b/matrix-sync.c
index 358f84b..c32d0bc 100644
--- a/matrix-sync.c
+++ b/matrix-sync.c
@@ -39,10 +39,7 @@ typedef struct _RoomEventParserData {
static void matrix_sync_complete(MatrixAccount *ma, gpointer user_data,
- int http_response_code, const gchar *http_response_msg,
- const gchar *body_start, JsonNode *body, const gchar *error_message);
-
-
+ const gchar *body_start, JsonNode *body);
/**
* handle an event for a room
@@ -176,35 +173,13 @@ static void matrix_handle_sync(MatrixAccount *ma, JsonNode *body)
PURPLE_CONNECTION_ERROR_OTHER_ERROR, "No next_batch field");
return;
}
- matrix_sync(ma, next_batch, matrix_sync_complete, NULL);
+ matrix_api_sync(ma, next_batch, matrix_sync_complete, NULL);
}
/* callback which is called when a /sync request completes */
-static void matrix_sync_complete(MatrixAccount *ma,
- gpointer user_data,
- int http_response_code,
- const gchar *http_response_msg,
- const gchar *body_start,
- JsonNode *body,
- const gchar *error_message)
+static void matrix_sync_complete(MatrixAccount *ma, gpointer user_data,
+ const gchar *body_start, JsonNode *body)
{
- if (error_message) {
- purple_debug_info("matrixprpl", "sync gave error %s\n",
- error_message);
- purple_connection_error_reason(ma->pc,
- PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error_message);
- return;
- }
-
- if (http_response_code >= 400) {
- purple_debug_info("matrixprpl", "sync gave response %s: %s\n",
- http_response_msg, body_start);
- purple_connection_error_reason(ma->pc,
- PURPLE_CONNECTION_ERROR_OTHER_ERROR,
- http_response_msg);
- return;
- }
-
purple_debug_info("matrixprpl", "got sync result %s\n", body_start);
purple_connection_update_progress(ma->pc, _("Connected"), 2, 3);
@@ -217,5 +192,5 @@ static void matrix_sync_complete(MatrixAccount *ma,
void matrix_sync_start_loop(MatrixAccount *ma)
{
purple_connection_update_progress(ma->pc, _("Initial Sync"), 1, 3);
- matrix_sync(ma, NULL, matrix_sync_complete, NULL);
+ matrix_api_sync(ma, NULL, matrix_sync_complete, NULL);
}