Skip to content

Commit

Permalink
Fixed wrong encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
astubenbord committed Nov 1, 2022
1 parent b3cb64c commit 2a1b90c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/core/logic/timeout_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class TimeoutClient implements BaseClient {
Response _handle400Error(Response response) {
if (response.statusCode == 400) {
// try to parse contained error message, otherwise return response
final JSON json = jsonDecode(response.body);
final JSON json = jsonDecode(utf8.decode(response.bodyBytes));
final Map<String, String> errorMessages = {};
//TODO: This could be simplified, look at error message format of paperless-ngx
for (final entry in json.entries) {
Expand Down
10 changes: 5 additions & 5 deletions lib/features/documents/repository/document_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -249,15 +249,15 @@ 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
Future<List<String>> autocomplete(String query, [int limit = 10]) async {
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);
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -88,22 +89,28 @@ 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);
}

@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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/features/login/services/authentication.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down

0 comments on commit 2a1b90c

Please sign in to comment.