Fixed wrong encodings

This commit is contained in:
Anton Stubenbord
2022-11-01 23:42:43 +01:00
parent b3cb64cad1
commit 2a1b90cc42
5 changed files with 31 additions and 21 deletions

View File

@@ -133,7 +133,7 @@ class DocumentRepositoryImpl implements DocumentRepository {
body: json.encode(doc.toJson()),
headers: {"Content-Type": "application/json"}).timeout(requestTimeout);
if (response.statusCode == 200) {
return DocumentModel.fromJson(jsonDecode(response.body));
return DocumentModel.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
} else {
throw const ErrorMessage(ErrorCode.documentUpdateFailed);
}
@@ -147,7 +147,7 @@ class DocumentRepositoryImpl implements DocumentRepository {
);
if (response.statusCode == 200) {
final searchResult = PagedSearchResult.fromJson(
jsonDecode(const Utf8Decoder().convert(response.body.codeUnits)),
jsonDecode(utf8.decode(response.bodyBytes)),
DocumentModel.fromJson,
);
return searchResult;
@@ -249,7 +249,7 @@ class DocumentRepositoryImpl implements DocumentRepository {
@override
Future<DocumentMetaData> getMetaData(DocumentModel document) async {
final response = await httpClient.get(Uri.parse("/api/documents/${document.id}/metadata/"));
return DocumentMetaData.fromJson(jsonDecode(response.body));
return DocumentMetaData.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
@override
@@ -257,7 +257,7 @@ class DocumentRepositoryImpl implements DocumentRepository {
final response =
await httpClient.get(Uri.parse("/api/search/autocomplete/?query=$query&limit=$limit}"));
if (response.statusCode == 200) {
return json.decode(response.body) as List<String>;
return jsonDecode(utf8.decode(response.bodyBytes)) as List<String>;
}
throw const ErrorMessage(ErrorCode.autocompleteQueryError);
}
@@ -268,7 +268,7 @@ class DocumentRepositoryImpl implements DocumentRepository {
await httpClient.get(Uri.parse("/api/documents/?more_like=$docId&pageSize=10"));
if (response.statusCode == 200) {
return PagedSearchResult<SimilarDocumentModel>.fromJson(
json.decode(response.body),
jsonDecode(utf8.decode(response.bodyBytes)),
SimilarDocumentModel.fromJson,
).results;
}

View File

@@ -37,7 +37,7 @@ class SavedViewRepositoryImpl implements SavedViewsRepository {
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode == 201) {
return SavedView.fromJson(jsonDecode(response.body));
return SavedView.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw ErrorMessage(ErrorCode.createSavedViewError, httpStatusCode: response.statusCode);
}

View File

@@ -75,9 +75,10 @@ class LabelRepositoryImpl implements LabelRepository {
Uri.parse('/api/correspondents/'),
body: json.encode(correspondent.toJson()),
headers: {"Content-Type": "application/json"},
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 201) {
return Correspondent.fromJson(json.decode(response.body));
return Correspondent.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw ErrorMessage(ErrorCode.correspondentCreateFailed, httpStatusCode: response.statusCode);
}
@@ -88,9 +89,10 @@ class LabelRepositoryImpl implements LabelRepository {
Uri.parse('/api/document_types/'),
body: json.encode(type.toJson()),
headers: {"Content-Type": "application/json"},
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 201) {
return DocumentType.fromJson(json.decode(response.body));
return DocumentType.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.documentTypeCreateFailed);
}
@@ -98,12 +100,17 @@ class LabelRepositoryImpl implements LabelRepository {
@override
Future<Tag> saveTag(Tag tag) async {
final body = json.encode(tag.toJson());
final response = await httpClient.post(Uri.parse('/api/tags/'), body: body, headers: {
"Content-Type": "application/json",
"Accept": "application/json; version=2",
});
final response = await httpClient.post(
Uri.parse('/api/tags/'),
body: body,
headers: {
"Content-Type": "application/json",
"Accept": "application/json; version=2",
},
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 201) {
return Tag.fromJson(json.decode(response.body));
return Tag.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.tagCreateFailed);
}
@@ -112,7 +119,7 @@ class LabelRepositoryImpl implements LabelRepository {
Future<int> getStatistics() async {
final response = await httpClient.get(Uri.parse('/api/statistics/'));
if (response.statusCode == 200) {
return json.decode(response.body)['documents_total'];
return jsonDecode(utf8.decode(response.bodyBytes))['documents_total'];
}
throw const ErrorMessage(ErrorCode.unknown);
}
@@ -154,9 +161,10 @@ class LabelRepositoryImpl implements LabelRepository {
Uri.parse('/api/correspondents/${correspondent.id}/'),
headers: {"Content-Type": "application/json"},
body: json.encode(correspondent.toJson()),
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 200) {
return Correspondent.fromJson(json.decode(response.body));
return Correspondent.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.unknown);
}
@@ -168,9 +176,10 @@ class LabelRepositoryImpl implements LabelRepository {
Uri.parse('/api/document_types/${documentType.id}/'),
headers: {"Content-Type": "application/json"},
body: json.encode(documentType.toJson()),
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 200) {
return DocumentType.fromJson(json.decode(response.body));
return DocumentType.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.unknown);
}
@@ -185,9 +194,10 @@ class LabelRepositoryImpl implements LabelRepository {
"Content-Type": "application/json",
},
body: json.encode(tag.toJson()),
encoding: Encoding.getByName("utf-8"),
);
if (response.statusCode == 200) {
return Tag.fromJson(json.decode(response.body));
return Tag.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.unknown);
}
@@ -225,7 +235,7 @@ class LabelRepositoryImpl implements LabelRepository {
headers: {"Content-Type": "application/json"},
);
if (response.statusCode == 201) {
return StoragePath.fromJson(json.decode(response.body));
return StoragePath.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw ErrorMessage(ErrorCode.storagePathCreateFailed, httpStatusCode: response.statusCode);
}
@@ -239,7 +249,7 @@ class LabelRepositoryImpl implements LabelRepository {
body: json.encode(path.toJson()),
);
if (response.statusCode == 200) {
return StoragePath.fromJson(json.decode(response.body));
return StoragePath.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
}
throw const ErrorMessage(ErrorCode.unknown);
}

View File

@@ -31,7 +31,7 @@ class AuthenticationService {
body: {"username": username, "password": password},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final data = jsonDecode(utf8.decode(response.bodyBytes));
return data['token'];
} else if (response.statusCode == 400 &&
response.body.toLowerCase().contains("no required certificate was sent")) {