Skip to content

Commit

Permalink
added interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
mirmoktadir committed Mar 28, 2024
1 parent 63d6846 commit 9146655
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/app/service/REST/dio_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

import 'api_header.dart';

class DioClient {
static const int TIME_OUT_DURATION = 10;
BaseOptions baseOptions = BaseOptions(

final _dio = Dio(BaseOptions(
connectTimeout: const Duration(seconds: TIME_OUT_DURATION),
receiveTimeout: const Duration(seconds: TIME_OUT_DURATION),
sendTimeout: const Duration(seconds: TIME_OUT_DURATION),
);
))
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
responseBody: true,
responseHeader: false,
error: true,
compact: true,
maxWidth: 90,
));

//GET

Expand All @@ -23,7 +34,7 @@ class DioClient {
Map<String, dynamic>? params,
}) async {
try {
var response = await Dio(baseOptions).get(url,
var response = await _dio.get(url,
options: Options(headers: Header.rapidApiHeader),
queryParameters: params);

Expand All @@ -39,7 +50,7 @@ class DioClient {
{required String url, Map<String, dynamic>? params, dynamic body}) async {
var payload = json.encode(body);
try {
var response = await Dio(baseOptions).post(url,
var response = await _dio.post(url,
options: Options(headers: Header.defaultHeader),
queryParameters: params,
data: payload);
Expand All @@ -56,7 +67,7 @@ class DioClient {
{required String url, Map<String, dynamic>? params, dynamic body}) async {
var payload = json.encode(body);
try {
var response = await Dio(baseOptions).patch(url,
var response = await _dio.patch(url,
options: Options(headers: Header.defaultHeader),
queryParameters: params,
data: payload);
Expand All @@ -72,7 +83,7 @@ class DioClient {
{required String url, Map<String, dynamic>? params, dynamic body}) async {
var payload = json.encode(body);
try {
var response = await Dio(baseOptions).delete(url,
var response = await _dio.delete(url,
options: Options(headers: Header.defaultHeader),
queryParameters: params,
data: payload);
Expand Down Expand Up @@ -100,7 +111,7 @@ class DioClient {
}

try {
var response = await Dio(baseOptions).post(url,
var response = await _dio.post(url,
options: Options(headers: Header.defaultMultipartHeader),
queryParameters: params,
data: formData);
Expand All @@ -124,7 +135,7 @@ class DioClient {
}

try {
var response = await Dio(baseOptions).post(url,
var response = await _dio.post(url,
options: Options(headers: Header.defaultMultipartHeader),
queryParameters: params,
data: formData);
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
pretty_dio_logger:
dependency: "direct main"
description:
name: pretty_dio_logger
sha256: "00b80053063935cf9a6190da344c5373b9d0e92da4c944c878ff2fbef0ef6dc2"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
pub_semver:
dependency: "direct dev"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies:
image_picker: ^1.0.4
image_cropper: ^5.0.1
web_socket_channel: ^2.4.0
pretty_dio_logger: ^1.3.1


dev_dependencies:
Expand Down

0 comments on commit 9146655

Please sign in to comment.