From 26a86de7fc67663e221cd7ba160f368043da2bec Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Sun, 12 Dec 2021 18:37:11 +0330 Subject: [PATCH] D1APP-22 configuration (network services) --- lib/services/request_handler.dart | 175 ++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 lib/services/request_handler.dart diff --git a/lib/services/request_handler.dart b/lib/services/request_handler.dart new file mode 100644 index 0000000..462ee4d --- /dev/null +++ b/lib/services/request_handler.dart @@ -0,0 +1,175 @@ +import 'dart:convert'; +import 'dart:developer'; +import 'dart:io'; +import 'package:http/http.dart' as http; + +class RequestHandler { + static late String token; + + String? errorMessage; + + dynamic body; + final Map _headers = { + "accept": "*/*", + "Content-Type": "application/json; charset=UTF-8", + }; + final String url; + dynamic _requestBody; + + bool isSuccess = false; + + RequestHandler( + this.url, { + Map? requestHeaders, + body, + bool useAutherization = true, + }) { + if (body != null) _requestBody = body; + if (requestHeaders != null) _headers.addAll(requestHeaders); + if (useAutherization) _headers.addAll({'Authorization': 'Bearer $token'}); + if (body != null) _requestBody = body; + } + + Future httpGet() async { + try { + final response = await http + .get( + Uri.parse(url), + headers: _headers, + ) + .timeout( + const Duration(seconds: 10), + ) + .catchError( + (e) => throw e, + ); + _handleResponse(response); + } catch (e) { + _handleError(null); + } + } + + Future post() async { + try { + final response = await http + .post( + Uri.parse(url), + body: json.encode(_requestBody), + headers: _headers, + ) + .timeout( + const Duration(seconds: 10), + ) + .catchError( + (e) => throw e, + ); + _handleResponse(response); + } catch (e) { + _handleError(null); + } + } + + Future delete() async { + try { + final response = await http + .delete( + Uri.parse(url), + body: json.encode(_requestBody), + headers: _headers, + ) + .timeout( + const Duration(seconds: 10), + ) + .catchError( + (e) => throw e, + ); + _handleResponse(response); + } catch (e) { + _handleError(null); + } + } + + Future multipart(File file) async { + try { + final request = http.MultipartRequest('POST', Uri.parse(url)); + _headers.update('Content-Type', (_) => 'multipart/form-data'); + request.headers.addAll(_headers); + request.files.add( + http.MultipartFile( + 'file', + file.readAsBytes().asStream(), + file.lengthSync(), + filename: 'desc', + ), + ); + final streamedResponse = await request + .send() + .timeout( + const Duration(seconds: 10), + ) + .catchError( + (e) => throw e, + ); + final response = await http.Response.fromStream(streamedResponse); + _handleResponse(response); + } catch (e) { + _handleError(null); + } + } + + Future put() async { + try { + final response = await http + .put( + Uri.parse(url), + body: json.encode(_requestBody), + headers: _headers, + ) + .timeout( + const Duration(seconds: 10), + ) + .catchError( + (e) => throw e, + ); + _handleResponse(response); + } catch (e) { + _handleError(null); + } + } + + void _handleResponse(http.Response? response) { + if (_handleError(response)) { + if (response!.body.isNotEmpty) { + body = json.decode(response.body); + } + } + } + + bool _handleError(http.Response? response) { + if (response == null) { + log( + 'Connecion failed or request timedout! ("$url")', + name: 'Request fail error', + ); + isSuccess = false; + errorMessage = _errorMessageGenerator(response); + return false; + } + if (response.statusCode != 200 && response.statusCode != 204) { + log( + 'Request to [$url] failed with status code ${response.statusCode} & result : \n ${response.body.isNotEmpty ? json.decode(response.body) : 'No result.'}', + name: 'Request fail error', + ); + isSuccess = false; + errorMessage = _errorMessageGenerator(response); + return false; + } + isSuccess = true; + return true; + } + + String _errorMessageGenerator(http.Response? response) { + //TODO: Needs implementation + return ''; + } +}