1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart';
const String APPLICATION_JSON = "application/json"; const String CONTENT_TYPE = "content-type"; const String ACCEPT = "accept"; const String AUTHORIZATION = "authorization"; const String DEFAULT_LANGUAGE = "en"; const String TOKEN = "Bearer token"; const String BASE_URL = "https://wpapi.ducafecat.tech";
class DioHttpUtil { static final DioHttpUtil _instance = DioHttpUtil._internal();
factory DioHttpUtil() => _instance;
late Dio _dio;
DioHttpUtil._internal() { Map<String, String> headers = { CONTENT_TYPE: APPLICATION_JSON, ACCEPT: APPLICATION_JSON, AUTHORIZATION: TOKEN, DEFAULT_LANGUAGE: DEFAULT_LANGUAGE };
var options = BaseOptions( baseUrl: BASE_URL, headers: headers, connectTimeout: const Duration(seconds: 5), receiveTimeout: const Duration(seconds: 3), responseType: ResponseType.json, );
_dio = Dio(options);
if (!kReleaseMode) { _dio.interceptors.add(PrettyDioLogger( requestHeader: true, requestBody: true, responseHeader: true, )); }
_dio.interceptors.add(RequestInterceptorHandler()); }
Future<Response> get(String url, {Map<String, dynamic>? params, Options? options, CancelToken? cancelToken}) async { Options requestOptions = options ?? Options(); Response response = await _dio.get(url, queryParameters: params, options: requestOptions, cancelToken: cancelToken);
return response; }
Future<Response> post(String url, dynamic data, Options? options, CancelToken? cancelToken) async { Options requestOptions = options ?? Options(); Response response = await _dio.post(url, data: data ?? {}, options: requestOptions, cancelToken: cancelToken); return response; }
Future<Response> put(String url, dynamic data, Options? options, CancelToken? cancelToken) async { Options reqeustOptions = options ?? Options(); Response response = await _dio.put(url, data: data ?? {}, options: reqeustOptions, cancelToken: cancelToken); return response; }
Future<Response> delete(String url, Options? options, dynamic data, CancelToken? cancelToken) async { Options reqeustOptions = options ?? Options(); Response response = await _dio.delete( url, data: data ?? {}, options: reqeustOptions, cancelToken: cancelToken, ); return response; } }
class RequestInterceptorHandler extends Interceptor { @override void onResponse(Response response, ResponseInterceptorHandler handler) { return handler.next(response); }
void next(RequestOptions options) {} }
|