Add debug logs
This commit is contained in:
parent
b3bb4e6531
commit
502623ad31
|
@ -38,7 +38,7 @@ class Client {
|
||||||
/// Optional persistent store for all data.
|
/// Optional persistent store for all data.
|
||||||
Store store;
|
Store store;
|
||||||
|
|
||||||
Client(this.clientName) {
|
Client(this.clientName, {this.debug = false}) {
|
||||||
connection = Connection(this);
|
connection = Connection(this);
|
||||||
|
|
||||||
if (this.clientName != "testclient") store = Store(this);
|
if (this.clientName != "testclient") store = Store(this);
|
||||||
|
@ -47,6 +47,9 @@ class Client {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether debug prints should be displayed.
|
||||||
|
final bool debug;
|
||||||
|
|
||||||
/// The required name for this client.
|
/// The required name for this client.
|
||||||
final String clientName;
|
final String clientName;
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ class Connection {
|
||||||
|
|
||||||
/// Outside of rooms there are account updates like account_data or presences.
|
/// Outside of rooms there are account updates like account_data or presences.
|
||||||
final StreamController<UserUpdate> onUserEvent =
|
final StreamController<UserUpdate> onUserEvent =
|
||||||
new StreamController.broadcast();
|
new StreamController.broadcast();
|
||||||
|
|
||||||
/// Called when the login state e.g. user gets logged out.
|
/// Called when the login state e.g. user gets logged out.
|
||||||
final StreamController<LoginState> onLoginStateChanged =
|
final StreamController<LoginState> onLoginStateChanged =
|
||||||
|
@ -187,6 +187,8 @@ class Connection {
|
||||||
if (client.isLogged())
|
if (client.isLogged())
|
||||||
headers["Authorization"] = "Bearer ${client.accessToken}";
|
headers["Authorization"] = "Bearer ${client.accessToken}";
|
||||||
|
|
||||||
|
if (client.debug) print("[REQUEST $type] Action: $action, Data: $data");
|
||||||
|
|
||||||
http.Response resp;
|
http.Response resp;
|
||||||
try {
|
try {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -212,12 +214,15 @@ class Connection {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} on TimeoutException catch (_) {
|
} on TimeoutException catch (_) {
|
||||||
|
|
||||||
return ErrorResponse(
|
return ErrorResponse(
|
||||||
error: "No connection possible...", errcode: "TIMEOUT", request: resp?.request);
|
error: "No connection possible...",
|
||||||
|
errcode: "TIMEOUT",
|
||||||
|
request: resp?.request);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return ErrorResponse(
|
return ErrorResponse(
|
||||||
error: "No connection possible...", errcode: "NO_CONNECTION", request: resp.request);
|
error: "No connection possible...",
|
||||||
|
errcode: "NO_CONNECTION",
|
||||||
|
request: resp.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> jsonResp;
|
Map<String, dynamic> jsonResp;
|
||||||
|
@ -225,13 +230,17 @@ class Connection {
|
||||||
jsonResp = jsonDecode(resp.body) as Map<String, dynamic>;
|
jsonResp = jsonDecode(resp.body) as Map<String, dynamic>;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return ErrorResponse(
|
return ErrorResponse(
|
||||||
error: "No connection possible...", errcode: "MALFORMED", request: resp.request);
|
error: "No connection possible...",
|
||||||
|
errcode: "MALFORMED",
|
||||||
|
request: resp.request);
|
||||||
}
|
}
|
||||||
if (jsonResp.containsKey("errcode") && jsonResp["errcode"] is String) {
|
if (jsonResp.containsKey("errcode") && jsonResp["errcode"] is String) {
|
||||||
if (jsonResp["errcode"] == "M_UNKNOWN_TOKEN") clear();
|
if (jsonResp["errcode"] == "M_UNKNOWN_TOKEN") clear();
|
||||||
return ErrorResponse.fromJson(jsonResp, resp.request);
|
return ErrorResponse.fromJson(jsonResp, resp.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (client.debug) print("[RESPONSE] ${jsonResp.toString()}");
|
||||||
|
|
||||||
return jsonResp;
|
return jsonResp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,8 +348,7 @@ class Connection {
|
||||||
|
|
||||||
if (room["invite_state"] is Map<String, dynamic> &&
|
if (room["invite_state"] is Map<String, dynamic> &&
|
||||||
room["invite_state"]["events"] is List<dynamic>)
|
room["invite_state"]["events"] is List<dynamic>)
|
||||||
_handleRoomEvents(
|
_handleRoomEvents(id, room["invite_state"]["events"], "invite_state");
|
||||||
id, room["invite_state"]["events"], "invite_state");
|
|
||||||
|
|
||||||
if (room["timeline"] is Map<String, dynamic> &&
|
if (room["timeline"] is Map<String, dynamic> &&
|
||||||
room["timeline"]["events"] is List<dynamic>)
|
room["timeline"]["events"] is List<dynamic>)
|
||||||
|
@ -352,8 +360,7 @@ class Connection {
|
||||||
|
|
||||||
if (room["account_data"] is Map<String, dynamic> &&
|
if (room["account_data"] is Map<String, dynamic> &&
|
||||||
room["account_data"]["events"] is List<dynamic>)
|
room["account_data"]["events"] is List<dynamic>)
|
||||||
_handleRoomEvents(
|
_handleRoomEvents(id, room["account_data"]["events"], "account_data");
|
||||||
id, room["account_data"]["events"], "account_data");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,8 +398,7 @@ class Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleRoomEvents(
|
void _handleRoomEvents(String chat_id, List<dynamic> events, String type) {
|
||||||
String chat_id, List<dynamic> events, String type) {
|
|
||||||
for (num i = 0; i < events.length; i++) {
|
for (num i = 0; i < events.length; i++) {
|
||||||
_handleEvent(events[i], chat_id, type);
|
_handleEvent(events[i], chat_id, type);
|
||||||
}
|
}
|
||||||
|
@ -411,8 +417,7 @@ class Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleEvent(
|
void _handleEvent(Map<String, dynamic> event, String roomID, String type) {
|
||||||
Map<String, dynamic> event, String roomID, String type) {
|
|
||||||
if (event["type"] is String && event["content"] is dynamic) {
|
if (event["type"] is String && event["content"] is dynamic) {
|
||||||
EventUpdate update = EventUpdate(
|
EventUpdate update = EventUpdate(
|
||||||
eventType: event["type"],
|
eventType: event["type"],
|
||||||
|
|
Loading…
Reference in a new issue