makeRequest method
Implementation
Future<Object?> makeRequest(
String functionName,
Map<String, Object?> args,
) async {
final Json requestInfo = {
'requestId': _randomBytesHex(16),
'name': functionName,
};
try {
final func = fnTable[functionName]!;
final encodedArgs = args.map(
(argName, argValue) => MapEntry(
argName,
encode(
typeTable,
'$functionName.args.$argName',
func.args[argName]!,
argValue,
),
),
);
requestInfo['args'] = encodedArgs;
final Json body = {
...requestInfo,
'version': 3,
'extra': extra,
'deviceInfo': await getDeviceInfo(await _deviceId())
};
await interceptors.onRequest?.call(body);
final response = await _client.post(
baseUrl,
headers: headers,
body: jsonEncode(body),
);
final responseBody = jsonDecode(utf8.decode(response.bodyBytes));
if (responseBody['error'] != null) {
throw _createError(
responseBody['error']['type'],
responseBody['error']['message'],
responseBody['error']['data'],
requestInfo,
);
} else {
final response = decode(
typeTable, '$functionName.ret', func.ret, responseBody['result']);
await interceptors.onResponse?.call(response);
return response;
}
} catch (e) {
if (e is SdkgenError) {
await interceptors.onError?.call(e);
rethrow;
} else {
final error = _createError('Fatal', e.toString(), null, requestInfo);
await interceptors.onError?.call(error);
throw error;
}
}
}