Skip to content

Instantly share code, notes, and snippets.

@ashishrawat2911
Created July 10, 2020 15:35
Show Gist options
  • Save ashishrawat2911/74416e528ae46bf25a81825c9a8c1c23 to your computer and use it in GitHub Desktop.
Save ashishrawat2911/74416e528ae46bf25a81825c9a8c1c23 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
const _defaultConnectTimeout = Duration.millisecondsPerMinute;
const _defaultReceiveTimeout = Duration.millisecondsPerMinute;
class DioClient {
final String baseUrl;
Dio _dio;
final List<Interceptor> interceptors;
DioClient(
this.baseUrl,
Dio dio, {
this.interceptors,
}) {
_dio = dio ?? Dio();
_dio
..options.baseUrl = baseUrl
..options.connectTimeout = _defaultConnectTimeout
..options.receiveTimeout = _defaultReceiveTimeout
..httpClientAdapter
..options.headers = {'Content-Type': 'application/json; charset=UTF-8'};
if (interceptors?.isNotEmpty ?? false) {
_dio.interceptors.addAll(interceptors);
}
if (kDebugMode) {
_dio.interceptors.add(LogInterceptor(
responseBody: true,
error: true,
requestHeader: false,
responseHeader: false,
request: false,
requestBody: false));
}
}
Future<dynamic> get(
String uri, {
Map<String, dynamic> queryParameters,
Options options,
CancelToken cancelToken,
ProgressCallback onReceiveProgress,
}) async {
try {
var response = await _dio.get(
uri,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);
return response.data;
} on SocketException catch (e) {
throw SocketException(e.toString());
} on FormatException catch (_) {
throw FormatException("Unable to process the data");
} catch (e) {
throw e;
}
}
Future<dynamic> post(
String uri, {
data,
Map<String, dynamic> queryParameters,
Options options,
CancelToken cancelToken,
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
}) async {
try {
var response = await _dio.post(
uri,
data: data,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
return response.data;
} on FormatException catch (_) {
throw FormatException("Unable to process the data");
} catch (e) {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment