authProxyRequest<TBodyType, TResponseType> method

Future<TResponseType> authProxyRequest<TBodyType, TResponseType>(
  1. String url,
  2. TBodyType body,
  3. TResponseType fromJson(
    1. Map<String, dynamic>
    )
)

Implementation

Future<TResponseType> authProxyRequest<TBodyType, TResponseType>(
  String url,
  TBodyType body,
  TResponseType Function(Map<String, dynamic>) fromJson,
) async {
  if (config.authProxyConfigId == null || config.authProxyConfigId!.isEmpty) {
    throw Exception(
        'Missing Auth Proxy config ID. Please verify environment variables.');
  }
  final fullUrl = '${config.authProxyBaseUrl}$url';
  final stringifiedBody = jsonEncode(body);

  final client = HttpClient();
  try {
    final request = await client.postUrl(Uri.parse(fullUrl));
    request.headers.set("X-Auth-Proxy-Config-ID", config.authProxyConfigId!);
    request.headers.set('X-Client-Version', VERSION);
    request.headers.contentType = ContentType.json;
    request.write(stringifiedBody);

    final response = await request.close();

    if (response.statusCode != 200) {
      final errorBody = await response.transform(utf8.decoder).join();
      throw TurnkeyRequestError(
        GrpcStatus.fromJson(jsonDecode(errorBody)),
      );
    }

    final responseBody = await response.transform(utf8.decoder).join();
    final decodedJson = jsonDecode(responseBody) as Map<String, dynamic>;

    return fromJson(decodedJson);
  } finally {
    client.close();
  }
}