feat(http): add new middleware feature

This commit is contained in:
2022-06-24 15:58:48 +02:00
parent 14cb3485e4
commit 81367e5f3a
42 changed files with 1021 additions and 2146 deletions
@@ -15,83 +15,68 @@
// 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 'dart:convert';
import 'package:wyatt_http_client/src/models/middleware_context.dart';
import 'package:http/http.dart';
import 'package:wyatt_http_client/src/models/unfreezed_request.dart';
import 'package:wyatt_http_client/src/utils/utils.dart';
import 'package:wyatt_http_client/src/utils/convert.dart';
import 'package:wyatt_http_client/src/utils/request_utils.dart';
class MiddlewareRequest {
UnfreezedRequest unfreezedRequest;
Request httpRequest;
MiddlewareContext context;
Request _httpRequest;
Request get request => _httpRequest;
// Proxy
String get method => _httpRequest.method;
Uri get url => _httpRequest.url;
Map<String, String> get headers => _httpRequest.headers;
Encoding get encoding => _httpRequest.encoding;
String get encodedBody => _httpRequest.body;
Object? get body => unfreezedRequest.body;
MiddlewareRequest({
required this.unfreezedRequest,
required this.httpRequest,
required this.context,
}) {
context = context.copyWith(originalRequest: this);
}
}) : _httpRequest = Request(unfreezedRequest.method, unfreezedRequest.url);
MiddlewareRequest copyWith({
UnfreezedRequest? unfreezedRequest,
Request? httpRequest,
MiddlewareContext? context,
}) {
return MiddlewareRequest(
unfreezedRequest: unfreezedRequest ?? this.unfreezedRequest,
httpRequest: httpRequest ?? this.httpRequest,
context: context ?? this.context,
);
}
void updateUnfreezedRequest(UnfreezedRequest unfreezedRequest) {
final request = httpRequest;
if (unfreezedRequest.headers != null) {
request.headers.addAll(unfreezedRequest.headers!);
}
if (unfreezedRequest.encoding != null) {
request.encoding = unfreezedRequest.encoding!;
}
void modifyRequest(UnfreezedRequest unfreezedRequest) {
String? _body;
if (unfreezedRequest.body != null) {
final body = unfreezedRequest.body;
if (body is String) {
request.body = body;
_body = body;
} else if (body is List) {
request.bodyBytes = body.cast<int>();
_body = String.fromCharCodes(body.cast<int>());
} else if (body is Map) {
request.bodyFields = body.cast<String, String>();
} else {
throw ArgumentError('Invalid request body "$body".');
_body = Convert.mapToQuery(body.cast<String, String>());
}
}
_httpRequest = RequestUtils.copyRequestWith(
_httpRequest,
method: unfreezedRequest.method,
url: unfreezedRequest.url,
headers: unfreezedRequest.headers,
body: _body,
) as Request;
if (unfreezedRequest.encoding != null) {
_httpRequest.encoding = unfreezedRequest.encoding!;
}
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;
void apply() {
modifyRequest(unfreezedRequest);
}
@override
String toString() => 'MiddlewareRequest(unfreezedRequest: '
'$unfreezedRequest, httpRequest: $httpRequest, context: $context)';
String toString() => 'MiddlewareRequest(unfreezedRequest: $unfreezedRequest)';
}