Last active
March 19, 2020 17:03
-
-
Save xsahil03x/887625bfb628ac7ae3c7e922330f342a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiBaseHelper { | |
final String _baseUrl = "http://api.themoviedb.org/3/"; | |
Future<dynamic> get(String url) async { | |
var responseJson; | |
try { | |
final response = await http.get(_baseUrl + url); | |
responseJson = _returnResponse(response); | |
} on SocketException { | |
throw FetchDataException('No Internet connection'); | |
} | |
return responseJson; | |
} | |
dynamic _returnResponse(http.Response response) { | |
switch (response.statusCode) { | |
case 200: | |
var responseJson = json.decode(response.body.toString()); | |
print(responseJson); | |
return responseJson; | |
case 400: | |
throw BadRequestException(response.body.toString()); | |
case 401: | |
case 403: | |
throw UnauthorisedException(response.body.toString()); | |
case 500: | |
default: | |
throw FetchDataException( | |
'Error occured while Communication with Server with StatusCode : ${response.statusCode}'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment