Externalized API and models as own package

This commit is contained in:
Anton Stubenbord
2022-12-02 01:48:13 +01:00
parent 60d1a2e62a
commit ec7707e4a4
143 changed files with 1496 additions and 1339 deletions

View File

@@ -1,35 +1,32 @@
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:paperless_mobile/features/login/bloc/authentication_cubit.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/store/local_vault.dart';
import 'package:http_interceptor/http_interceptor.dart';
import 'package:injectable/injectable.dart';
@injectable
class AuthenticationInterceptor implements InterceptorContract {
AuthenticationCubit authenticationCubit;
AuthenticationInterceptor(this.authenticationCubit);
final LocalVault _localVault;
AuthenticationInterceptor(this._localVault);
@override
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
final authState = authenticationCubit.state;
final auth = await _localVault.loadAuthenticationInformation();
if (kDebugMode) {
log("Intercepted ${request.method} request to ${request.url.toString()}");
}
if (authState.authentication == null) {
throw const ErrorMessage(ErrorCode.notAuthenticated);
if (auth == null) {
throw const PaperlessServerException(ErrorCode.notAuthenticated);
}
return request.copyWith(
//Append server Url
url: Uri.parse(
authState.authentication!.serverUrl + request.url.toString()),
headers: authState.authentication!.token.isEmpty
url: Uri.parse(auth.serverUrl + request.url.toString()),
headers: auth.token.isEmpty
? request.headers
: {
...request.headers,
'Authorization': 'Token ${authState.authentication!.token}'
},
: {...request.headers, 'Authorization': 'Token ${auth.token}'},
);
}