requestWithTargetedResponseCode static method

Future Function() requestWithTargetedResponseCode(
  1. Future<ApiResponse> request(),
  2. List<int> targettedResponseCodes
)

Implementation

static Future<dynamic> Function() requestWithTargetedResponseCode(
    Future<ApiResponse> Function() request,
    List<int> targettedResponseCodes) {
  return () async {
    try {
      var response = await request();
      if (targettedResponseCodes.contains(response.statusCode)) {
        // the response met the targetted response code
        return;
      } else {
        throw Exception('Invalid response code: ${response.statusCode}');
      }
    } catch (e) {
      rethrow;
    }
  };
}