Skip to content

Commit b667918

Browse files
committed
Address comments
1 parent 206a7ff commit b667918

File tree

1 file changed

+93
-51
lines changed

1 file changed

+93
-51
lines changed

pkgs/http_profile/lib/http_profile.dart

Lines changed: 93 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ final class HttpProfileRequestEvent {
1111
final int _timestamp;
1212
final String _name;
1313

14-
/// [timestamp] should be the time at which the event occurred, as a
15-
/// microsecond value on the monotonic clock used by the [Timeline].
16-
HttpProfileRequestEvent({required int timestamp, required String name})
17-
: _timestamp = timestamp,
14+
HttpProfileRequestEvent({required DateTime timestamp, required String name})
15+
: _timestamp = timestamp.microsecondsSinceEpoch,
1816
_name = name;
1917

2018
Map<String, dynamic> _toJson() => <String, dynamic>{
@@ -30,9 +28,12 @@ final class HttpProfileProxyData {
3028
final bool? _isDirect;
3129
final int? _port;
3230

33-
HttpProfileProxyData(
34-
{String? host, String? username, bool? isDirect, int? port})
35-
: _host = host,
31+
HttpProfileProxyData({
32+
String? host,
33+
String? username,
34+
bool? isDirect,
35+
int? port,
36+
}) : _host = host,
3637
_username = username,
3738
_isDirect = isDirect,
3839
_port = port;
@@ -45,15 +46,48 @@ final class HttpProfileProxyData {
4546
};
4647
}
4748

49+
/// Describes a redirect that an HTTP connection went through.
50+
class HttpProfileRedirectData {
51+
int _statusCode;
52+
String _method;
53+
String _location;
54+
55+
HttpProfileRedirectData({
56+
required int statusCode,
57+
required String method,
58+
required String location,
59+
}) : _statusCode = statusCode,
60+
_method = method,
61+
_location = location;
62+
63+
Map<String, dynamic> _toJson() => <String, dynamic>{
64+
'statusCode': _statusCode,
65+
'method': _method,
66+
'location': _location,
67+
};
68+
}
69+
4870
/// Describes details about an HTTP request.
4971
final class HttpProfileRequestData {
5072
final Map<String, dynamic> _data;
5173

5274
final void Function() _updated;
5375

54-
/// The elements of [connectionInfo] can either be [String]s or [int]s.
76+
/// Information about the networking connection used in the HTTP request.
77+
///
78+
/// This information is meant to be used for debugging.
79+
///
80+
/// It can contain any arbitrary data as long as the values are of type
81+
/// [String] or [int]. For example:
82+
/// { 'localPort': 1285, 'remotePort': 443, 'connectionPoolId': '21x23' }
5583
set connectionInfo(Map<String, dynamic /*String|int*/ > value) {
56-
_data['connectionInfo'] = value;
84+
for (final v in value.values) {
85+
if (!(v is String || v is int)) {
86+
throw ArgumentError(
87+
"The values in connectionInfo must be of type String or int.");
88+
}
89+
}
90+
_data['connectionInfo'] = {...value};
5791
_updated();
5892
}
5993

@@ -64,30 +98,29 @@ final class HttpProfileRequestData {
6498
}
6599

66100
/// The cookies presented to the server (in the 'cookie' header).
67-
set cookies(List<String> value) {
68-
_data['cookies'] = value;
101+
set cookies(List<Cookie> value) {
102+
_data['cookies'] = [for (final cookie in value) cookie.toString()];
69103
_updated();
70104
}
71105

72-
/// The error associated with the failed request.
106+
/// The error associated with a failed request.
73107
set error(String value) {
74108
_data['error'] = value;
75109
_updated();
76110
}
77111

78-
/// Whether redirects were followed automatically.
112+
/// Whether automatic redirect following was enabled for the request.
79113
set followRedirects(bool value) {
80114
_data['followRedirects'] = value;
81115
_updated();
82116
}
83117

84118
set headers(Map<String, List<String>> value) {
85-
_data['headers'] = value;
119+
_data['headers'] = {...value};
86120
_updated();
87121
}
88122

89-
/// If [followRedirects] is true, this is the maximum number of redirects that
90-
/// were followed.
123+
/// The maximum of redirects allowed during the request.
91124
set maxRedirects(int value) {
92125
_data['maxRedirects'] = value;
93126
_updated();
@@ -105,8 +138,10 @@ final class HttpProfileRequestData {
105138
_updated();
106139
}
107140

108-
HttpProfileRequestData._(
109-
Map<String, dynamic> this._data, void Function() this._updated);
141+
const HttpProfileRequestData._(
142+
Map<String, dynamic> this._data,
143+
void Function() this._updated,
144+
);
110145
}
111146

112147
/// Describes details about a response to an HTTP request.
@@ -115,27 +150,38 @@ final class HttpProfileResponseData {
115150

116151
final void Function() _updated;
117152

118-
/// Records a redirect that the connection went through. The elements of
119-
/// [redirect] can either be [String]s or [int]s.
120-
void addRedirect(Map<String, dynamic /*String|int*/ > redirect) {
121-
_data['redirects'].add(redirect);
153+
/// Records a redirect that the connection went through.
154+
void addRedirect(HttpProfileRedirectData redirect) {
155+
_data['redirects'].add(redirect._toJson());
122156
_updated();
123157
}
124158

125159
/// The cookies set by the server (from the 'set-cookie' header).
126-
set cookies(List<String> value) {
127-
_data['cookies'] = value;
160+
set cookies(List<Cookie> value) {
161+
_data['cookies'] = [for (final cookie in value) cookie.toString()];
128162
_updated();
129163
}
130164

131-
/// The elements of [connectionInfo] can either be [String]s or [int]s.
165+
/// Information about the networking connection used in the HTTP response.
166+
///
167+
/// This information is meant to be used for debugging.
168+
///
169+
/// It can contain any arbitrary data as long as the values are of type
170+
/// [String] or [int]. For example:
171+
/// { 'localPort': 1285, 'remotePort': 443, 'connectionPoolId': '21x23' }
132172
set connectionInfo(Map<String, dynamic /*String|int*/ > value) {
133-
_data['connectionInfo'] = value;
173+
for (final v in value.values) {
174+
if (!(v is String || v is int)) {
175+
throw ArgumentError(
176+
"The values in connectionInfo must be of type String or int.");
177+
}
178+
}
179+
_data['connectionInfo'] = {...value};
134180
_updated();
135181
}
136182

137183
set headers(Map<String, List<String>> value) {
138-
_data['headers'] = value;
184+
_data['headers'] = {...value};
139185
_updated();
140186
}
141187

@@ -144,8 +190,8 @@ final class HttpProfileResponseData {
144190
// This specifies whether the response bytes were compressed when they were
145191
// received across the wire and whether callers will receive compressed or
146192
// uncompressed bytes when they listen to the response body byte stream.
147-
set compressionState(String value) {
148-
_data['compressionState'] = value;
193+
set compressionState(HttpClientResponseCompressionState value) {
194+
_data['compressionState'] = value.name;
149195
_updated();
150196
}
151197

@@ -177,29 +223,29 @@ final class HttpProfileResponseData {
177223
_updated();
178224
}
179225

180-
/// The time at which the initial response was received, as a microsecond
181-
/// value on the monotonic clock used by the [Timeline].
182-
set startTime(int value) {
183-
_data['startTime'] = value;
226+
/// The time at which the initial response was received.
227+
set startTime(DateTime value) {
228+
_data['startTime'] = value.microsecondsSinceEpoch;
184229
_updated();
185230
}
186231

187-
/// The time at which the response was completed, as a microsecond value on
188-
/// the monotonic clock used by the [Timeline]. Note that DevTools will not
232+
/// The time at which the response was completed. Note that DevTools will not
189233
/// consider the request to be complete until [endTime] is non-null.
190-
set endTime(int value) {
191-
_data['endTime'] = value;
234+
set endTime(DateTime value) {
235+
_data['endTime'] = value.microsecondsSinceEpoch;
192236
_updated();
193237
}
194238

195-
/// The error associated with the failed request.
239+
/// The error associated with a failed request.
196240
set error(String value) {
197241
_data['error'] = value;
198242
_updated();
199243
}
200244

201245
HttpProfileResponseData._(
202-
Map<String, dynamic> this._data, void Function() this._updated) {
246+
Map<String, dynamic> this._data,
247+
void Function() this._updated,
248+
) {
203249
_data['redirects'] = <Map<String, dynamic>>[];
204250
}
205251
}
@@ -242,29 +288,25 @@ final class HttpClientRequestProfile {
242288
/// Usage example:
243289
///
244290
/// ```dart
245-
/// profile.addEvent(HttpProfileRequestEvent(Timeline.now, "Connection Established");
246-
/// profile.addEvent(HttpProfileRequestEvent(Timeline.now, "Remote Disconnected");
291+
/// profile.addEvent(HttpProfileRequestEvent(DateTime.now(), "Connection Established");
292+
/// profile.addEvent(HttpProfileRequestEvent(DateTime.now(), "Remote Disconnected");
247293
/// ```
248294
void addEvent(HttpProfileRequestEvent event) {
249295
_data['events'].add(event._toJson());
250296
_updated();
251297
}
252298

253-
/// The time at which the request was initiated, as a microsecond value on the
254-
/// monotonic clock used by the [Timeline].
255-
int? get requestStartTimestamp => _data['requestStartTimestamp'] as int?;
256-
set requestStartTimestamp(int? value) {
257-
_data['requestStartTimestamp'] = value;
299+
/// The time at which the request was initiated.
300+
set requestStartTimestamp(DateTime value) {
301+
_data['requestStartTimestamp'] = value.microsecondsSinceEpoch;
258302
_updated();
259303
}
260304

261-
/// The time at which the request was completed, as a microsecond value on the
262-
/// monotonic clock used by the [Timeline]. Note that DevTools will not
305+
/// The time at which the request was completed. Note that DevTools will not
263306
/// consider the request to be complete until [requestEndTimestamp] is
264307
/// non-null.
265-
int? get requestEndTimestamp => _data['requestEndTimestamp'] as int?;
266-
set requestEndTimestamp(int? value) {
267-
_data['requestEndTimestamp'] = value;
308+
set requestEndTimestamp(DateTime value) {
309+
_data['requestEndTimestamp'] = value.microsecondsSinceEpoch;
268310
_updated();
269311
}
270312

0 commit comments

Comments
 (0)