feat(http): add new middleware feature
This commit is contained in:
@@ -33,7 +33,7 @@ class MiddlewareClient extends BaseClient {
|
||||
Client? inner,
|
||||
}) : pipeline = pipeline ?? Pipeline(),
|
||||
inner = inner ?? Client() {
|
||||
this.pipeline.setClient(this);
|
||||
print('Using Pipeline:\n$pipeline');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -100,27 +100,30 @@ class MiddlewareClient extends BaseClient {
|
||||
body: body,
|
||||
encoding: encoding,
|
||||
),
|
||||
httpRequest: Request(method, url),
|
||||
context: MiddlewareContext(pipeline: pipeline),
|
||||
);
|
||||
final requestContext = MiddlewareContext(
|
||||
pipeline: pipeline,
|
||||
client: this,
|
||||
originalRequest: originalRequest,
|
||||
);
|
||||
|
||||
final modifiedRequest = await pipeline.onRequest(
|
||||
requestContext,
|
||||
originalRequest.copyWith(),
|
||||
);
|
||||
|
||||
final res = await Response.fromStream(
|
||||
await send(modifiedRequest.httpRequest),
|
||||
);
|
||||
final response = await pipeline.onResponse(
|
||||
MiddlewareResponse(
|
||||
httpResponse: res,
|
||||
middlewareRequest: modifiedRequest,
|
||||
context: MiddlewareContext(
|
||||
pipeline: pipeline,
|
||||
originalRequest: originalRequest,
|
||||
),
|
||||
final originalResponse = MiddlewareResponse(
|
||||
httpResponse: await Response.fromStream(
|
||||
await send(modifiedRequest.request),
|
||||
),
|
||||
);
|
||||
|
||||
return response.httpResponse as Response;
|
||||
final responseContext =
|
||||
requestContext.copyWith(originalResponse: originalResponse);
|
||||
|
||||
final modifiedResponse =
|
||||
await pipeline.onResponse(responseContext, originalResponse.copyWith());
|
||||
|
||||
return modifiedResponse.httpResponse as Response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user