Created
November 2, 2024 23:47
-
-
Save oyewalekehinde/623375425fd3c557adee0e747ad53a2f to your computer and use it in GitHub Desktop.
httpservice
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 HttpService { | |
| Dio? _dio; | |
| final String baseUrl; | |
| final bool hasAuthorization; | |
| final bool isFormType; | |
| HttpService( | |
| {required this.baseUrl, | |
| this.hasAuthorization = false, | |
| this.isFormType = false}) { | |
| _dio = Dio(BaseOptions( | |
| baseUrl: baseUrl, | |
| connectTimeout: const Duration(seconds: 10), | |
| receiveTimeout: const Duration(seconds: 8), | |
| )); | |
| _interceptorsInit(); | |
| } | |
| static const int timeoutDuration = 1; | |
| Future<Response> getRequest( | |
| urlEndPoint, { | |
| Map<String, dynamic>? queryParameters, | |
| }) async { | |
| Response response; | |
| if (kDebugMode) log(urlEndPoint); | |
| response = await _dio! | |
| .get(urlEndPoint, queryParameters: queryParameters) | |
| .timeout(const Duration(minutes: timeoutDuration)); | |
| if(response.statusCode==401){ | |
| } | |
| return response; | |
| } | |
| Future<Response> post( | |
| urlEndpoint, { | |
| data, | |
| Map<String, dynamic>? queryParameters, | |
| }) async { | |
| Response response; | |
| if (kDebugMode) log(urlEndpoint.toString()); | |
| response = await _dio! | |
| .post(urlEndpoint, data: data, queryParameters: queryParameters) | |
| .timeout(const Duration(minutes: timeoutDuration)); | |
| return response; | |
| } | |
| Future<Response> put(urlEndpoint, | |
| {data, Map<String, dynamic>? queryParameters}) async { | |
| Response response; | |
| response = await _dio! | |
| .put(urlEndpoint, data: data, queryParameters: queryParameters) | |
| .timeout(const Duration(minutes: timeoutDuration)); | |
| return response; | |
| } | |
| Future<Response> delete(urlEndpoint, | |
| {data, Map<String, dynamic>? queryParameters}) async { | |
| Response response; | |
| response = await _dio! | |
| .delete(urlEndpoint, data: data, queryParameters: queryParameters) | |
| .timeout(const Duration(minutes: timeoutDuration)); | |
| return response; | |
| } | |
| Future<Response> patch(urlEndpoint, | |
| {data, Map<String, dynamic>? queryParameters}) async { | |
| Response response; | |
| response = await _dio! | |
| .patch(urlEndpoint, data: data, queryParameters: queryParameters) | |
| .timeout(const Duration(minutes: timeoutDuration)); | |
| return response; | |
| } | |
| _interceptorsInit() { | |
| _dio!.interceptors.add(HeaderInterceptor( | |
| hasToken: hasAuthorization, | |
| dio: _dio!, | |
| contentType: isFormType | |
| ? HeaderContentType.formType | |
| : HeaderContentType.jsonType)); | |
| } | |
| } |
Author
oyewalekehinde
commented
Dec 1, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment