feat(http): add new middleware feature
This commit is contained in:
@@ -16,35 +16,37 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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';
|
||||
import 'package:wyatt_http_client/src/utils/http_status.dart';
|
||||
|
||||
class MiddlewareResponse {
|
||||
BaseResponse httpResponse;
|
||||
MiddlewareRequest middlewareRequest;
|
||||
MiddlewareContext context;
|
||||
|
||||
// Proxy
|
||||
int get statusCode => httpResponse.statusCode;
|
||||
HttpStatus get status => HttpStatus.from(statusCode);
|
||||
String get body {
|
||||
if (httpResponse is Response) {
|
||||
return (httpResponse as Response).body;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
int? get contentLength => httpResponse.contentLength;
|
||||
Map<String, String> get headers => httpResponse.headers;
|
||||
|
||||
MiddlewareResponse({
|
||||
required this.httpResponse,
|
||||
required this.middlewareRequest,
|
||||
required this.context,
|
||||
}) {
|
||||
context = context.copyWith(originalResponse: this);
|
||||
}
|
||||
});
|
||||
|
||||
MiddlewareResponse copyWith({
|
||||
BaseResponse? httpResponse,
|
||||
MiddlewareRequest? middlewareRequest,
|
||||
MiddlewareContext? context,
|
||||
}) {
|
||||
return MiddlewareResponse(
|
||||
httpResponse: httpResponse ?? this.httpResponse,
|
||||
middlewareRequest: middlewareRequest ?? this.middlewareRequest,
|
||||
context: context ?? this.context,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'MiddlewareResponse(httpResponse: $httpResponse, '
|
||||
'middlewareRequest: $middlewareRequest, context: $context)';
|
||||
String toString() =>
|
||||
'MiddlewareResponse(httpResponse: $httpResponse)';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user