WIP - Provider refactorings

This commit is contained in:
Anton Stubenbord
2022-12-27 00:12:33 +01:00
parent 60aecb549d
commit bf0e186646
53 changed files with 434 additions and 416 deletions

View File

@@ -2,27 +2,25 @@ import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:http_interceptor/http_interceptor.dart';
import 'package:paperless_mobile/core/store/local_vault.dart';
class AuthenticationInterceptor implements InterceptorContract {
final LocalVault _localVault;
AuthenticationInterceptor(this._localVault);
String? serverUrl;
String? token;
AuthenticationInterceptor({this.serverUrl, this.token});
@override
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
final auth = await _localVault.loadAuthenticationInformation();
if (kDebugMode) {
log("Intercepted ${request.method} request to ${request.url.toString()}");
}
return request.copyWith(
//Append server Url
headers: auth?.token?.isEmpty ?? true
url: Uri.parse((serverUrl ?? '') + request.url.toString()),
headers: token?.isEmpty ?? true
? request.headers
: {
...request.headers,
'Authorization': 'Token ${auth!.token}',
'Authorization': 'Token $token',
},
);
}

View File

@@ -0,0 +1,38 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:paperless_mobile/core/type/types.dart';
class DioHttpErrorInterceptor implements InterceptorsWrapper {
@override
void onError(DioError e, ErrorInterceptorHandler handler) {
//TODO: Implement and debug how error handling works, or if request has to be resolved.
if (e.response?.statusCode == 400) {
// try to parse contained error message, otherwise return response
final Map<String, dynamic> json = jsonDecode(e.response?.data);
final PaperlessValidationErrors errorMessages = {};
for (final entry in json.entries) {
if (entry.value is List) {
errorMessages.putIfAbsent(
entry.key, () => (entry.value as List).cast<String>().first);
} else if (entry.value is String) {
errorMessages.putIfAbsent(entry.key, () => entry.value);
} else {
errorMessages.putIfAbsent(entry.key, () => entry.value.toString());
}
}
throw errorMessages;
}
handler.next(e);
}
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
handler.next(options);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
handler.next(response);
}
}