feat(http): [WIP] implements middleware system
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
// 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 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart';
|
||||
import 'package:wyatt_http_client/src/middleware.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';
|
||||
|
||||
class MiddlewareClient extends BaseClient {
|
||||
final Client inner;
|
||||
final Middleware middleware;
|
||||
final Pipeline pipeline;
|
||||
|
||||
MiddlewareClient(
|
||||
this.pipeline, {
|
||||
Middleware? middleware,
|
||||
Client? inner,
|
||||
}) : inner = inner ?? Client(),
|
||||
middleware = middleware ?? pipeline.middleware {
|
||||
this.middleware.setClient(this);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> head(Uri url, {Map<String, String>? headers}) =>
|
||||
_sendUnstreamed('HEAD', url, headers);
|
||||
|
||||
@override
|
||||
Future<Response> get(Uri url, {Map<String, String>? headers}) =>
|
||||
_sendUnstreamed('GET', url, headers);
|
||||
|
||||
@override
|
||||
Future<Response> post(
|
||||
Uri url, {
|
||||
Map<String, String>? headers,
|
||||
Object? body,
|
||||
Encoding? encoding,
|
||||
}) =>
|
||||
_sendUnstreamed('POST', url, headers, body, encoding);
|
||||
|
||||
@override
|
||||
Future<Response> put(
|
||||
Uri url, {
|
||||
Map<String, String>? headers,
|
||||
Object? body,
|
||||
Encoding? encoding,
|
||||
}) =>
|
||||
_sendUnstreamed('PUT', url, headers, body, encoding);
|
||||
|
||||
@override
|
||||
Future<Response> patch(
|
||||
Uri url, {
|
||||
Map<String, String>? headers,
|
||||
Object? body,
|
||||
Encoding? encoding,
|
||||
}) =>
|
||||
_sendUnstreamed('PATCH', url, headers, body, encoding);
|
||||
|
||||
@override
|
||||
Future<Response> delete(
|
||||
Uri url, {
|
||||
Map<String, String>? headers,
|
||||
Object? body,
|
||||
Encoding? encoding,
|
||||
}) =>
|
||||
_sendUnstreamed('DELETE', url, headers, body, encoding);
|
||||
|
||||
@override
|
||||
Future<StreamedResponse> send(BaseRequest request) {
|
||||
return inner.send(request);
|
||||
}
|
||||
|
||||
Future<Response> _sendUnstreamed(
|
||||
String method,
|
||||
Uri url,
|
||||
Map<String, String>? headers, [
|
||||
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 res = await Response.fromStream(
|
||||
await send(modifiedRequest.httpRequest),
|
||||
);
|
||||
final response =
|
||||
middleware.onResponse(MiddlewareResponse(httpResponse: res));
|
||||
|
||||
return response.httpResponse as Response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user