postWithDio function

FutureOr postWithDio(
  1. String endpoint, {
  2. Map<String, dynamic>? body,
  3. Map<String, String>? headers,
  4. Duration timeoutDuration = const Duration(seconds: 60),
})

Implementation

FutureOr<dynamic> postWithDio(String endpoint,
    {Map<String, dynamic>? body,
    Map<String, String>? headers,
    Duration timeoutDuration = const Duration(seconds: 60)}) async {
  final dio = _configureDio(timeoutDuration: timeoutDuration);
  await initBinoxusPayEnvs();
  final baseUrl = binoxusPayEnvSingleton.baseUrl;
  final url = "$baseUrl$endpoint";

  try {
    final response =
        await dio.post(url, data: body, options: Options(headers: headers));

    if (response.statusCode == 200 || response.statusCode == 201) {
      return response.data;
    } else {
      throw Exception('Error : ${response.statusCode}');
    }
  } on DioException catch (e) {
    if (e.type == DioExceptionType.connectionTimeout ||
        e.type == DioExceptionType.receiveTimeout) {
      throw Exception('Request to $endpoint timed out');
    } else {
      throw Exception('Request failed: ${e.message}');
    }
  }
}