refactor: upgrade to http 0.13

This commit is contained in:
Lukas Lihotzki 2021-03-30 14:29:24 +02:00
parent af693700fb
commit 04674e2dfd
2 changed files with 40 additions and 46 deletions

View File

@ -139,7 +139,7 @@ class MatrixApi {
dynamic data = '', dynamic data = '',
int timeout, int timeout,
String contentType = 'application/json', String contentType = 'application/json',
Map<String, String> query, Map<String, dynamic> query,
}) async { }) async {
if (homeserver == null) { if (homeserver == null) {
throw ('No homeserver specified.'); throw ('No homeserver specified.');
@ -149,13 +149,8 @@ class MatrixApi {
(!(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;
final queryPart = query?.entries final url = homeserver
?.where((x) => x.value != null) .resolveUri(Uri(path: '_matrix$action', queryParameters: query));
?.map((x) => [x.key, x.value].map(Uri.encodeQueryComponent).join('='))
?.join('&');
final url = ['${homeserver.toString()}/_matrix$action', queryPart]
.where((x) => x != null && x != '')
.join('?');
var headers = <String, String>{}; var headers = <String, String>{};
if (type == RequestType.PUT || type == RequestType.POST) { if (type == RequestType.PUT || type == RequestType.POST) {
@ -235,11 +230,8 @@ class MatrixApi {
/// Gets discovery information about the domain. The file may include additional keys. /// Gets discovery information about the domain. The file may include additional keys.
/// https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client /// https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client
Future<WellKnownInformation> requestWellKnownInformation() async { Future<WellKnownInformation> requestWellKnownInformation() async {
var baseUrl = homeserver.toString(); final response =
if (baseUrl.endsWith('/')) { await httpClient.get(homeserver.resolve('.well-known/matrix/client'));
baseUrl = baseUrl.substring(0, baseUrl.length - 1);
}
final response = await httpClient.get('$baseUrl/.well-known/matrix/client');
final rawJson = json.decode(response.body); final rawJson = json.decode(response.body);
return WellKnownInformation.fromJson(rawJson); return WellKnownInformation.fromJson(rawJson);
} }
@ -332,9 +324,10 @@ class MatrixApi {
AuthenticationData auth, AuthenticationData auth,
String kind, String kind,
}) async { }) async {
var action = '/client/r0/register'; final response =
if (kind != null) action += '?kind=${Uri.encodeQueryComponent(kind)}'; await request(RequestType.POST, '/client/r0/register', query: {
final response = await request(RequestType.POST, action, data: { if (kind != null) 'kind': kind,
}, data: {
if (username != null) 'username': username, if (username != null) 'username': username,
if (password != null) 'password': password, if (password != null) 'password': password,
if (deviceId != null) 'device_id': deviceId, if (deviceId != null) 'device_id': deviceId,
@ -483,7 +476,10 @@ class MatrixApi {
Future<bool> usernameAvailable(String username) async { Future<bool> usernameAvailable(String username) async {
final response = await request( final response = await request(
RequestType.GET, RequestType.GET,
'/client/r0/register/available?username=$username', '/client/r0/register/available',
query: {
'username': username,
},
); );
return response['available']; return response['available'];
} }
@ -970,16 +966,10 @@ class MatrixApi {
String thirdPidSignedToken, String thirdPidSignedToken,
Map<String, dynamic> thirdPidSignedSiganture, Map<String, dynamic> thirdPidSignedSiganture,
}) async { }) async {
var action = '/client/r0/join/${Uri.encodeComponent(roomIdOrAlias)}';
final queryPart = servers
?.map((x) => 'server_name=${Uri.encodeQueryComponent(x)}')
?.join('&');
if (queryPart != null && queryPart != '') {
action += '?' + queryPart;
}
final response = await request( final response = await request(
RequestType.POST, RequestType.POST,
action, '/client/r0/join/${Uri.encodeComponent(roomIdOrAlias)}',
query: {'server_name': servers},
data: { data: {
if (thirdPidSignedSiganture != null) if (thirdPidSignedSiganture != null)
'third_party_signed': { 'third_party_signed': {
@ -1307,9 +1297,11 @@ class MatrixApi {
contentType ?? lookupMimeType(fileName, headerBytes: file); contentType ?? lookupMimeType(fileName, headerBytes: file);
headers['Content-Length'] = length.toString(); headers['Content-Length'] = length.toString();
fileName = Uri.encodeQueryComponent(fileName); fileName = Uri.encodeQueryComponent(fileName);
final url = final url = homeserver.resolveUri(Uri(
'${homeserver.toString()}/_matrix/media/r0/upload?filename=$fileName'; path: '_matrix/media/r0/upload',
final streamedRequest = http.StreamedRequest('POST', Uri.parse(url)) queryParameters: {'filename': fileName},
));
final streamedRequest = http.StreamedRequest('POST', url)
..headers.addAll(headers); ..headers.addAll(headers);
streamedRequest.contentLength = length; streamedRequest.contentLength = length;
streamedRequest.sink.add(file); streamedRequest.sink.add(file);
@ -1333,11 +1325,11 @@ class MatrixApi {
/// URL in a message and wants to render a preview for the user. /// URL in a message and wants to render a preview for the user.
/// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-media-r0-preview-url /// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-media-r0-preview-url
Future<OpenGraphData> requestOpenGraphDataForUrl(Uri url, {int ts}) async { Future<OpenGraphData> requestOpenGraphDataForUrl(Uri url, {int ts}) async {
var action = var action = homeserver
'${homeserver.toString()}/_matrix/media/r0/preview_url?url=${Uri.encodeQueryComponent(url.toString())}'; .resolveUri(Uri(path: '_matrix/media/r0/preview_url', queryParameters: {
if (ts != null) { 'url': url.toString(),
action += '&ts=${Uri.encodeQueryComponent(ts.toString())}'; if (ts != null) 'ts': ts.toString(),
} }));
final response = await httpClient.get(action); final response = await httpClient.get(action);
final rawJson = json.decode(response.body.isEmpty ? '{}' : response.body); final rawJson = json.decode(response.body.isEmpty ? '{}' : response.body);
return OpenGraphData.fromJson(rawJson); return OpenGraphData.fromJson(rawJson);
@ -1346,7 +1338,7 @@ class MatrixApi {
/// This endpoint allows clients to retrieve the configuration of the content repository, such as upload limitations. /// This endpoint allows clients to retrieve the configuration of the content repository, such as upload limitations.
/// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-media-r0-config /// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-media-r0-config
Future<int> requestMaxUploadSize() async { Future<int> requestMaxUploadSize() async {
var action = '${homeserver.toString()}/_matrix/media/r0/config'; var action = homeserver.resolve('_matrix/media/r0/config');
final response = await httpClient.get(action); final response = await httpClient.get(action);
final rawJson = json.decode(response.body.isEmpty ? '{}' : response.body); final rawJson = json.decode(response.body.isEmpty ? '{}' : response.body);
return rawJson['m.upload.size']; return rawJson['m.upload.size'];
@ -1485,10 +1477,11 @@ class MatrixApi {
/// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload /// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload
Future<DeviceListsUpdate> requestDeviceListsUpdate( Future<DeviceListsUpdate> requestDeviceListsUpdate(
String from, String to) async { String from, String to) async {
final response = await request( final response =
RequestType.GET, await request(RequestType.GET, '/client/r0/keys/changes', query: {
'/client/r0/keys/changes?from=${Uri.encodeQueryComponent(from)}&to=${Uri.encodeQueryComponent(to)}', 'from': from,
); 'to': to,
});
return DeviceListsUpdate.fromJson(response); return DeviceListsUpdate.fromJson(response);
} }
@ -1946,9 +1939,10 @@ class MatrixApi {
Future<List<ThirdPartyLocation>> requestThirdPartyLocationsByAlias( Future<List<ThirdPartyLocation>> requestThirdPartyLocationsByAlias(
String alias) async { String alias) async {
final response = await request( final response = await request(
RequestType.GET, RequestType.GET, '/client/r0/thirdparty/location',
'/client/r0/thirdparty/location?alias=${Uri.encodeComponent(alias)}', query: {
); 'alias': alias,
});
return (response['chunk'] as List) return (response['chunk'] as List)
.map((i) => ThirdPartyLocation.fromJson(i)) .map((i) => ThirdPartyLocation.fromJson(i))
.toList(); .toList();
@ -1958,10 +1952,10 @@ class MatrixApi {
/// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-thirdparty-user /// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-thirdparty-user
Future<List<ThirdPartyUser>> requestThirdPartyUsersByUserId( Future<List<ThirdPartyUser>> requestThirdPartyUsersByUserId(
String userId) async { String userId) async {
final response = await request( final response =
RequestType.GET, await request(RequestType.GET, '/client/r0/thirdparty/user', query: {
'/client/r0/thirdparty/user?userid=${Uri.encodeComponent(userId)}', 'userid': userId,
); });
return (response['chunk'] as List) return (response['chunk'] as List)
.map((i) => ThirdPartyUser.fromJson(i)) .map((i) => ThirdPartyUser.fromJson(i))
.toList(); .toList();

View File

@ -7,7 +7,7 @@ environment:
sdk: ">=2.10.0 <3.0.0" sdk: ">=2.10.0 <3.0.0"
dependencies: dependencies:
http: ^0.12.2 http: ^0.13.0
logger: ^0.9.4 logger: ^0.9.4
mime: ^1.0.0 mime: ^1.0.0