Skip to content

Instantly share code, notes, and snippets.

@xsahil03x
Last active March 19, 2020 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsahil03x/887625bfb628ac7ae3c7e922330f342a to your computer and use it in GitHub Desktop.
Save xsahil03x/887625bfb628ac7ae3c7e922330f342a to your computer and use it in GitHub Desktop.
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