Skip to content

Instantly share code, notes, and snippets.

@vijayinyoutube
Created June 4, 2022 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayinyoutube/81d827c0956a21b1a34fe77d57d1d110 to your computer and use it in GitHub Desktop.
Save vijayinyoutube/81d827c0956a21b1a34fe77d57d1d110 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../../Business_Logic/Exceptions/exception_handlers.dart';
class BaseClient {
static const int timeOutDuration = 35;
//GET
Future<dynamic> get(String url) async {
var uri = Uri.parse(url);
try {
var response =
await http.get(uri).timeout(const Duration(seconds: timeOutDuration));
return _processResponse(response);
} catch (e) {
throw ExceptionHandlers().getExceptionString(e);
}
}
//POST
//DELETE
//OTHERS
//----------------------ERROR STATUS CODES----------------------
dynamic _processResponse(http.Response response) {
switch (response.statusCode) {
case 200:
var responseJson = response.body;
return responseJson;
case 400: //Bad request
throw BadRequestException(jsonDecode(response.body)['message']);
case 401: //Unauthorized
throw UnAuthorizedException(jsonDecode(response.body)['message']);
case 403: //Forbidden
throw UnAuthorizedException(jsonDecode(response.body)['message']);
case 404: //Resource Not Found
throw NotFoundException(jsonDecode(response.body)['message']);
case 500: //Internal Server Error
default:
throw FetchDataException(
'Something went wrong! ${response.statusCode}');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment