@@ -5,12 +5,28 @@ import 'package:http_interceptor/extensions/extensions.dart';
55import 'package:http_interceptor/http/http.dart' ;
66import 'package:http_interceptor/utils/utils.dart' ;
77
8+ /// A class that mimics HTTP Request in order to intercept it's data.
89class RequestData {
10+ /// The HTTP method of the request.
11+ ///
12+ /// Most commonly "GET" or "POST", less commonly "HEAD", "PUT", or "DELETE".
13+ /// Non-standard method names are also supported.
914 Method method;
15+
16+ /// The base URL String to which the request will be sent. It does not include
17+ /// the query parameters.
1018 String baseUrl;
19+
20+ /// Map of String to String that represents the headers of the request.
1121 Map <String , String > headers;
22+
23+ /// Map of String to String that represents the query parameters of the
24+ /// request.
1225 Map <String , dynamic > params;
13- dynamic ? body;
26+
27+ dynamic body;
28+
29+ /// The encoding used for the request.
1430 Encoding ? encoding;
1531
1632 RequestData ({
@@ -23,24 +39,38 @@ class RequestData {
2339 }) : headers = headers ?? {},
2440 params = params ?? {};
2541
42+ /// The complete URL String including query parameters to which the request
43+ /// will be sent.
2644 String get url => buildUrlString (baseUrl, params);
2745
28- factory RequestData .fromHttpRequest (Request request) {
46+ /// Creates a new request data from an HTTP request.
47+ ///
48+ /// For now it only supports [Request] .
49+ /// TODO(codingalecr): Support for [MultipartRequest] and [StreamedRequest] .
50+ factory RequestData .fromHttpRequest (BaseRequest request) {
2951 var params = Map <String , dynamic >();
3052 request.url.queryParametersAll.forEach ((key, value) {
3153 params[key] = value;
3254 });
3355 String baseUrl = request.url.origin + request.url.path;
34- return RequestData (
35- method: methodFromString (request.method),
36- encoding: request.encoding,
37- body: request.body,
38- baseUrl: baseUrl,
39- headers: request.headers,
40- params: params,
56+
57+ if (request is Request ) {
58+ return RequestData (
59+ method: methodFromString (request.method),
60+ baseUrl: baseUrl,
61+ headers: request.headers,
62+ body: request.body,
63+ encoding: request.encoding,
64+ params: params,
65+ );
66+ }
67+
68+ throw UnsupportedError (
69+ "Can't intercept ${request .runtimeType }. Request type not supported yet." ,
4170 );
4271 }
4372
73+ /// Converts this request data to an HTTP request.
4474 Request toHttpRequest () {
4575 var reqUrl = buildUrlString (baseUrl, params);
4676
@@ -50,7 +80,7 @@ class RequestData {
5080 if (encoding != null ) request.encoding = encoding! ;
5181 if (body != null ) {
5282 if (body is String ) {
53- request.body = body;
83+ request.body = body as String ;
5484 } else if (body is List ) {
5585 request.bodyBytes = body? .cast <int >();
5686 } else if (body is Map ) {
@@ -63,6 +93,7 @@ class RequestData {
6393 return request;
6494 }
6595
96+ /// Convenient toString implementation for logging.
6697 @override
6798 String toString () {
6899 return 'Request Data { $method , $baseUrl , $headers , $params , $body }' ;
0 commit comments