feat(http): [WIP] implements middleware system

This commit is contained in:
2022-06-23 17:07:04 +02:00
parent ea4a30ca00
commit 58d3b70bee
12 changed files with 820 additions and 0 deletions
@@ -0,0 +1,89 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:http/http.dart';
import 'package:wyatt_http_client/src/models/unfreezed_request.dart';
import 'package:wyatt_http_client/src/utils/utils.dart';
class MiddlewareRequest {
UnfreezedRequest unfreezedRequest;
Request httpRequest;
MiddlewareRequest({
required this.unfreezedRequest,
required this.httpRequest,
});
MiddlewareRequest copyWith({
UnfreezedRequest? unfreezedRequest,
Request? httpRequest,
}) {
return MiddlewareRequest(
unfreezedRequest: unfreezedRequest ?? this.unfreezedRequest,
httpRequest: httpRequest ?? this.httpRequest,
);
}
void updateUnfreezedRequest(UnfreezedRequest unfreezedRequest) {
final request = httpRequest;
if (unfreezedRequest.headers != null) {
request.headers.addAll(unfreezedRequest.headers!);
}
if (unfreezedRequest.encoding != null) {
request.encoding = unfreezedRequest.encoding!;
}
if (unfreezedRequest.body != null) {
final body = unfreezedRequest.body;
if (body is String) {
request.body = body;
} else if (body is List) {
request.bodyBytes = body.cast<int>();
} else if (body is Map) {
request.bodyFields = body.cast<String, String>();
} else {
throw ArgumentError('Invalid request body "$body".');
}
}
this.unfreezedRequest = unfreezedRequest;
httpRequest = request;
}
void updateHttpRequest({
String? method,
Uri? url,
Map<String, String>? headers,
int? maxRedirects,
bool? followRedirects,
bool? persistentConnection,
String? body,
}) {
httpRequest = Utils.copyRequestWith(
httpRequest,
method: method,
url: url,
headers: headers,
maxRedirects: maxRedirects,
followRedirects: followRedirects,
persistentConnection: persistentConnection,
body: body,
) as Request;
}
@override
String toString() => 'MiddlewareRequest(unfreezedRequest: '
'$unfreezedRequest, httpRequest: $httpRequest)';
}