Fixed login and error handling

This commit is contained in:
Anton Stubenbord
2022-12-30 01:28:43 +01:00
parent bf0e186646
commit 2326c9d1d6
43 changed files with 502 additions and 447 deletions

View File

@@ -0,0 +1,21 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/models.dart';
class IdQueryParameterJsonConverter
extends JsonConverter<IdQueryParameter, Map<String, dynamic>> {
const IdQueryParameterJsonConverter();
static const _idKey = "id";
static const _assignmentStatusKey = 'assignmentStatus';
@override
IdQueryParameter fromJson(Map<String, dynamic> json) {
return IdQueryParameter(json[_assignmentStatusKey], json[_idKey]);
}
@override
Map<String, dynamic> toJson(IdQueryParameter object) {
return {
_idKey: object.id,
_assignmentStatusKey: object.assignmentStatus,
};
}
}

View File

@@ -1,6 +1,8 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/paperless_api.dart';
@JsonSerializable()
class DocumentFilter extends Equatable {
static const _oneDay = Duration(days: 1);
static const DocumentFilter initial = DocumentFilter();

View File

@@ -1,8 +1,10 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/document_model.dart';
const pageRegex = r".*page=(\d+).*";
//Todo: make this an interface and delegate serialization to implementations
class PagedSearchResultJsonSerializer<T> {
final Map<String, dynamic> json;
final T Function(Map<String, dynamic>) fromJson;
@@ -10,6 +12,7 @@ class PagedSearchResultJsonSerializer<T> {
PagedSearchResultJsonSerializer(this.json, this.fromJson);
}
@JsonSerializable()
class PagedSearchResult<T> extends Equatable {
/// Total number of available items
final int count;

View File

@@ -1,9 +1,17 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/converters/id_query_parameter_json_converter.dart';
@IdQueryParameterJsonConverter()
@JsonSerializable()
class IdQueryParameter extends Equatable {
final int? _assignmentStatus;
final int? _id;
@Deprecated("Use named constructors, this is only meant for code generation")
const IdQueryParameter(this._assignmentStatus, this._id);
const IdQueryParameter.notAssigned()
: _assignmentStatus = 1,
_id = null;
@@ -28,6 +36,9 @@ class IdQueryParameter extends Equatable {
int? get id => _id;
@visibleForTesting
int? get assignmentStatus => _assignmentStatus;
Map<String, String> toQueryParameter(String field) {
final Map<String, String> params = {};
if (onlyNotAssigned || onlyAssigned) {

View File

@@ -12,6 +12,7 @@ class PaperlessAuthenticationApiImpl implements PaperlessAuthenticationApi {
required String username,
required String password,
}) async {
print(client.hashCode);
late Response response;
try {
response = await client.post(
@@ -21,27 +22,19 @@ class PaperlessAuthenticationApiImpl implements PaperlessAuthenticationApi {
"password": password,
},
);
} on FormatException catch (e) {
final source = e.source;
if (source is String &&
source.contains("400 No required SSL certificate was sent")) {
} on DioError catch (error) {
if (error.error is ErrorCode) {
throw PaperlessServerException(
ErrorCode.missingClientCertificate,
httpStatusCode: response.statusCode,
error.error,
httpStatusCode: error.response?.statusCode,
);
} else {
throw error.error;
}
}
if (response.statusCode == 200) {
return response.data['token'];
} else if (response.statusCode == 400 &&
response
.data //TODO: Check if text is included in statusMessage instead of body
.toLowerCase()
.contains("no required certificate was sent")) {
throw PaperlessServerException(
ErrorCode.invalidClientCertificateConfiguration,
httpStatusCode: response.statusCode,
);
} else {
throw PaperlessServerException(
ErrorCode.authenticationFailed,

View File

@@ -15,12 +15,19 @@ class PaperlessDocumentsApiImpl implements PaperlessDocumentsApi {
Uint8List documentBytes, {
required String filename,
required String title,
String contentType = 'application/octet-stream',
DateTime? createdAt,
int? documentType,
int? correspondent,
Iterable<int> tags = const [],
}) async {
final formData = FormData();
final formData = FormData()
..files.add(
MapEntry(
'document',
MultipartFile.fromBytes(documentBytes, filename: filename),
),
);
formData.fields.add(MapEntry('title', title));
if (createdAt != null) {
@@ -35,6 +42,7 @@ class PaperlessDocumentsApiImpl implements PaperlessDocumentsApi {
for (final tag in tags) {
formData.fields.add(MapEntry('tags', tag.toString()));
}
final response =
await client.post('/api/documents/post_document/', data: formData);
if (response.statusCode != 200) {