Skip to content

Commit

Permalink
Merge pull request #225 from Securrency-OSS/asim/dv/form-builder
Browse files Browse the repository at this point in the history
feat: Added dynamic values in network headers
  • Loading branch information
divyanshub024 authored Oct 19, 2023
2 parents 5131ed9 + aae897b commit d927917
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
52 changes: 19 additions & 33 deletions packages/mirai/lib/src/services/mirai_network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@ class MiraiNetworkService {
static Future<Response?> request(
MiraiNetworkRequest request,
BuildContext context,
) {
) async {
Map<String, dynamic> headers =
Map<String, dynamic>.from(request.headers ?? {});
headers = await _updateBody(context.mounted ? context : context, headers);
_dio.options.headers = headers;
_dio.options.contentType = request.contentType;

switch (request.method) {
case Method.get:
return getRequest(request);
case Method.post:
return postRequest(request, context);
return postRequest(request, context.mounted ? context : context);
case Method.put:
return putRequest(request);
case Method.delete:
return deleteRequest(request);
}
}

static Future<Response?> getRequest(MiraiNetworkRequest request) {
static Future<Response?> getRequest(MiraiNetworkRequest request) async {
return _dio.get(
request.url,
data: request.body,
queryParameters: request.queryParameters,
options: Options(
contentType: request.contentType,
headers: request.headers,
),
);
}

Expand All @@ -46,14 +48,14 @@ class MiraiNetworkService {
BuildContext context,
) async {
final body = await _updateBody(context, request.body);

Map<String, dynamic> headers =
Map<String, dynamic>.from(request.headers ?? {});
headers = await _updateBody(context.mounted ? context : context, headers);
return _dio.post(
request.url,
data: body,
queryParameters: request.queryParameters,
options: Options(
contentType: request.contentType,
headers: request.headers,
),
);
}

Expand All @@ -62,10 +64,6 @@ class MiraiNetworkService {
request.url,
data: request.body,
queryParameters: request.queryParameters,
options: Options(
contentType: request.contentType,
headers: request.headers,
),
);
}

Expand All @@ -74,10 +72,6 @@ class MiraiNetworkService {
request.url,
data: request.body,
queryParameters: request.queryParameters,
options: Options(
contentType: request.contentType,
headers: request.headers,
),
);
}

Expand All @@ -90,22 +84,14 @@ class MiraiNetworkService {
final key = mapEntry.key;
final value = mapEntry.value;
if (value is Map && value.containsKey('actionType')) {
try {
Log.d("Loading from an action callback");
Log.d("Loading from an action callback");

final dynamic callbackValue = await Future<dynamic>.value(
Mirai.onCallFromJson(value as Map<String, dynamic>, context),
);
final dynamic callbackValue = await Future<dynamic>.value(
Mirai.onCallFromJson(value as Map<String, dynamic>, context),
);

body.update(
key,
(existingValue) => callbackValue,
);

continue;
} catch (e) {
Log.e(e);
}
body[key] = callbackValue;
continue;
} else if (value is File) {
String fileName = value.path.split('/').last;
final multipart =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class _$MiraiWebViewCopyWithImpl<$Res, $Val extends MiraiWebView>
}

/// @nodoc
abstract class _$$_MiraiWebViewCopyWith<$Res>
abstract class _$$MiraiWebViewImplCopyWith<$Res>
implements $MiraiWebViewCopyWith<$Res> {
factory _$$_MiraiWebViewCopyWith(
_$_MiraiWebView value, $Res Function(_$_MiraiWebView) then) =
__$$_MiraiWebViewCopyWithImpl<$Res>;
factory _$$MiraiWebViewImplCopyWith(
_$MiraiWebViewImpl value, $Res Function(_$MiraiWebViewImpl) then) =
__$$MiraiWebViewImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
Expand All @@ -107,11 +107,11 @@ abstract class _$$_MiraiWebViewCopyWith<$Res>
}

/// @nodoc
class __$$_MiraiWebViewCopyWithImpl<$Res>
extends _$MiraiWebViewCopyWithImpl<$Res, _$_MiraiWebView>
implements _$$_MiraiWebViewCopyWith<$Res> {
__$$_MiraiWebViewCopyWithImpl(
_$_MiraiWebView _value, $Res Function(_$_MiraiWebView) _then)
class __$$MiraiWebViewImplCopyWithImpl<$Res>
extends _$MiraiWebViewCopyWithImpl<$Res, _$MiraiWebViewImpl>
implements _$$MiraiWebViewImplCopyWith<$Res> {
__$$MiraiWebViewImplCopyWithImpl(
_$MiraiWebViewImpl _value, $Res Function(_$MiraiWebViewImpl) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
Expand All @@ -123,7 +123,7 @@ class __$$_MiraiWebViewCopyWithImpl<$Res>
Object? userAgent = freezed,
Object? enableZoom = null,
}) {
return _then(_$_MiraiWebView(
return _then(_$MiraiWebViewImpl(
url: null == url
? _value.url
: url // ignore: cast_nullable_to_non_nullable
Expand All @@ -150,16 +150,16 @@ class __$$_MiraiWebViewCopyWithImpl<$Res>

/// @nodoc
@JsonSerializable()
class _$_MiraiWebView implements _MiraiWebView {
const _$_MiraiWebView(
class _$MiraiWebViewImpl implements _MiraiWebView {
const _$MiraiWebViewImpl(
{required this.url,
this.javaScriptMode = JavaScriptMode.unrestricted,
this.backgroundColor = "#000000",
this.userAgent,
this.enableZoom = false});

factory _$_MiraiWebView.fromJson(Map<String, dynamic> json) =>
_$$_MiraiWebViewFromJson(json);
factory _$MiraiWebViewImpl.fromJson(Map<String, dynamic> json) =>
_$$MiraiWebViewImplFromJson(json);

@override
final String url;
Expand All @@ -184,7 +184,7 @@ class _$_MiraiWebView implements _MiraiWebView {
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$_MiraiWebView &&
other is _$MiraiWebViewImpl &&
(identical(other.url, url) || other.url == url) &&
(identical(other.javaScriptMode, javaScriptMode) ||
other.javaScriptMode == javaScriptMode) &&
Expand All @@ -204,12 +204,12 @@ class _$_MiraiWebView implements _MiraiWebView {
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$_MiraiWebViewCopyWith<_$_MiraiWebView> get copyWith =>
__$$_MiraiWebViewCopyWithImpl<_$_MiraiWebView>(this, _$identity);
_$$MiraiWebViewImplCopyWith<_$MiraiWebViewImpl> get copyWith =>
__$$MiraiWebViewImplCopyWithImpl<_$MiraiWebViewImpl>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$_MiraiWebViewToJson(
return _$$MiraiWebViewImplToJson(
this,
);
}
Expand All @@ -221,10 +221,10 @@ abstract class _MiraiWebView implements MiraiWebView {
final JavaScriptMode javaScriptMode,
final String backgroundColor,
final String? userAgent,
final bool enableZoom}) = _$_MiraiWebView;
final bool enableZoom}) = _$MiraiWebViewImpl;

factory _MiraiWebView.fromJson(Map<String, dynamic> json) =
_$_MiraiWebView.fromJson;
_$MiraiWebViewImpl.fromJson;

@override
String get url;
Expand All @@ -238,6 +238,6 @@ abstract class _MiraiWebView implements MiraiWebView {
bool get enableZoom;
@override
@JsonKey(ignore: true)
_$$_MiraiWebViewCopyWith<_$_MiraiWebView> get copyWith =>
_$$MiraiWebViewImplCopyWith<_$MiraiWebViewImpl> get copyWith =>
throw _privateConstructorUsedError;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d927917

Please sign in to comment.