Feature/middlewares #9
@ -17,8 +17,13 @@
|
|||||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:wyatt_http_client/src/authentication/refresh_token_client.dart';
|
import 'package:wyatt_http_client/src/middleware_client.dart';
|
||||||
import 'package:wyatt_http_client/src/rest_client.dart';
|
import 'package:wyatt_http_client/src/middlewares/body_to_json_middleware.dart';
|
||||||
|
import 'package:wyatt_http_client/src/middlewares/refresh_token_middleware.dart';
|
||||||
|
import 'package:wyatt_http_client/src/middlewares/simple_logger_middleware.dart';
|
||||||
|
import 'package:wyatt_http_client/src/middlewares/uri_prefix_middleware.dart';
|
||||||
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
import 'package:wyatt_http_client/src/utils/http_status.dart';
|
||||||
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
||||||
|
|
||||||
enum EmailVerificationAction {
|
enum EmailVerificationAction {
|
||||||
@ -262,24 +267,14 @@ class Login {
|
|||||||
|
|
||||||
class FastAPI {
|
class FastAPI {
|
||||||
final String baseUrl;
|
final String baseUrl;
|
||||||
final RefreshTokenClient client;
|
final MiddlewareClient client;
|
||||||
final int apiVersion;
|
final int apiVersion;
|
||||||
|
|
||||||
FastAPI({
|
FastAPI({
|
||||||
this.baseUrl = 'localhost:80',
|
this.baseUrl = 'localhost:80',
|
||||||
RefreshTokenClient? client,
|
MiddlewareClient? client,
|
||||||
this.apiVersion = 1,
|
this.apiVersion = 1,
|
||||||
}) : client = client ??
|
}) : client = client ?? MiddlewareClient();
|
||||||
RefreshTokenClient(
|
|
||||||
authorizationEndpoint: '',
|
|
||||||
tokenEndpoint: '',
|
|
||||||
accessTokenParser: (body) => body['access_token']! as String,
|
|
||||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
|
||||||
inner: RestClient(
|
|
||||||
protocol: Protocols.http,
|
|
||||||
authority: baseUrl,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
String get apiPath => '/api/v$apiVersion';
|
String get apiPath => '/api/v$apiVersion';
|
||||||
|
|
||||||
@ -323,14 +318,21 @@ class FastAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<TokenSuccess> signInWithPassword(Login login) async {
|
Future<TokenSuccess> signInWithPassword(Login login) async {
|
||||||
final r = await client.authorize(login.toMap());
|
final r = await client.post(
|
||||||
return TokenSuccess.fromJson(r.body);
|
Uri.parse('$apiPath/auth/sign-in-with-password'),
|
||||||
|
body: login.toMap(),
|
||||||
|
);
|
||||||
|
if (r.statusCode != 200) {
|
||||||
|
throw Exception('Invalid reponse: ${r.statusCode}');
|
||||||
|
} else {
|
||||||
|
return TokenSuccess.fromJson(r.body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<TokenSuccess> refresh() async {
|
// Future<TokenSuccess> refresh() async {
|
||||||
final r = await client.refresh();
|
// final r = await client.refresh();
|
||||||
return TokenSuccess.fromJson(r?.body ?? '');
|
// return TokenSuccess.fromJson(r?.body ?? '');
|
||||||
}
|
// }
|
||||||
|
|
||||||
Future<List<Account>> getAccountList() async {
|
Future<List<Account>> getAccountList() async {
|
||||||
final r = await client.get(
|
final r = await client.get(
|
||||||
@ -351,17 +353,30 @@ class FastAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main(List<String> args) async {
|
void main(List<String> args) async {
|
||||||
|
final Pipeline pipeline = Pipeline()
|
||||||
|
.addMiddleware(SimpleLoggerMiddleware())
|
||||||
|
.addMiddleware(
|
||||||
|
UriPrefixMiddleware(
|
||||||
|
protocol: Protocols.http,
|
||||||
|
authority: 'localhost:80',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.addMiddleware(BodyToJsonMiddleware())
|
||||||
|
.addMiddleware(
|
||||||
|
RefreshTokenMiddleware(
|
||||||
|
authorizationEndpoint: '/api/v1/auth/sign-in-with-password',
|
||||||
|
tokenEndpoint: '/api/v1/auth/refresh',
|
||||||
|
accessTokenParser: (body) => body['access_token']! as String,
|
||||||
|
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||||
|
unauthorized: HttpStatus.forbidden,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
print(pipeline.getLogic());
|
||||||
|
final client = MiddlewareClient(pipeline: pipeline);
|
||||||
|
|
||||||
final api = FastAPI(
|
final api = FastAPI(
|
||||||
client: RefreshTokenClient(
|
client: client,
|
||||||
authorizationEndpoint: '/api/v1/auth/sign-in-with-password',
|
|
||||||
tokenEndpoint: '/api/v1/auth/refresh',
|
|
||||||
accessTokenParser: (body) => body['access_token']! as String,
|
|
||||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
|
||||||
inner: RestClient(
|
|
||||||
protocol: Protocols.http,
|
|
||||||
authority: 'localhost:80',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// await api.sendSignUpCode('git@pcl.ovh');
|
// await api.sendSignUpCode('git@pcl.ovh');
|
||||||
|
@ -117,7 +117,7 @@ import 'package:wyatt_http_client/src/utils/protocols.dart';
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
Future<void> main(List<String> args) async {
|
Future<void> main(List<String> args) async {
|
||||||
final Pipeline pipeline1 = Pipeline()
|
final Pipeline pipeline = Pipeline()
|
||||||
.addMiddleware(SimpleLoggerMiddleware())
|
.addMiddleware(SimpleLoggerMiddleware())
|
||||||
.addMiddleware(
|
.addMiddleware(
|
||||||
UriPrefixMiddleware(
|
UriPrefixMiddleware(
|
||||||
@ -125,21 +125,18 @@ Future<void> main(List<String> args) async {
|
|||||||
authority: 'localhost:80',
|
authority: 'localhost:80',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.addMiddleware(BodyToJsonMiddleware());
|
.addMiddleware(BodyToJsonMiddleware())
|
||||||
|
.addMiddleware(
|
||||||
final Pipeline pipeline2 = Pipeline().addMiddleware(
|
RefreshTokenMiddleware(
|
||||||
RefreshTokenMiddleware(
|
authorizationEndpoint: '/api/v1/account/test?action=authorize',
|
||||||
authorizationEndpoint:
|
tokenEndpoint: '/api/v1/account/test?action=refresh',
|
||||||
'http://localhost:80/api/v1/account/test?action=authorize',
|
accessTokenParser: (body) => body['access_token']! as String,
|
||||||
tokenEndpoint: 'http://localhost:80/api/v1/account/test?action=refresh',
|
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||||
innerClientMiddlewares: pipeline1.middleware,
|
),
|
||||||
),
|
);
|
||||||
);
|
|
||||||
|
|
||||||
final Pipeline pipeline = pipeline1 + pipeline2;
|
|
||||||
|
|
||||||
print(pipeline.getLogic());
|
print(pipeline.getLogic());
|
||||||
final client = MiddlewareClient(pipeline);
|
final client = MiddlewareClient(pipeline: pipeline);
|
||||||
final r = await client.post(
|
final r = await client.post(
|
||||||
Uri.parse('/api/v1/account/test'),
|
Uri.parse('/api/v1/account/test'),
|
||||||
body: <String, String>{
|
body: <String, String>{
|
||||||
|
@ -14,60 +14,27 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:http/http.dart';
|
part of 'pipeline.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 {
|
class Middleware {
|
||||||
Middleware? child;
|
/// The http [MiddlewareClient] used by this [Middleware]
|
||||||
|
|
||||||
MiddlewareClient? _client;
|
MiddlewareClient? _client;
|
||||||
|
|
||||||
Middleware({
|
String getName() => 'MiddlewareNode';
|
||||||
this.child,
|
|
||||||
});
|
|
||||||
|
|
||||||
Middleware._({
|
// ignore: avoid_setters_without_getters
|
||||||
this.child,
|
set httpClient(MiddlewareClient? client) => _client = client;
|
||||||
MiddlewareClient? client,
|
|
||||||
}) : _client = client;
|
|
||||||
|
|
||||||
String getName() => 'Middleware';
|
Client? get client => _client?.inner;
|
||||||
|
|
||||||
void setClient(MiddlewareClient? client) {
|
Future<MiddlewareRequest> onRequest(
|
||||||
_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,
|
MiddlewareRequest request,
|
||||||
) {
|
) async {
|
||||||
return child?.onRequest(request) ?? request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
MiddlewareResponse onResponse(MiddlewareResponse response) {
|
Future<MiddlewareResponse> onResponse(MiddlewareResponse response) async {
|
||||||
return child?.onResponse(response) ?? response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -17,33 +17,32 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:http/http.dart';
|
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_request.dart';
|
||||||
import 'package:wyatt_http_client/src/models/middleware_response.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/models/unfreezed_request.dart';
|
||||||
import 'package:wyatt_http_client/src/pipeline.dart';
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
import 'package:wyatt_http_client/src/utils/http_methods.dart';
|
||||||
|
|
||||||
class MiddlewareClient extends BaseClient {
|
class MiddlewareClient extends BaseClient {
|
||||||
final Client inner;
|
final Client inner;
|
||||||
final Middleware middleware;
|
|
||||||
final Pipeline pipeline;
|
final Pipeline pipeline;
|
||||||
|
|
||||||
MiddlewareClient(
|
MiddlewareClient({
|
||||||
this.pipeline, {
|
Pipeline? pipeline,
|
||||||
Middleware? middleware,
|
|
||||||
Client? inner,
|
Client? inner,
|
||||||
}) : inner = inner ?? Client(),
|
}) : pipeline = pipeline ?? Pipeline(),
|
||||||
middleware = middleware ?? pipeline.middleware {
|
inner = inner ?? Client() {
|
||||||
this.middleware.setClient(this);
|
this.pipeline.setClient(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> head(Uri url, {Map<String, String>? headers}) =>
|
Future<Response> head(Uri url, {Map<String, String>? headers}) =>
|
||||||
_sendUnstreamed('HEAD', url, headers);
|
_sendUnstreamed(HttpMethods.head.method, url, headers);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> get(Uri url, {Map<String, String>? headers}) =>
|
Future<Response> get(Uri url, {Map<String, String>? headers}) =>
|
||||||
_sendUnstreamed('GET', url, headers);
|
_sendUnstreamed(HttpMethods.get.method, url, headers);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> post(
|
Future<Response> post(
|
||||||
@ -52,7 +51,7 @@ class MiddlewareClient extends BaseClient {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Encoding? encoding,
|
Encoding? encoding,
|
||||||
}) =>
|
}) =>
|
||||||
_sendUnstreamed('POST', url, headers, body, encoding);
|
_sendUnstreamed(HttpMethods.post.method, url, headers, body, encoding);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> put(
|
Future<Response> put(
|
||||||
@ -61,7 +60,7 @@ class MiddlewareClient extends BaseClient {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Encoding? encoding,
|
Encoding? encoding,
|
||||||
}) =>
|
}) =>
|
||||||
_sendUnstreamed('PUT', url, headers, body, encoding);
|
_sendUnstreamed(HttpMethods.put.method, url, headers, body, encoding);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> patch(
|
Future<Response> patch(
|
||||||
@ -70,7 +69,7 @@ class MiddlewareClient extends BaseClient {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Encoding? encoding,
|
Encoding? encoding,
|
||||||
}) =>
|
}) =>
|
||||||
_sendUnstreamed('PATCH', url, headers, body, encoding);
|
_sendUnstreamed(HttpMethods.patch.method, url, headers, body, encoding);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> delete(
|
Future<Response> delete(
|
||||||
@ -79,7 +78,7 @@ class MiddlewareClient extends BaseClient {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Encoding? encoding,
|
Encoding? encoding,
|
||||||
}) =>
|
}) =>
|
||||||
_sendUnstreamed('DELETE', url, headers, body, encoding);
|
_sendUnstreamed(HttpMethods.delete.method, url, headers, body, encoding);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<StreamedResponse> send(BaseRequest request) {
|
Future<StreamedResponse> send(BaseRequest request) {
|
||||||
@ -93,24 +92,34 @@ class MiddlewareClient extends BaseClient {
|
|||||||
Object? body,
|
Object? body,
|
||||||
Encoding? encoding,
|
Encoding? encoding,
|
||||||
]) async {
|
]) async {
|
||||||
final modifiedRequest = middleware.onRequest(
|
final originalRequest = MiddlewareRequest(
|
||||||
MiddlewareRequest(
|
unfreezedRequest: UnfreezedRequest(
|
||||||
unfreezedRequest: UnfreezedRequest(
|
method: method,
|
||||||
method: method,
|
url: url,
|
||||||
url: url,
|
headers: headers,
|
||||||
headers: headers,
|
body: body,
|
||||||
body: body,
|
encoding: encoding,
|
||||||
encoding: encoding,
|
|
||||||
),
|
|
||||||
httpRequest: Request(method, url),
|
|
||||||
),
|
),
|
||||||
|
httpRequest: Request(method, url),
|
||||||
|
context: MiddlewareContext(pipeline: pipeline),
|
||||||
|
);
|
||||||
|
final modifiedRequest = await pipeline.onRequest(
|
||||||
|
originalRequest.copyWith(),
|
||||||
);
|
);
|
||||||
|
|
||||||
final res = await Response.fromStream(
|
final res = await Response.fromStream(
|
||||||
await send(modifiedRequest.httpRequest),
|
await send(modifiedRequest.httpRequest),
|
||||||
);
|
);
|
||||||
final response =
|
final response = await pipeline.onResponse(
|
||||||
middleware.onResponse(MiddlewareResponse(httpResponse: res));
|
MiddlewareResponse(
|
||||||
|
httpResponse: res,
|
||||||
|
middlewareRequest: modifiedRequest,
|
||||||
|
context: MiddlewareContext(
|
||||||
|
pipeline: pipeline,
|
||||||
|
originalRequest: originalRequest,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
return response.httpResponse as Response;
|
return response.httpResponse as Response;
|
||||||
}
|
}
|
||||||
|
103
packages/wyatt_http_client/lib/src/middleware_node.dart
Normal file
103
packages/wyatt_http_client/lib/src/middleware_node.dart
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
part of 'pipeline.dart';
|
||||||
|
|
||||||
|
class MiddlewareNode {
|
||||||
|
final Pipeline pipeline;
|
||||||
|
|
||||||
|
Middleware? middleware;
|
||||||
|
|
||||||
|
late MiddlewareNode _parent;
|
||||||
|
late MiddlewareNode _child;
|
||||||
|
final bool _isEnd;
|
||||||
|
|
||||||
|
/// Reference to the previous [MiddlewareNode] in the [Pipeline]
|
||||||
|
MiddlewareNode get parent => _parent;
|
||||||
|
|
||||||
|
/// Reference to the next [MiddlewareNode] in the [Pipeline]
|
||||||
|
MiddlewareNode get child => _child;
|
||||||
|
|
||||||
|
/// Whether this is the begin [MiddlewareNode]
|
||||||
|
bool get isBegin => _parent == this;
|
||||||
|
|
||||||
|
/// Whether this is the end [MiddlewareNode]
|
||||||
|
bool get isEnd => _child == this;
|
||||||
|
|
||||||
|
/// Whether this is the first [MiddlewareNode]
|
||||||
|
bool get isFirst => !isBegin && _parent == pipeline.begin;
|
||||||
|
|
||||||
|
/// Whether this is the last [MiddlewareNode]
|
||||||
|
bool get isLast => !isEnd && _child == pipeline.end;
|
||||||
|
|
||||||
|
MiddlewareNode._(
|
||||||
|
this.pipeline,
|
||||||
|
this.middleware, {
|
||||||
|
MiddlewareNode? parent,
|
||||||
|
MiddlewareNode? child,
|
||||||
|
}) : _isEnd = false {
|
||||||
|
_parent = parent ?? this;
|
||||||
|
_child = child ?? this;
|
||||||
|
}
|
||||||
|
|
||||||
|
MiddlewareNode._end(this.pipeline) : _isEnd = true {
|
||||||
|
_child = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
MiddlewareNode._begin(this.pipeline) : _isEnd = true {
|
||||||
|
_parent = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new [MiddlewareNode] right **before** this in [pipeline]
|
||||||
|
MiddlewareNode insertBefore(Middleware middleware) {
|
||||||
|
if (isBegin) {
|
||||||
|
throw StateError(
|
||||||
|
'A MiddlewareNode cannot be inserted '
|
||||||
|
'before begin MiddlewareNode',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
final newMiddlewareNode =
|
||||||
|
MiddlewareNode._(pipeline, middleware, parent: _parent, child: this);
|
||||||
|
_parent._child = newMiddlewareNode;
|
||||||
|
_parent = newMiddlewareNode;
|
||||||
|
pipeline._length++;
|
||||||
|
return newMiddlewareNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new [MiddlewareNode] right **after** this in [pipeline]
|
||||||
|
MiddlewareNode insertAfter(Middleware middleware) {
|
||||||
|
if (isEnd) {
|
||||||
|
throw StateError(
|
||||||
|
'A MiddlewareNode cannot be inserted '
|
||||||
|
'after end MiddlewareNode',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
final newMiddlewareNode =
|
||||||
|
MiddlewareNode._(pipeline, middleware, parent: this, child: _child);
|
||||||
|
_child._parent = newMiddlewareNode;
|
||||||
|
_child = newMiddlewareNode;
|
||||||
|
pipeline._length++;
|
||||||
|
return newMiddlewareNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
MiddlewareNode remove() {
|
||||||
|
if (_isEnd) throw StateError('Cannot remove end MiddlewareNode');
|
||||||
|
_child._parent = _parent;
|
||||||
|
_parent._child = _child;
|
||||||
|
pipeline._length--;
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
}
|
@ -16,21 +16,17 @@
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
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_request.dart';
|
||||||
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
|
||||||
class BodyToJsonMiddleware extends Middleware {
|
class BodyToJsonMiddleware extends Middleware {
|
||||||
BodyToJsonMiddleware({
|
@override
|
||||||
super.child,
|
String getName() => 'BodyToJson';
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getName() => 'BodyToJsonMiddleware';
|
Future<MiddlewareRequest> onRequest(MiddlewareRequest request) {
|
||||||
|
|
||||||
@override
|
|
||||||
MiddlewareRequest onRequest(MiddlewareRequest request) {
|
|
||||||
print(
|
print(
|
||||||
'BodyToJson::OnRequest: transforms body in json if Map then update '
|
'${getName()}::OnRequest: transforms body in json if Map then update '
|
||||||
'headers with right content-type',
|
'headers with right content-type',
|
||||||
);
|
);
|
||||||
var newReq = request.unfreezedRequest;
|
var newReq = request.unfreezedRequest;
|
||||||
|
@ -14,13 +14,9 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:wyatt_http_client/src/middleware.dart';
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
|
||||||
class DefaultMiddleware extends Middleware {
|
class DefaultMiddleware extends Middleware {
|
||||||
DefaultMiddleware({
|
|
||||||
super.child,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getName() => 'DefaultMiddleware';
|
String getName() => 'DefaultMiddleware';
|
||||||
}
|
}
|
||||||
|
@ -14,59 +14,200 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:wyatt_http_client/src/middleware.dart';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:http/http.dart';
|
||||||
import 'package:wyatt_http_client/src/middleware_client.dart';
|
import 'package:wyatt_http_client/src/middleware_client.dart';
|
||||||
import 'package:wyatt_http_client/src/middlewares/default_middleware.dart';
|
|
||||||
import 'package:wyatt_http_client/src/models/middleware_request.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/pipeline.dart';
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
import 'package:wyatt_http_client/src/utils/authentication_methods.dart';
|
||||||
|
import 'package:wyatt_http_client/src/utils/header_keys.dart';
|
||||||
|
import 'package:wyatt_http_client/src/utils/http_status.dart';
|
||||||
|
|
||||||
|
typedef TokenParser = String Function(Map<String, dynamic>);
|
||||||
|
|
||||||
class RefreshTokenMiddleware extends Middleware {
|
class RefreshTokenMiddleware extends Middleware {
|
||||||
final String authorizationEndpoint;
|
final String authorizationEndpoint;
|
||||||
final String tokenEndpoint;
|
final String tokenEndpoint;
|
||||||
|
|
||||||
String? accessToken;
|
String? accessToken;
|
||||||
|
final TokenParser accessTokenParser;
|
||||||
String? refreshToken;
|
String? refreshToken;
|
||||||
|
final TokenParser refreshTokenParser;
|
||||||
|
|
||||||
Middleware innerClientMiddlewares;
|
final String authenticationHeader;
|
||||||
|
final String authenticationMethod;
|
||||||
|
final HttpStatus unauthorized;
|
||||||
|
final int maxRetries;
|
||||||
|
|
||||||
RefreshTokenMiddleware({
|
RefreshTokenMiddleware({
|
||||||
required this.authorizationEndpoint,
|
required this.authorizationEndpoint,
|
||||||
required this.tokenEndpoint,
|
required this.tokenEndpoint,
|
||||||
Middleware? innerClientMiddlewares,
|
required this.accessTokenParser,
|
||||||
super.child,
|
required this.refreshTokenParser,
|
||||||
}) : innerClientMiddlewares = innerClientMiddlewares ?? DefaultMiddleware();
|
this.authenticationHeader = HeaderKeys.authorization,
|
||||||
|
this.authenticationMethod = AuthenticationMethods.bearer,
|
||||||
|
this.unauthorized = HttpStatus.unauthorized,
|
||||||
|
this.maxRetries = 3,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getName() => 'RefreshTokenMiddleware';
|
String getName() => 'RefreshToken';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MiddlewareRequest onRequest(MiddlewareRequest request) {
|
Future<MiddlewareRequest> onRequest(MiddlewareRequest request) async {
|
||||||
print(
|
print(
|
||||||
'RefreshToken::OnRequest: accessToken: $accessToken',
|
'${getName()}::OnRequest: accessToken: $accessToken',
|
||||||
);
|
);
|
||||||
if (accessToken == null) {
|
if (request.context.originalRequest?.unfreezedRequest.url ==
|
||||||
// Refresh token
|
Uri.parse(authorizationEndpoint)) {
|
||||||
final pipeline = Pipeline().addMiddleware(innerClientMiddlewares);
|
return super.onRequest(request);
|
||||||
print(pipeline.getLogic());
|
}
|
||||||
final client = MiddlewareClient(
|
if (accessToken != null) {
|
||||||
pipeline,
|
// Modify header with accessToken
|
||||||
inner: getClient(),
|
var newReq = request.unfreezedRequest;
|
||||||
|
final mutation = {
|
||||||
|
authenticationHeader: '$authenticationMethod $accessToken',
|
||||||
|
};
|
||||||
|
Map<String, String>? headers = newReq.headers;
|
||||||
|
if (headers != null) {
|
||||||
|
headers.addAll(mutation);
|
||||||
|
} else {
|
||||||
|
headers = mutation;
|
||||||
|
}
|
||||||
|
newReq = newReq.copyWith(headers: headers);
|
||||||
|
request.updateUnfreezedRequest(newReq);
|
||||||
|
return super.onRequest(request);
|
||||||
|
}
|
||||||
|
if (refreshToken != null) {
|
||||||
|
// Refresh accessToken with refreshToken before perform request
|
||||||
|
final subPipeline = request.context.pipeline.fromUntil(this);
|
||||||
|
final httpClient = MiddlewareClient(
|
||||||
|
pipeline: subPipeline,
|
||||||
|
inner: client,
|
||||||
);
|
);
|
||||||
final _ = client.post(Uri.parse(tokenEndpoint));
|
final Map<String, String> headers = {
|
||||||
}
|
authenticationHeader: '$authenticationHeader $refreshToken',
|
||||||
var newReq = request.unfreezedRequest;
|
};
|
||||||
final mutation = {
|
final response =
|
||||||
'authorization': accessToken ?? '',
|
await httpClient.get(Uri.parse(tokenEndpoint), headers: headers);
|
||||||
};
|
final status = HttpStatus.from(response.statusCode);
|
||||||
Map<String, String>? headers = newReq.headers;
|
if (status.isSuccess()) {
|
||||||
if (headers != null) {
|
final body = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
headers.addAll(mutation);
|
accessToken = accessTokenParser(body);
|
||||||
} else {
|
|
||||||
headers = mutation;
|
|
||||||
}
|
|
||||||
newReq = newReq.copyWith(headers: headers);
|
|
||||||
request.updateUnfreezedRequest(newReq);
|
|
||||||
|
|
||||||
|
// Then modify current request with accessToken
|
||||||
|
var newReq = request.unfreezedRequest;
|
||||||
|
final mutation = {
|
||||||
|
authenticationHeader: '$authenticationMethod $accessToken',
|
||||||
|
};
|
||||||
|
Map<String, String>? headers = newReq.headers;
|
||||||
|
if (headers != null) {
|
||||||
|
headers.addAll(mutation);
|
||||||
|
} else {
|
||||||
|
headers = mutation;
|
||||||
|
}
|
||||||
|
newReq = newReq.copyWith(headers: headers);
|
||||||
|
request.updateUnfreezedRequest(newReq);
|
||||||
|
} else {
|
||||||
|
// Retry
|
||||||
|
int retries = 0;
|
||||||
|
while (retries < maxRetries) {
|
||||||
|
final Map<String, String> headers = {
|
||||||
|
authenticationHeader: '$authenticationHeader $refreshToken',
|
||||||
|
};
|
||||||
|
final response =
|
||||||
|
await httpClient.get(Uri.parse(tokenEndpoint), headers: headers);
|
||||||
|
final status = HttpStatus.from(response.statusCode);
|
||||||
|
if (status.isSuccess()) {
|
||||||
|
final body = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
accessToken = accessTokenParser(body);
|
||||||
|
|
||||||
|
// Then modify current request with accessToken
|
||||||
|
var newReq = request.unfreezedRequest;
|
||||||
|
final mutation = {
|
||||||
|
authenticationHeader: '$authenticationMethod $accessToken',
|
||||||
|
};
|
||||||
|
Map<String, String>? headers = newReq.headers;
|
||||||
|
if (headers != null) {
|
||||||
|
headers.addAll(mutation);
|
||||||
|
} else {
|
||||||
|
headers = mutation;
|
||||||
|
}
|
||||||
|
newReq = newReq.copyWith(headers: headers);
|
||||||
|
request.updateUnfreezedRequest(newReq);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
retries++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.onRequest(request);
|
||||||
|
}
|
||||||
|
// Pass
|
||||||
return super.onRequest(request);
|
return super.onRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<MiddlewareResponse> onResponse(MiddlewareResponse response) async {
|
||||||
|
final res = await super.onResponse(response);
|
||||||
|
final status = HttpStatus.from(res.httpResponse.statusCode);
|
||||||
|
if (res.context.originalRequest?.unfreezedRequest.url ==
|
||||||
|
Uri.parse(authorizationEndpoint)) {
|
||||||
|
if (status.isSuccess()) {
|
||||||
|
final body = jsonDecode((res.httpResponse as Response).body) as Map<String, dynamic>;
|
||||||
|
final accessToken = accessTokenParser(body);
|
||||||
|
final refreshToken = refreshTokenParser(body);
|
||||||
|
|
||||||
|
if (accessToken.isNotEmpty) {
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
}
|
||||||
|
if (refreshToken.isNotEmpty) {
|
||||||
|
this.refreshToken = refreshToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
if (status == unauthorized) {
|
||||||
|
print(
|
||||||
|
'${getName()}::OnResponse: $unauthorized',
|
||||||
|
);
|
||||||
|
// Refresh token then retry
|
||||||
|
final subPipeline = res.context.pipeline.fromUntil(this);
|
||||||
|
final httpClient = MiddlewareClient(
|
||||||
|
pipeline: subPipeline,
|
||||||
|
inner: client,
|
||||||
|
);
|
||||||
|
final Map<String, String> headers = {
|
||||||
|
authenticationHeader: '$authenticationHeader $refreshToken',
|
||||||
|
};
|
||||||
|
final response =
|
||||||
|
await httpClient.get(Uri.parse(tokenEndpoint), headers: headers);
|
||||||
|
final refreshstatus = HttpStatus.from(response.statusCode);
|
||||||
|
if (refreshstatus.isSuccess()) {
|
||||||
|
print(
|
||||||
|
'${getName()}::OnResponse: refresh successfuly',
|
||||||
|
);
|
||||||
|
final body = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
accessToken = accessTokenParser(body);
|
||||||
|
|
||||||
|
// Then modify current request with accessToken
|
||||||
|
final midReq = res.middlewareRequest;
|
||||||
|
final newReq = midReq.httpRequest;
|
||||||
|
final mutation = {
|
||||||
|
authenticationHeader: '$authenticationMethod $accessToken',
|
||||||
|
};
|
||||||
|
Map<String, String>? headers = newReq.headers;
|
||||||
|
if (headers != null) {
|
||||||
|
headers.addAll(mutation);
|
||||||
|
} else {
|
||||||
|
headers = mutation;
|
||||||
|
}
|
||||||
|
midReq.updateHttpRequest(headers: headers);
|
||||||
|
final newRes = await httpClient.send(midReq.httpRequest);
|
||||||
|
return res.copyWith(httpResponse: res as Response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,32 +14,29 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
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_request.dart';
|
||||||
import 'package:wyatt_http_client/src/models/middleware_response.dart';
|
import 'package:wyatt_http_client/src/models/middleware_response.dart';
|
||||||
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
|
||||||
class SimpleLoggerMiddleware extends Middleware {
|
class SimpleLoggerMiddleware extends Middleware {
|
||||||
SimpleLoggerMiddleware({
|
@override
|
||||||
super.child,
|
String getName() => 'SimpleLogger';
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getName() => 'SimpleLoggerMiddleware';
|
Future<MiddlewareRequest> onRequest(MiddlewareRequest request) {
|
||||||
|
|
||||||
@override
|
|
||||||
MiddlewareRequest onRequest(MiddlewareRequest request) {
|
|
||||||
print(
|
print(
|
||||||
'Logger::OnRequest: ${request.httpRequest.method} '
|
'${getName()}::OnRequest: ${request.httpRequest.method} '
|
||||||
'${request.httpRequest.url}',
|
'${request.httpRequest.url}\n${request.unfreezedRequest.headers}'
|
||||||
|
'\n>> ${request.unfreezedRequest.body}',
|
||||||
);
|
);
|
||||||
return super.onRequest(request);
|
return super.onRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MiddlewareResponse onResponse(MiddlewareResponse response) {
|
Future<MiddlewareResponse> onResponse(MiddlewareResponse response) async {
|
||||||
final res = super.onResponse(response);
|
final res = await super.onResponse(response);
|
||||||
print(
|
print(
|
||||||
'Logger::OnResponse: ${res.httpResponse.statusCode} -> '
|
'${getName()}::OnResponse: ${res.httpResponse.statusCode} -> '
|
||||||
'received ${res.httpResponse.contentLength} bytes',
|
'received ${res.httpResponse.contentLength} bytes',
|
||||||
);
|
);
|
||||||
return res;
|
return res;
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
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_request.dart';
|
||||||
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
import 'package:wyatt_http_client/src/utils/protocols.dart';
|
||||||
|
|
||||||
class UriPrefixMiddleware extends Middleware {
|
class UriPrefixMiddleware extends Middleware {
|
||||||
@ -25,17 +25,16 @@ class UriPrefixMiddleware extends Middleware {
|
|||||||
UriPrefixMiddleware({
|
UriPrefixMiddleware({
|
||||||
required this.protocol,
|
required this.protocol,
|
||||||
required this.authority,
|
required this.authority,
|
||||||
super.child,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getName() => 'UriPrefixMiddleware';
|
String getName() => 'UriPrefix';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MiddlewareRequest onRequest(MiddlewareRequest request) {
|
Future<MiddlewareRequest> onRequest(MiddlewareRequest request) {
|
||||||
final Uri uri =
|
final Uri uri =
|
||||||
Uri.parse('${protocol.scheme}$authority${request.httpRequest.url}');
|
Uri.parse('${protocol.scheme}$authority${request.httpRequest.url}');
|
||||||
print('UriPrefix::OnRequest: ${request.httpRequest.url} -> $uri');
|
print('${getName()}::OnRequest: ${request.httpRequest.url} -> $uri');
|
||||||
request.updateHttpRequest(url: uri);
|
request.updateHttpRequest(url: uri);
|
||||||
return super.onRequest(request);
|
return super.onRequest(request);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,48 @@
|
|||||||
|
// 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:wyatt_http_client/src/models/middleware_request.dart';
|
||||||
|
import 'package:wyatt_http_client/src/models/middleware_response.dart';
|
||||||
|
import 'package:wyatt_http_client/src/pipeline.dart';
|
||||||
|
|
||||||
|
class MiddlewareContext {
|
||||||
|
Pipeline pipeline;
|
||||||
|
MiddlewareRequest? originalRequest;
|
||||||
|
MiddlewareResponse? originalResponse;
|
||||||
|
|
||||||
|
MiddlewareContext({
|
||||||
|
required this.pipeline,
|
||||||
|
this.originalRequest,
|
||||||
|
this.originalResponse,
|
||||||
|
});
|
||||||
|
|
||||||
|
MiddlewareContext copyWith({
|
||||||
|
Pipeline? pipeline,
|
||||||
|
MiddlewareRequest? originalRequest,
|
||||||
|
MiddlewareResponse? originalResponse,
|
||||||
|
}) {
|
||||||
|
return MiddlewareContext(
|
||||||
|
pipeline: pipeline ?? this.pipeline,
|
||||||
|
originalRequest: originalRequest ?? this.originalRequest,
|
||||||
|
originalResponse: originalResponse ?? this.originalResponse,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => 'MiddlewareContext(pipeline: $pipeline, '
|
||||||
|
'originalRequest: $originalRequest, originalResponse: $originalResponse)';
|
||||||
|
}
|
@ -16,25 +16,33 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
|
|
||||||
|
import 'package:wyatt_http_client/src/models/middleware_context.dart';
|
||||||
import 'package:wyatt_http_client/src/models/unfreezed_request.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/utils.dart';
|
||||||
|
|
||||||
class MiddlewareRequest {
|
class MiddlewareRequest {
|
||||||
UnfreezedRequest unfreezedRequest;
|
UnfreezedRequest unfreezedRequest;
|
||||||
Request httpRequest;
|
Request httpRequest;
|
||||||
|
MiddlewareContext context;
|
||||||
|
|
||||||
MiddlewareRequest({
|
MiddlewareRequest({
|
||||||
required this.unfreezedRequest,
|
required this.unfreezedRequest,
|
||||||
required this.httpRequest,
|
required this.httpRequest,
|
||||||
});
|
required this.context,
|
||||||
|
}) {
|
||||||
|
context = context.copyWith(originalRequest: this);
|
||||||
|
}
|
||||||
|
|
||||||
MiddlewareRequest copyWith({
|
MiddlewareRequest copyWith({
|
||||||
UnfreezedRequest? unfreezedRequest,
|
UnfreezedRequest? unfreezedRequest,
|
||||||
Request? httpRequest,
|
Request? httpRequest,
|
||||||
|
MiddlewareContext? context,
|
||||||
}) {
|
}) {
|
||||||
return MiddlewareRequest(
|
return MiddlewareRequest(
|
||||||
unfreezedRequest: unfreezedRequest ?? this.unfreezedRequest,
|
unfreezedRequest: unfreezedRequest ?? this.unfreezedRequest,
|
||||||
httpRequest: httpRequest ?? this.httpRequest,
|
httpRequest: httpRequest ?? this.httpRequest,
|
||||||
|
context: context ?? this.context,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,5 +93,5 @@ class MiddlewareRequest {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() => 'MiddlewareRequest(unfreezedRequest: '
|
String toString() => 'MiddlewareRequest(unfreezedRequest: '
|
||||||
'$unfreezedRequest, httpRequest: $httpRequest)';
|
'$unfreezedRequest, httpRequest: $httpRequest, context: $context)';
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,35 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
|
import 'package:wyatt_http_client/src/models/middleware_context.dart';
|
||||||
|
import 'package:wyatt_http_client/src/models/middleware_request.dart';
|
||||||
|
|
||||||
class MiddlewareResponse {
|
class MiddlewareResponse {
|
||||||
BaseResponse httpResponse;
|
BaseResponse httpResponse;
|
||||||
|
MiddlewareRequest middlewareRequest;
|
||||||
|
MiddlewareContext context;
|
||||||
|
|
||||||
MiddlewareResponse({
|
MiddlewareResponse({
|
||||||
required this.httpResponse,
|
required this.httpResponse,
|
||||||
});
|
required this.middlewareRequest,
|
||||||
|
required this.context,
|
||||||
|
}) {
|
||||||
|
context = context.copyWith(originalResponse: this);
|
||||||
|
}
|
||||||
|
|
||||||
MiddlewareResponse copyWith({
|
MiddlewareResponse copyWith({
|
||||||
BaseResponse? httpResponse,
|
BaseResponse? httpResponse,
|
||||||
|
MiddlewareRequest? middlewareRequest,
|
||||||
|
MiddlewareContext? context,
|
||||||
}) {
|
}) {
|
||||||
return MiddlewareResponse(
|
return MiddlewareResponse(
|
||||||
httpResponse: httpResponse ?? this.httpResponse,
|
httpResponse: httpResponse ?? this.httpResponse,
|
||||||
|
middlewareRequest: middlewareRequest ?? this.middlewareRequest,
|
||||||
|
context: context ?? this.context,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() => 'MiddlewareResponse(httpResponse: $httpResponse)';
|
String toString() => 'MiddlewareResponse(httpResponse: $httpResponse, '
|
||||||
|
'middlewareRequest: $middlewareRequest, context: $context)';
|
||||||
}
|
}
|
||||||
|
@ -14,44 +14,140 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import 'package:wyatt_http_client/src/middleware.dart';
|
import 'package:http/http.dart';
|
||||||
import 'package:wyatt_http_client/src/middlewares/default_middleware.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';
|
||||||
|
|
||||||
|
part 'middleware.dart';
|
||||||
|
part 'middleware_node.dart';
|
||||||
|
|
||||||
class Pipeline {
|
class Pipeline {
|
||||||
final Middleware _middleware;
|
int _length = 0;
|
||||||
|
late final MiddlewareNode begin;
|
||||||
|
late final MiddlewareNode end;
|
||||||
|
|
||||||
Pipeline() : _middleware = DefaultMiddleware();
|
MiddlewareNode get first => begin.child;
|
||||||
|
MiddlewareNode get last => end.parent;
|
||||||
|
bool get isEmpty => _length == 0;
|
||||||
|
bool get isNotEmpty => !isEmpty;
|
||||||
|
int get length => _length;
|
||||||
|
|
||||||
|
Pipeline() {
|
||||||
|
_initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
Pipeline.fromIterable(Iterable<Middleware?> list) {
|
||||||
|
_initialize();
|
||||||
|
MiddlewareNode parent = begin;
|
||||||
|
for (final element in list) {
|
||||||
|
if (element != null) {
|
||||||
|
parent = parent.insertAfter(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Pipeline.from(Pipeline pipeline)
|
||||||
|
: this.fromIterable(pipeline.middlewares.map((node) => node.middleware));
|
||||||
|
|
||||||
|
void _initialize() {
|
||||||
|
_length = 0;
|
||||||
|
begin = MiddlewareNode._begin(this);
|
||||||
|
end = MiddlewareNode._end(this);
|
||||||
|
begin._child = end;
|
||||||
|
end._parent = begin;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<MiddlewareNode> get middlewares sync* {
|
||||||
|
for (var middleware = first;
|
||||||
|
middleware != end;
|
||||||
|
middleware = middleware.child) {
|
||||||
|
yield middleware;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int indexOf(MiddlewareNode node) {
|
||||||
|
int i = -1;
|
||||||
|
int j = -1;
|
||||||
|
for (final element in middlewares) {
|
||||||
|
j++;
|
||||||
|
if (element == node) {
|
||||||
|
i = j;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int indexOfChild(Middleware middleware) {
|
||||||
|
int i = -1;
|
||||||
|
int j = -1;
|
||||||
|
for (final element in middlewares) {
|
||||||
|
j++;
|
||||||
|
if (element.middleware == middleware) {
|
||||||
|
i = j;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create new [Pipeline] from first [Middleware] to the specified one.
|
||||||
|
Pipeline fromUntil(Middleware middleware, {bool include = false}) {
|
||||||
|
final nodes = <Middleware?>[];
|
||||||
|
for (final element in middlewares) {
|
||||||
|
if (element.middleware != middleware) {
|
||||||
|
nodes.add(element.middleware);
|
||||||
|
}
|
||||||
|
if (element.middleware == middleware) {
|
||||||
|
if (include) {
|
||||||
|
nodes.add(element.middleware);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Pipeline.fromIterable(nodes);
|
||||||
|
}
|
||||||
|
|
||||||
Pipeline addMiddleware(Middleware middleware) {
|
Pipeline addMiddleware(Middleware middleware) {
|
||||||
_middleware.addChild(middleware);
|
last.insertAfter(middleware);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Middleware get middleware {
|
void setClient(MiddlewareClient? client) {
|
||||||
return _middleware;
|
for (var node = first; node != end; node = node.child) {
|
||||||
|
node.middleware?.httpClient = client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Pipeline operator +(Pipeline other) {
|
Future<MiddlewareRequest> onRequest(MiddlewareRequest request) async {
|
||||||
final copy = _middleware.deepCopy()..addChild(other.middleware);
|
MiddlewareRequest req = request;
|
||||||
return Pipeline()..addMiddleware(copy);
|
for (var node = first; node != end; node = node.child) {
|
||||||
|
req = await node.middleware?.onRequest(req) ?? req;
|
||||||
|
}
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<MiddlewareResponse> onResponse(MiddlewareResponse response) async {
|
||||||
|
MiddlewareResponse res = response;
|
||||||
|
for (var node = last; node != begin; node = node.parent) {
|
||||||
|
res = await node.middleware?.onResponse(res) ?? res;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
String getLogic() {
|
String getLogic() {
|
||||||
final req = <String>[];
|
final req = <String>[];
|
||||||
final res = <String>[];
|
final res = <String>[];
|
||||||
Middleware? m = _middleware;
|
for (final m in middlewares) {
|
||||||
while (m != null) {
|
req.add('${m.middleware}');
|
||||||
if (m is! DefaultMiddleware) {
|
res.insert(0, '${m.middleware}');
|
||||||
req.add('$m');
|
|
||||||
res.insert(0, '$m');
|
|
||||||
}
|
|
||||||
m = m.child;
|
|
||||||
}
|
}
|
||||||
return '[Req] -> ${req.join(' -> ')}\n[Res] -> ${res.join(' -> ')}';
|
return '[Req] -> ${req.join(' -> ')}\n[Res] -> ${res.join(' -> ')}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return middleware.toString();
|
return getLogic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
packages/wyatt_http_client/lib/src/utils/http_methods.dart
Normal file
28
packages/wyatt_http_client/lib/src/utils/http_methods.dart
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
enum HttpMethods {
|
||||||
|
head('HEAD'),
|
||||||
|
get('GET'),
|
||||||
|
post('POST'),
|
||||||
|
put('PUT'),
|
||||||
|
patch('PATCH'),
|
||||||
|
delete('DELETE');
|
||||||
|
|
||||||
|
final String method;
|
||||||
|
|
||||||
|
const HttpMethods(this.method);
|
||||||
|
}
|
@ -15,69 +15,109 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/// Status codes for HTTP responses. Extracted from dart:io
|
/// Status codes for HTTP responses. Extracted from dart:io
|
||||||
abstract class HttpStatus {
|
enum HttpStatus {
|
||||||
static const int continue_ = 100;
|
continue_(100),
|
||||||
static const int switchingProtocols = 101;
|
switchingProtocols(101),
|
||||||
static const int processing = 102;
|
processing(102),
|
||||||
static const int ok = 200;
|
ok(200),
|
||||||
static const int created = 201;
|
created(201),
|
||||||
static const int accepted = 202;
|
accepted(202),
|
||||||
static const int nonAuthoritativeInformation = 203;
|
nonAuthoritativeInformation(203),
|
||||||
static const int noContent = 204;
|
noContent(204),
|
||||||
static const int resetContent = 205;
|
resetContent(205),
|
||||||
static const int partialContent = 206;
|
partialContent(206),
|
||||||
static const int multiStatus = 207;
|
multiStatus(207),
|
||||||
static const int alreadyReported = 208;
|
alreadyReported(208),
|
||||||
static const int imUsed = 226;
|
imUsed(226),
|
||||||
static const int multipleChoices = 300;
|
multipleChoices(300),
|
||||||
static const int movedPermanently = 301;
|
movedPermanently(301),
|
||||||
static const int found = 302;
|
found(302),
|
||||||
static const int movedTemporarily = 302; // Common alias for found.
|
movedTemporarily(302), // Common alias for found.
|
||||||
static const int seeOther = 303;
|
seeOther(303),
|
||||||
static const int notModified = 304;
|
notModified(304),
|
||||||
static const int useProxy = 305;
|
useProxy(305),
|
||||||
static const int temporaryRedirect = 307;
|
temporaryRedirect(307),
|
||||||
static const int permanentRedirect = 308;
|
permanentRedirect(308),
|
||||||
static const int badRequest = 400;
|
badRequest(400),
|
||||||
static const int unauthorized = 401;
|
unauthorized(401),
|
||||||
static const int paymentRequired = 402;
|
paymentRequired(402),
|
||||||
static const int forbidden = 403;
|
forbidden(403),
|
||||||
static const int notFound = 404;
|
notFound(404),
|
||||||
static const int methodNotAllowed = 405;
|
methodNotAllowed(405),
|
||||||
static const int notAcceptable = 406;
|
notAcceptable(406),
|
||||||
static const int proxyAuthenticationRequired = 407;
|
proxyAuthenticationRequired(407),
|
||||||
static const int requestTimeout = 408;
|
requestTimeout(408),
|
||||||
static const int conflict = 409;
|
conflict(409),
|
||||||
static const int gone = 410;
|
gone(410),
|
||||||
static const int lengthRequired = 411;
|
lengthRequired(411),
|
||||||
static const int preconditionFailed = 412;
|
preconditionFailed(412),
|
||||||
static const int requestEntityTooLarge = 413;
|
requestEntityTooLarge(413),
|
||||||
static const int requestUriTooLong = 414;
|
requestUriTooLong(414),
|
||||||
static const int unsupportedMediaType = 415;
|
unsupportedMediaType(415),
|
||||||
static const int requestedRangeNotSatisfiable = 416;
|
requestedRangeNotSatisfiable(416),
|
||||||
static const int expectationFailed = 417;
|
expectationFailed(417),
|
||||||
static const int misdirectedRequest = 421;
|
misdirectedRequest(421),
|
||||||
static const int unprocessableEntity = 422;
|
unprocessableEntity(422),
|
||||||
static const int locked = 423;
|
locked(423),
|
||||||
static const int failedDependency = 424;
|
failedDependency(424),
|
||||||
static const int upgradeRequired = 426;
|
upgradeRequired(426),
|
||||||
static const int preconditionRequired = 428;
|
preconditionRequired(428),
|
||||||
static const int tooManyRequests = 429;
|
tooManyRequests(429),
|
||||||
static const int requestHeaderFieldsTooLarge = 431;
|
requestHeaderFieldsTooLarge(431),
|
||||||
static const int connectionClosedWithoutResponse = 444;
|
connectionClosedWithoutResponse(444),
|
||||||
static const int unavailableForLegalReasons = 451;
|
unavailableForLegalReasons(451),
|
||||||
static const int clientClosedRequest = 499;
|
clientClosedRequest(499),
|
||||||
static const int internalServerError = 500;
|
internalServerError(500),
|
||||||
static const int notImplemented = 501;
|
notImplemented(501),
|
||||||
static const int badGateway = 502;
|
badGateway(502),
|
||||||
static const int serviceUnavailable = 503;
|
serviceUnavailable(503),
|
||||||
static const int gatewayTimeout = 504;
|
gatewayTimeout(504),
|
||||||
static const int httpVersionNotSupported = 505;
|
httpVersionNotSupported(505),
|
||||||
static const int variantAlsoNegotiates = 506;
|
variantAlsoNegotiates(506),
|
||||||
static const int insufficientStorage = 507;
|
insufficientStorage(507),
|
||||||
static const int loopDetected = 508;
|
loopDetected(508),
|
||||||
static const int notExtended = 510;
|
notExtended(510),
|
||||||
static const int networkAuthenticationRequired = 511;
|
networkAuthenticationRequired(511),
|
||||||
// Client generated status code.
|
// Client generated status code.
|
||||||
static const int networkConnectTimeoutError = 599;
|
networkConnectTimeoutError(599);
|
||||||
|
|
||||||
|
final int statusCode;
|
||||||
|
|
||||||
|
const HttpStatus(this.statusCode);
|
||||||
|
|
||||||
|
bool equals(Object other) {
|
||||||
|
if (other is HttpStatus) {
|
||||||
|
return statusCode == other.statusCode;
|
||||||
|
}
|
||||||
|
if (other is int) {
|
||||||
|
return statusCode == other;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isInfo() {
|
||||||
|
return statusCode >= 100 && statusCode < 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isSuccess() {
|
||||||
|
return statusCode >= 200 && statusCode < 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isRedirection() {
|
||||||
|
return statusCode >= 300 && statusCode < 400;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isClientError() {
|
||||||
|
return statusCode >= 400 && statusCode < 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isServerError() {
|
||||||
|
return statusCode >= 500 && statusCode < 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
factory HttpStatus.from(int status) {
|
||||||
|
return HttpStatus.values
|
||||||
|
.firstWhere((element) => element.statusCode == status);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user