feat(http): implements doublelinked list for middlewares

This commit is contained in:
2022-06-24 10:47:28 +02:00
parent 58d3b70bee
commit 14cb3485e4
16 changed files with 720 additions and 267 deletions
@@ -17,33 +17,32 @@
import 'dart:convert';
import 'package:http/http.dart';
import 'package:wyatt_http_client/src/middleware.dart';
import 'package:wyatt_http_client/src/models/middleware_context.dart';
import 'package:wyatt_http_client/src/models/middleware_request.dart';
import 'package:wyatt_http_client/src/models/middleware_response.dart';
import 'package:wyatt_http_client/src/models/unfreezed_request.dart';
import 'package:wyatt_http_client/src/pipeline.dart';
import 'package:wyatt_http_client/src/utils/http_methods.dart';
class MiddlewareClient extends BaseClient {
final Client inner;
final Middleware middleware;
final Pipeline pipeline;
MiddlewareClient(
this.pipeline, {
Middleware? middleware,
MiddlewareClient({
Pipeline? pipeline,
Client? inner,
}) : inner = inner ?? Client(),
middleware = middleware ?? pipeline.middleware {
this.middleware.setClient(this);
}) : pipeline = pipeline ?? Pipeline(),
inner = inner ?? Client() {
this.pipeline.setClient(this);
}
@override
Future<Response> head(Uri url, {Map<String, String>? headers}) =>
_sendUnstreamed('HEAD', url, headers);
_sendUnstreamed(HttpMethods.head.method, url, headers);
@override
Future<Response> get(Uri url, {Map<String, String>? headers}) =>
_sendUnstreamed('GET', url, headers);
_sendUnstreamed(HttpMethods.get.method, url, headers);
@override
Future<Response> post(
@@ -52,7 +51,7 @@ class MiddlewareClient extends BaseClient {
Object? body,
Encoding? encoding,
}) =>
_sendUnstreamed('POST', url, headers, body, encoding);
_sendUnstreamed(HttpMethods.post.method, url, headers, body, encoding);
@override
Future<Response> put(
@@ -61,7 +60,7 @@ class MiddlewareClient extends BaseClient {
Object? body,
Encoding? encoding,
}) =>
_sendUnstreamed('PUT', url, headers, body, encoding);
_sendUnstreamed(HttpMethods.put.method, url, headers, body, encoding);
@override
Future<Response> patch(
@@ -70,7 +69,7 @@ class MiddlewareClient extends BaseClient {
Object? body,
Encoding? encoding,
}) =>
_sendUnstreamed('PATCH', url, headers, body, encoding);
_sendUnstreamed(HttpMethods.patch.method, url, headers, body, encoding);
@override
Future<Response> delete(
@@ -79,7 +78,7 @@ class MiddlewareClient extends BaseClient {
Object? body,
Encoding? encoding,
}) =>
_sendUnstreamed('DELETE', url, headers, body, encoding);
_sendUnstreamed(HttpMethods.delete.method, url, headers, body, encoding);
@override
Future<StreamedResponse> send(BaseRequest request) {
@@ -93,24 +92,34 @@ class MiddlewareClient extends BaseClient {
Object? body,
Encoding? encoding,
]) async {
final modifiedRequest = middleware.onRequest(
MiddlewareRequest(
unfreezedRequest: UnfreezedRequest(
method: method,
url: url,
headers: headers,
body: body,
encoding: encoding,
),
httpRequest: Request(method, url),
final originalRequest = MiddlewareRequest(
unfreezedRequest: UnfreezedRequest(
method: method,
url: url,
headers: headers,
body: body,
encoding: encoding,
),
httpRequest: Request(method, url),
context: MiddlewareContext(pipeline: pipeline),
);
final modifiedRequest = await pipeline.onRequest(
originalRequest.copyWith(),
);
final res = await Response.fromStream(
await send(modifiedRequest.httpRequest),
);
final response =
middleware.onResponse(MiddlewareResponse(httpResponse: res));
final response = await pipeline.onResponse(
MiddlewareResponse(
httpResponse: res,
middlewareRequest: modifiedRequest,
context: MiddlewareContext(
pipeline: pipeline,
originalRequest: originalRequest,
),
),
);
return response.httpResponse as Response;
}