Added working edit transaction
This commit is contained in:
+122
-31
@@ -1,4 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:helpers/helpers/print.dart';
|
||||
|
||||
class Api {
|
||||
@@ -10,38 +13,40 @@ class Api {
|
||||
_instance.initDone = true;
|
||||
_instance.dio = Dio();
|
||||
_instance.dio.options.baseUrl = "http://localhost:8081/";
|
||||
// _instance.dio.options.baseUrl = "https://rluv.fosscat.com/";
|
||||
_instance.dio.interceptors.add(
|
||||
InterceptorsWrapper(
|
||||
onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
|
||||
// Do something before request is sent.
|
||||
// If you want to resolve the request with custom data,
|
||||
// you can resolve a `Response` using `handler.resolve(response)`.
|
||||
// If you want to reject the request with a error message,
|
||||
// you can reject with a `DioException` using `handler.reject(dioError)`.
|
||||
return handler.next(options);
|
||||
},
|
||||
onResponse: (Response response, ResponseInterceptorHandler handler) {
|
||||
if (response.statusCode != null &&
|
||||
response.statusCode! < 500 &&
|
||||
response.statusCode! >= 400) {
|
||||
return handler.reject(DioException.badResponse(
|
||||
requestOptions: RequestOptions(),
|
||||
response: response,
|
||||
statusCode: response.statusCode!));
|
||||
}
|
||||
// Do something with response data.
|
||||
// If you want to reject the request with a error message,
|
||||
// you can reject a `DioException` object using `handler.reject(dioError)`.
|
||||
return handler.next(response);
|
||||
},
|
||||
onError: (DioException e, ErrorInterceptorHandler handler) {
|
||||
printPink(e);
|
||||
// Do something with response error.
|
||||
// If you want to resolve the request with some custom data,
|
||||
// you can resolve a `Response` object using `handler.resolve(response)`.
|
||||
return handler.next(e);
|
||||
},
|
||||
),
|
||||
LoggingInterceptor(),
|
||||
// InterceptorsWrapper(
|
||||
// onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
|
||||
// // Do something before request is sent.
|
||||
// // If you want to resolve the request with custom data,
|
||||
// // you can resolve a `Response` using `handler.resolve(response)`.
|
||||
// // If you want to reject the request with a error message,
|
||||
// // you can reject with a `DioException` using `handler.reject(dioError)`.
|
||||
// return handler.next(options);
|
||||
// },
|
||||
// onResponse: (Response response, ResponseInterceptorHandler handler) {
|
||||
// if (response.statusCode != null &&
|
||||
// response.statusCode! < 500 &&
|
||||
// response.statusCode! >= 400) {
|
||||
// return handler.reject(DioException.badResponse(
|
||||
// requestOptions: RequestOptions(),
|
||||
// response: response,
|
||||
// statusCode: response.statusCode!));
|
||||
// }
|
||||
// // Do something with response data.
|
||||
// // If you want to reject the request with a error message,
|
||||
// // you can reject a `DioException` object using `handler.reject(dioError)`.
|
||||
// return handler.next(response);
|
||||
// },
|
||||
// onError: (DioException e, ErrorInterceptorHandler handler) {
|
||||
// printPink(e);
|
||||
// // Do something with response error.
|
||||
// // If you want to resolve the request with some custom data,
|
||||
// // you can resolve a `Response` object using `handler.resolve(response)`.
|
||||
// return handler.next(e);
|
||||
// },
|
||||
// ),
|
||||
);
|
||||
return _instance;
|
||||
}
|
||||
@@ -79,6 +84,21 @@ class Api {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> post(
|
||||
{required String path, Object? data}) async {
|
||||
try {
|
||||
final res = await dio.post(path, data: data);
|
||||
|
||||
if (res.data != null) {
|
||||
return res.data as Map<String, dynamic>;
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
printRed('Error in put: $err');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> delete(
|
||||
{required String path, Object? data}) async {
|
||||
try {
|
||||
@@ -94,3 +114,74 @@ class Api {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LoggingInterceptor extends Interceptor {
|
||||
LoggingInterceptor();
|
||||
|
||||
@override
|
||||
Future onRequest(
|
||||
RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
logPrint('///*** REQUEST ***\\\\\\');
|
||||
printKV('URI', options.uri);
|
||||
printKV('METHOD', options.method);
|
||||
logPrint('HEADERS:');
|
||||
options.headers.forEach((key, v) => printKV(' - $key', v));
|
||||
logPrint('BODY:');
|
||||
printJson(options.data);
|
||||
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
logPrint('///*** ERROR RESPONSE ***\\\\\\');
|
||||
logPrint('URI: ${err.requestOptions.uri}');
|
||||
if (err.response != null) {
|
||||
logPrint('STATUS CODE: ${err.response?.statusCode?.toString()}');
|
||||
}
|
||||
logPrint('$err');
|
||||
if (err.response != null) {
|
||||
printKV('REDIRECT', err.response?.realUri ?? '');
|
||||
logPrint('BODY:');
|
||||
printJson(err.response?.data);
|
||||
}
|
||||
return handler.next(err);
|
||||
}
|
||||
|
||||
@override
|
||||
Future onResponse(
|
||||
Response response, ResponseInterceptorHandler handler) async {
|
||||
logPrint('///*** RESPONSE ***\\\\\\');
|
||||
printKV('URI', response.requestOptions.uri);
|
||||
printKV('STATUS CODE', response.statusCode ?? '');
|
||||
// printKV('REDIRECT', response.isRedirect);
|
||||
logPrint('BODY:');
|
||||
printJson(response.data);
|
||||
|
||||
return handler.next(response);
|
||||
}
|
||||
|
||||
void printKV(String key, Object v) {
|
||||
if (kDebugMode) {
|
||||
printOrange('$key: $v');
|
||||
}
|
||||
}
|
||||
|
||||
void printJson(Map<String, dynamic>? s) {
|
||||
if (kDebugMode) {
|
||||
if (s == null) {
|
||||
printAmber({});
|
||||
return;
|
||||
}
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyprint = encoder.convert(s);
|
||||
printAmber(prettyprint);
|
||||
}
|
||||
}
|
||||
|
||||
void logPrint(String s) {
|
||||
if (kDebugMode) {
|
||||
printOrange(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user