callServerEndpoint<T> method

  1. @override
Future<T> callServerEndpoint<T>(
  1. String endpoint,
  2. String method,
  3. Map<String, dynamic> args
)
override

Calls a server endpoint method by its name, passing arguments in a map. Typically, this method is called by generated code.

Implementation

@override
Future<T> callServerEndpoint<T>(
    String endpoint, String method, Map<String, dynamic> args) async {
  if (!_initialized) await _initialize();

  try {
    var body =
        formatArgs(args, await authenticationKeyManager?.get(), method);

    var url = Uri.parse('$host$endpoint');

    var request = await _httpClient.postUrl(url);
    request.headers.contentType =
        ContentType('application', 'json', charset: 'utf-8');
    request.contentLength = utf8.encode(body).length;
    request.write(body);

    await request.flush();

    var response = await request.close().timeout(connectionTimeout);

    var data = await _readResponse(response);

    if (response.statusCode != HttpStatus.ok) {
      throw getExceptionFrom(
        data: data,
        serializationManager: serializationManager,
        statusCode: response.statusCode,
      );
    }

    if (T == getType<void>()) {
      return returnVoid() as T;
    } else {
      return parseData<T>(data, T, serializationManager);
    }
  } catch (e) {
    if (logFailedCalls) {
      print('Failed call: $endpoint.$method');
      print('$e');
    }

    rethrow;
  }
}