feat(http): [WIP] implements middleware system
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// 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/middleware_client.dart';
|
||||
import 'package:wyatt_http_client/src/models/middleware_request.dart';
|
||||
import 'package:wyatt_http_client/src/models/middleware_response.dart';
|
||||
|
||||
class Middleware {
|
||||
Middleware? child;
|
||||
|
||||
MiddlewareClient? _client;
|
||||
|
||||
Middleware({
|
||||
this.child,
|
||||
});
|
||||
|
||||
Middleware._({
|
||||
this.child,
|
||||
MiddlewareClient? client,
|
||||
}) : _client = client;
|
||||
|
||||
String getName() => 'Middleware';
|
||||
|
||||
void setClient(MiddlewareClient? client) {
|
||||
_client = client;
|
||||
child?.setClient(client);
|
||||
}
|
||||
|
||||
Client? getClient() {
|
||||
return _client?.inner;
|
||||
}
|
||||
|
||||
Middleware deepCopy() {
|
||||
if (child != null) {
|
||||
return Middleware._(child: child?.deepCopy(), client: _client);
|
||||
} else {
|
||||
return Middleware._(client: _client);
|
||||
}
|
||||
}
|
||||
|
||||
void addChild(Middleware middleware) {
|
||||
if (child != null) {
|
||||
child?.addChild(middleware);
|
||||
} else {
|
||||
child = middleware;
|
||||
}
|
||||
}
|
||||
|
||||
MiddlewareRequest onRequest(
|
||||
MiddlewareRequest request,
|
||||
) {
|
||||
return child?.onRequest(request) ?? request;
|
||||
}
|
||||
|
||||
MiddlewareResponse onResponse(MiddlewareResponse response) {
|
||||
return child?.onResponse(response) ?? response;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return getName();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user