Skip to content

Commit

Permalink
fix: changed headers definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Jul 2, 2024
1 parent 25e13f3 commit 0de490f
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/alice/lib/helper/alice_save_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ class AliceSaveHelper {

stringBuffer.writeAll([
'${context.i18n(AliceTranslationKey.saveLogRequestSize)} ${AliceConversionHelper.formatBytes(call.request?.size ?? 0)}\n',
'${context.i18n(AliceTranslationKey.saveLogRequestBody)} ${AliceBodyParser.formatBody(context: context, body: call.request?.body, contentType: call.request?.contentType)}\n',
'${context.i18n(AliceTranslationKey.saveLogRequestBody)} ${AliceParser.formatBody(context: context, body: call.request?.body, contentType: call.request?.contentType)}\n',
'--------------------------------------------\n',
'${context.i18n(AliceTranslationKey.saveLogResponse)}\n',
'--------------------------------------------\n',
'${context.i18n(AliceTranslationKey.saveLogResponseTime)} ${call.response?.time}\n',
'${context.i18n(AliceTranslationKey.saveLogResponseStatus)} ${call.response?.status}\n',
'${context.i18n(AliceTranslationKey.saveLogResponseSize)} ${AliceConversionHelper.formatBytes(call.response?.size ?? 0)}\n',
'${context.i18n(AliceTranslationKey.saveLogResponseHeaders)} ${_encoder.convert(call.response?.headers)}\n',
'${context.i18n(AliceTranslationKey.saveLogResponseBody)} ${AliceBodyParser.formatBody(context: context, body: call.response?.body, contentType: AliceBodyParser.getContentType(context: context, headers: call.response?.headers))}\n',
'${context.i18n(AliceTranslationKey.saveLogResponseBody)} ${AliceParser.formatBody(context: context, body: call.response?.body, contentType: AliceParser.getContentType(context: context, headers: call.response?.headers))}\n',
]);

if (call.error != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/alice/lib/model/alice_http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:equatable/equatable.dart';
class AliceHttpRequest with EquatableMixin {
int size = 0;
DateTime time = DateTime.now();
Map<String, dynamic> headers = <String, dynamic>{};
Map<String,String> headers = <String,String>{};
dynamic body = '';
String? contentType = '';
List<Cookie> cookies = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AliceCallRequestScreen extends StatelessWidget {
value: AliceConversionHelper.formatBytes(call.request?.size ?? 0)),
AliceCallListRow(
name: context.i18n(AliceTranslationKey.callRequestContentType),
value: AliceBodyParser.getContentType(
value: AliceParser.getContentType(
context: context, headers: call.request?.headers)),
];

Expand Down Expand Up @@ -105,10 +105,10 @@ class AliceCallRequestScreen extends StatelessWidget {
String _getBodyContent({required BuildContext context}) {
final dynamic body = call.request?.body;
return body != null
? AliceBodyParser.formatBody(
? AliceParser.formatBody(
context: context,
body: body,
contentType: AliceBodyParser.getContentType(
contentType: AliceParser.getContentType(
context: context, headers: call.request?.headers),
)
: context.i18n(AliceTranslationKey.callRequestBodyEmpty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class _BodyDataColumnState extends State<_BodyDataColumn> {
}

String? _getContentTypeOfResponse() {
return AliceBodyParser.getContentType(
return AliceParser.getContentType(
context: context, headers: call.response?.headers);
}

Expand Down Expand Up @@ -301,11 +301,11 @@ class _TextBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Map<String, String>? headers = call.response?.headers;
final String bodyContent = AliceBodyParser.formatBody(
final String bodyContent = AliceParser.formatBody(
context: context,
body: call.response?.body,
contentType:
AliceBodyParser.getContentType(context: context, headers: headers),
AliceParser.getContentType(context: context, headers: headers),
);
return AliceCallListRow(
name: context.i18n(AliceTranslationKey.callResponseBody),
Expand Down Expand Up @@ -358,15 +358,15 @@ class _UnknownBody extends StatelessWidget {
Widget build(BuildContext context) {
final Map<String, String>? headers = call.response?.headers;
final String contentType =
AliceBodyParser.getContentType(context: context, headers: headers) ??
AliceParser.getContentType(context: context, headers: headers) ??
context.i18n(AliceTranslationKey.callResponseHeadersUnknown);

if (showUnsupportedBody) {
final bodyContent = AliceBodyParser.formatBody(
final bodyContent = AliceParser.formatBody(
context: context,
body: call.response?.body,
contentType:
AliceBodyParser.getContentType(context: context, headers: headers),
AliceParser.getContentType(context: context, headers: headers),
);
return AliceCallListRow(
name: context.i18n(AliceTranslationKey.callResponseBody),
Expand Down
20 changes: 16 additions & 4 deletions packages/alice/lib/utils/alice_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:alice/ui/common/alice_context_ext.dart';
import 'package:flutter/material.dart';

/// Body parser helper used to parsing body data.
class AliceBodyParser {
class AliceParser {
static const String _jsonContentTypeSmall = 'content-type';
static const String _jsonContentTypeBig = 'Content-Type';
static const String _stream = 'Stream';
Expand Down Expand Up @@ -81,16 +81,28 @@ class AliceBodyParser {
/// it, it will return unknown content type.
static String? getContentType({
required BuildContext context,
Map<String, dynamic>? headers,
Map<String, String>? headers,
}) {
if (headers != null) {
if (headers.containsKey(_jsonContentTypeSmall)) {
return headers[_jsonContentTypeSmall] as String?;
return headers[_jsonContentTypeSmall];
}
if (headers.containsKey(_jsonContentTypeBig)) {
return headers[_jsonContentTypeBig] as String?;
return headers[_jsonContentTypeBig];
}
}
return context.i18n(AliceTranslationKey.unknown);
}

static Map<String, String> parseHeaders({dynamic headers}) {
if (headers is Map<String, String>) {
return headers;
}

if (headers is Map<String, dynamic>) {
return headers.map((key, value) => MapEntry(key, value.toString()));
}

throw ArgumentError("Invalid headers value.");
}
}
2 changes: 1 addition & 1 deletion packages/alice/lib/utils/curl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ String getCurlCommand(AliceHttpCall call) {

curlCmd.write(' -X ${call.method}');

for (final MapEntry<String, dynamic> header
for (final MapEntry<String, String> header
in call.request?.headers.entries ?? []) {
if (header.key.toLowerCase() == HttpHeaders.acceptEncodingHeader &&
header.value.toString().toLowerCase() == 'gzip') {
Expand Down
12 changes: 6 additions & 6 deletions packages/alice/test/utils/alice_body_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
group("AliceBodyParser", () {
test("should parse json body and pretty print it", () {
expect(
AliceBodyParser.formatBody(
AliceParser.formatBody(
context: context,
body: '{"id": 1, "name": "test}',
contentType: "application/json"),
Expand All @@ -26,7 +26,7 @@ void main() {

test("should parse unknown body", () {
expect(
AliceBodyParser.formatBody(
AliceParser.formatBody(
context: context,
body: 'test',
),
Expand All @@ -36,7 +36,7 @@ void main() {

test("should parse empty body", () {
expect(
AliceBodyParser.formatBody(
AliceParser.formatBody(
context: context,
body: '',
),
Expand All @@ -46,14 +46,14 @@ void main() {

test("should parse application/json content type", () {
expect(
AliceBodyParser.getContentType(
AliceParser.getContentType(
context: context,
headers: {'Content-Type': "application/json"},
),
"application/json");

expect(
AliceBodyParser.getContentType(
AliceParser.getContentType(
context: context,
headers: {'content-type': "application/json"},
),
Expand All @@ -62,7 +62,7 @@ void main() {

test("should parse unknown content type", () {
expect(
AliceBodyParser.getContentType(
AliceParser.getContentType(
context: context,
headers: {},
),
Expand Down
3 changes: 2 additions & 1 deletion packages/alice_dio/lib/alice_dio_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:alice/model/alice_http_error.dart';
import 'package:alice/model/alice_http_request.dart';
import 'package:alice/model/alice_http_response.dart';
import 'package:alice/model/alice_log.dart';
import 'package:alice/utils/alice_parser.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -75,7 +76,7 @@ class AliceDioAdapter extends InterceptorsWrapper with AliceAdapter {

request
..time = DateTime.now()
..headers = options.headers
..headers = AliceParser.parseHeaders(headers: options.headers)
..contentType = options.contentType.toString()
..queryParameters = options.queryParameters;

Expand Down
4 changes: 2 additions & 2 deletions packages/alice_http/lib/alice_http_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AliceHttpAdapter with AliceAdapter {
httpRequest
..body = body ?? (response.request! as http.Request).body ?? ''
..size = utf8.encode(httpRequest.body.toString()).length
..headers = Map<String, dynamic>.from(response.request!.headers);
..headers = Map<String, String>.from(response.request!.headers);
} else if (body == null) {
httpRequest
..size = 0
Expand All @@ -56,7 +56,7 @@ class AliceHttpAdapter with AliceAdapter {

String? contentType = 'unknown';
if (httpRequest.headers.containsKey('Content-Type')) {
contentType = httpRequest.headers['Content-Type'] as String?;
contentType = httpRequest.headers['Content-Type'];
}

httpRequest
Expand Down
5 changes: 3 additions & 2 deletions packages/alice_http_client/lib/alice_http_client_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:alice/core/alice_adapter.dart';
import 'package:alice/model/alice_http_call.dart';
import 'package:alice/model/alice_http_request.dart';
import 'package:alice/model/alice_http_response.dart';
import 'package:alice/utils/alice_parser.dart';

class AliceHttpClientAdapter with AliceAdapter {
/// Handles httpClientRequest and creates http alice call from it
Expand Down Expand Up @@ -43,10 +44,10 @@ class AliceHttpClientAdapter with AliceAdapter {
headers[header] = value;
});

httpRequest.headers = headers;
httpRequest.headers = AliceParser.parseHeaders(headers: headers);
String? contentType = 'unknown';
if (headers.containsKey('Content-Type')) {
contentType = headers['Content-Type'] as String?;
contentType = headers['Content-Type'];
}

httpRequest
Expand Down
32 changes: 17 additions & 15 deletions packages/alice_objectbox/lib/model/cached_alice_http_request.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import 'dart:convert' show jsonDecode, jsonEncode;
import 'dart:io' show Cookie;

import 'package:alice/model/alice_form_data_file.dart';
import 'package:alice/model/alice_from_data_field.dart';
import 'package:alice/model/alice_http_request.dart';
import 'package:alice/utils/alice_parser.dart';
import 'package:alice_objectbox/json_converter/alice_form_data_field_converter.dart';
import 'package:alice_objectbox/json_converter/alice_form_data_file_converter.dart';
import 'package:meta/meta.dart';
Expand All @@ -17,7 +17,7 @@ class CachedAliceHttpRequest implements AliceHttpRequest {
this.objectId = 0,
this.size = 0,
DateTime? time,
this.headers = const <String, dynamic>{},
this.headers = const <String, String>{},
this.body = '',
this.contentType = '',
this.cookies = const [],
Expand All @@ -40,15 +40,15 @@ class CachedAliceHttpRequest implements AliceHttpRequest {

@override
@Transient()
Map<String, dynamic> headers;
Map<String, String> headers;

/// Custom data type converter of [headers].
String get dbHeaders => jsonEncode(headers);

/// Custom data type converter of [headers].
set dbHeaders(String value) =>
headers = jsonDecode(value) as Map<String, dynamic>;

set dbHeaders(String value) => headers = AliceParser.parseHeaders(

This comment has been minimized.

Copy link
@techouse

techouse Jul 2, 2024

Collaborator

I'm not sure you understand what this does. This setter is here because ObjectBox can't store a Map https://docs.objectbox.io/advanced/custom-types

Not sure why you changed it.

Anyhow, after ANY change to the ObjectBox models, please re-run code generation using

dart run build_runner build --delete-conflicting-outputs

inside packages/alice_objectbox

headers: jsonDecode(value),
);
@override
@Transient()
dynamic body;
Expand Down Expand Up @@ -135,15 +135,17 @@ class CachedAliceHttpRequest implements AliceHttpRequest {
.toList();

@override
List<Object?> get props => [ size,
time,
headers,
body,
contentType,
cookies,
queryParameters,
formDataFiles,
formDataFields,];
List<Object?> get props => [

This comment has been minimized.

Copy link
@techouse

techouse Jul 2, 2024

Collaborator

All of these getters should probably be annotated with @Transient https://docs.objectbox.io/entity-annotations#transient that way ObjectBox won't attempt to do anything with them.

size,
time,
headers,
body,
contentType,
cookies,
queryParameters,
formDataFiles,
formDataFields,
];

@override
bool? get stringify => true;
Expand Down

0 comments on commit 0de490f

Please sign in to comment.