Created
June 4, 2022 17:42
-
-
Save vijayinyoutube/81d827c0956a21b1a34fe77d57d1d110 to your computer and use it in GitHub Desktop.
This file contains 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
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