feat(http): implements doublelinked list for middlewares
This commit is contained in:
@@ -17,8 +17,13 @@
|
||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:wyatt_http_client/src/authentication/refresh_token_client.dart';
|
||||
import 'package:wyatt_http_client/src/rest_client.dart';
|
||||
import 'package:wyatt_http_client/src/middleware_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';
|
||||
|
||||
enum EmailVerificationAction {
|
||||
@@ -262,24 +267,14 @@ class Login {
|
||||
|
||||
class FastAPI {
|
||||
final String baseUrl;
|
||||
final RefreshTokenClient client;
|
||||
final MiddlewareClient client;
|
||||
final int apiVersion;
|
||||
|
||||
FastAPI({
|
||||
this.baseUrl = 'localhost:80',
|
||||
RefreshTokenClient? client,
|
||||
MiddlewareClient? client,
|
||||
this.apiVersion = 1,
|
||||
}) : client = client ??
|
||||
RefreshTokenClient(
|
||||
authorizationEndpoint: '',
|
||||
tokenEndpoint: '',
|
||||
accessTokenParser: (body) => body['access_token']! as String,
|
||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||
inner: RestClient(
|
||||
protocol: Protocols.http,
|
||||
authority: baseUrl,
|
||||
),
|
||||
);
|
||||
}) : client = client ?? MiddlewareClient();
|
||||
|
||||
String get apiPath => '/api/v$apiVersion';
|
||||
|
||||
@@ -323,14 +318,21 @@ class FastAPI {
|
||||
}
|
||||
|
||||
Future<TokenSuccess> signInWithPassword(Login login) async {
|
||||
final r = await client.authorize(login.toMap());
|
||||
return TokenSuccess.fromJson(r.body);
|
||||
final r = await client.post(
|
||||
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 {
|
||||
final r = await client.refresh();
|
||||
return TokenSuccess.fromJson(r?.body ?? '');
|
||||
}
|
||||
// Future<TokenSuccess> refresh() async {
|
||||
// final r = await client.refresh();
|
||||
// return TokenSuccess.fromJson(r?.body ?? '');
|
||||
// }
|
||||
|
||||
Future<List<Account>> getAccountList() async {
|
||||
final r = await client.get(
|
||||
@@ -351,17 +353,30 @@ class FastAPI {
|
||||
}
|
||||
|
||||
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(
|
||||
client: RefreshTokenClient(
|
||||
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',
|
||||
),
|
||||
),
|
||||
client: client,
|
||||
);
|
||||
|
||||
// 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 {
|
||||
final Pipeline pipeline1 = Pipeline()
|
||||
final Pipeline pipeline = Pipeline()
|
||||
.addMiddleware(SimpleLoggerMiddleware())
|
||||
.addMiddleware(
|
||||
UriPrefixMiddleware(
|
||||
@@ -125,21 +125,18 @@ Future<void> main(List<String> args) async {
|
||||
authority: 'localhost:80',
|
||||
),
|
||||
)
|
||||
.addMiddleware(BodyToJsonMiddleware());
|
||||
|
||||
final Pipeline pipeline2 = Pipeline().addMiddleware(
|
||||
RefreshTokenMiddleware(
|
||||
authorizationEndpoint:
|
||||
'http://localhost:80/api/v1/account/test?action=authorize',
|
||||
tokenEndpoint: 'http://localhost:80/api/v1/account/test?action=refresh',
|
||||
innerClientMiddlewares: pipeline1.middleware,
|
||||
),
|
||||
);
|
||||
|
||||
final Pipeline pipeline = pipeline1 + pipeline2;
|
||||
.addMiddleware(BodyToJsonMiddleware())
|
||||
.addMiddleware(
|
||||
RefreshTokenMiddleware(
|
||||
authorizationEndpoint: '/api/v1/account/test?action=authorize',
|
||||
tokenEndpoint: '/api/v1/account/test?action=refresh',
|
||||
accessTokenParser: (body) => body['access_token']! as String,
|
||||
refreshTokenParser: (body) => body['refresh_token']! as String,
|
||||
),
|
||||
);
|
||||
|
||||
print(pipeline.getLogic());
|
||||
final client = MiddlewareClient(pipeline);
|
||||
final client = MiddlewareClient(pipeline: pipeline);
|
||||
final r = await client.post(
|
||||
Uri.parse('/api/v1/account/test'),
|
||||
body: <String, String>{
|
||||
|
||||
Reference in New Issue
Block a user