refactor: remove timeouts in matrix_api_lite

This commit is contained in:
Lukas Lihotzki 2021-06-24 17:37:41 +02:00
parent e3a1b6aa77
commit 1b84de0b44
1 changed files with 5 additions and 29 deletions

View File

@ -70,17 +70,10 @@ class MatrixApi extends Api {
super.unexpectedResponse(response, responseBody); super.unexpectedResponse(response, responseBody);
} }
/// Matrix synchronisation is done with https long polling. This needs a
/// timeout which is usually 30 seconds.
int syncTimeoutSec;
int _timeoutFactor = 1;
MatrixApi({ MatrixApi({
Uri homeserver, Uri homeserver,
String accessToken, String accessToken,
http.Client httpClient, http.Client httpClient,
this.syncTimeoutSec = 30,
}) : super( }) : super(
httpClient: httpClient, httpClient: httpClient,
baseUri: homeserver, baseUri: homeserver,
@ -88,7 +81,7 @@ class MatrixApi extends Api {
/// Used for all Matrix json requests using the [c2s API](https://matrix.org/docs/spec/client_server/r0.6.0.html). /// Used for all Matrix json requests using the [c2s API](https://matrix.org/docs/spec/client_server/r0.6.0.html).
/// ///
/// Throws: TimeoutException, FormatException, MatrixException /// Throws: FormatException, MatrixException
/// ///
/// You must first set [this.homeserver] and for some endpoints also /// You must first set [this.homeserver] and for some endpoints also
/// [this.accessToken] before you can use this! For example to send a /// [this.accessToken] before you can use this! For example to send a
@ -108,14 +101,12 @@ class MatrixApi extends Api {
RequestType type, RequestType type,
String action, { String action, {
dynamic data = '', dynamic data = '',
int timeout,
String contentType = 'application/json', String contentType = 'application/json',
Map<String, dynamic> query, Map<String, dynamic> query,
}) async { }) async {
if (homeserver == null) { if (homeserver == null) {
throw ('No homeserver specified.'); throw ('No homeserver specified.');
} }
timeout ??= (_timeoutFactor * syncTimeoutSec) + 5;
dynamic json; dynamic json;
(!(data is String)) ? json = jsonEncode(data) : json = data; (!(data is String)) ? json = jsonEncode(data) : json = data;
if (data is List<int> || action.startsWith('/media/r0/upload')) json = data; if (data is List<int> || action.startsWith('/media/r0/upload')) json = data;
@ -136,26 +127,16 @@ class MatrixApi extends Api {
try { try {
switch (type) { switch (type) {
case RequestType.GET: case RequestType.GET:
resp = await httpClient.get(url, headers: headers).timeout( resp = await httpClient.get(url, headers: headers);
Duration(seconds: timeout),
);
break; break;
case RequestType.POST: case RequestType.POST:
resp = resp = await httpClient.post(url, body: json, headers: headers);
await httpClient.post(url, body: json, headers: headers).timeout(
Duration(seconds: timeout),
);
break; break;
case RequestType.PUT: case RequestType.PUT:
resp = resp = await httpClient.put(url, body: json, headers: headers);
await httpClient.put(url, body: json, headers: headers).timeout(
Duration(seconds: timeout),
);
break; break;
case RequestType.DELETE: case RequestType.DELETE:
resp = await httpClient.delete(url, headers: headers).timeout( resp = await httpClient.delete(url, headers: headers);
Duration(seconds: timeout),
);
break; break;
} }
var respBody = resp.body; var respBody = resp.body;
@ -173,11 +154,6 @@ class MatrixApi extends Api {
} }
jsonResp = jsonDecode(jsonString) jsonResp = jsonDecode(jsonString)
as Map<String, dynamic>; // May throw FormatException as Map<String, dynamic>; // May throw FormatException
_timeoutFactor = 1;
} on TimeoutException catch (e, s) {
_timeoutFactor *= 2;
throw MatrixConnectionException(e, s);
} catch (e, s) { } catch (e, s) {
throw MatrixConnectionException(e, s); throw MatrixConnectionException(e, s);
} }