Skip to content

Commit adaa5e4

Browse files
committed
Add getters to classes
1 parent 8da6e0e commit adaa5e4

7 files changed

+772
-92
lines changed

pkgs/http_profile/lib/src/http_client_request_profile.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ final class HttpProfileRequestEvent {
99
final int _timestamp;
1010
final String _name;
1111

12+
DateTime get timestamp => DateTime.fromMicrosecondsSinceEpoch(_timestamp);
13+
14+
String get name => _name;
15+
1216
HttpProfileRequestEvent({required DateTime timestamp, required String name})
1317
: _timestamp = timestamp.microsecondsSinceEpoch,
1418
_name = name;
1519

20+
static HttpProfileRequestEvent _fromJson(Map<String, dynamic> json) =>
21+
HttpProfileRequestEvent(
22+
timestamp:
23+
DateTime.fromMicrosecondsSinceEpoch(json['timestamp'] as int),
24+
name: json['event'] as String,
25+
);
26+
1627
Map<String, dynamic> _toJson() => <String, dynamic>{
1728
'timestamp': _timestamp,
1829
'event': _name,
@@ -31,6 +42,12 @@ final class HttpClientRequestProfile {
3142

3243
final _data = <String, dynamic>{};
3344

45+
/// The HTTP request method associated with the request.
46+
String get requestMethod => _data['requestMethod'] as String;
47+
48+
/// The URI to which the request was sent.
49+
String get requestUri => _data['requestUri'] as String;
50+
3451
/// Records an event related to the request.
3552
///
3653
/// Usage example:
@@ -54,6 +71,12 @@ final class HttpClientRequestProfile {
5471
_updated();
5572
}
5673

74+
/// An unmodifiable list containing the events related to the request.
75+
List<HttpProfileRequestEvent> get events =>
76+
UnmodifiableListView((_data['events'] as List<Map<String, dynamic>>).map(
77+
HttpProfileRequestEvent._fromJson,
78+
));
79+
5780
/// Details about the request.
5881
late final HttpProfileRequestData requestData;
5982

pkgs/http_profile/lib/src/http_profile.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:collection' show UnmodifiableListView;
6+
import 'dart:collection' show UnmodifiableListView, UnmodifiableMapView;
77
import 'dart:developer' show Service, addHttpClientProfilingData;
88
import 'dart:io' show HttpClient, HttpClientResponseCompressionState;
99
import 'dart:isolate' show Isolate;

pkgs/http_profile/lib/src/http_profile_request_data.dart

Lines changed: 123 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ final class HttpProfileProxyData {
1010
final bool? _isDirect;
1111
final int? _port;
1212

13+
String? get host => _host;
14+
15+
String? get username => _username;
16+
17+
bool? get isDirect => _isDirect;
18+
19+
int? get port => _port;
20+
1321
HttpProfileProxyData({
1422
String? host,
1523
String? username,
@@ -20,6 +28,14 @@ final class HttpProfileProxyData {
2028
_isDirect = isDirect,
2129
_port = port;
2230

31+
static HttpProfileProxyData _fromJson(Map<String, dynamic> json) =>
32+
HttpProfileProxyData(
33+
host: json['host'] as String?,
34+
username: json['username'] as String?,
35+
isDirect: json['isDirect'] as bool?,
36+
port: json['port'] as int?,
37+
);
38+
2339
Map<String, dynamic> _toJson() => <String, dynamic>{
2440
if (_host != null) 'host': _host,
2541
if (_username != null) 'username': _username,
@@ -50,20 +66,43 @@ final class HttpProfileRequestData {
5066
/// This information is meant to be used for debugging.
5167
///
5268
/// It can contain any arbitrary data as long as the values are of type
53-
/// [String] or [int]. For example:
54-
/// { 'localPort': 1285, 'remotePort': 443, 'connectionPoolId': '21x23' }
55-
set connectionInfo(Map<String, dynamic /*String|int*/ > value) {
69+
/// [String] or [int].
70+
///
71+
/// This field can only be modified by assigning a Map to it. That is:
72+
/// ```dart
73+
/// // Valid
74+
/// profile?.requestData.connectionInfo = {
75+
/// 'localPort': 1285,
76+
/// 'remotePort': 443,
77+
/// 'connectionPoolId': '21x23',
78+
/// };
79+
///
80+
/// // Invalid
81+
/// profile?.requestData.connectionInfo?['localPort'] = 1285;
82+
/// ```
83+
set connectionInfo(Map<String, dynamic /*String|int*/ >? value) {
5684
_checkAndUpdate();
57-
for (final v in value.values) {
58-
if (!(v is String || v is int)) {
59-
throw ArgumentError(
60-
'The values in connectionInfo must be of type String or int.',
61-
);
85+
if (value == null) {
86+
_requestData.remove('connectionInfo');
87+
} else {
88+
for (final v in value.values) {
89+
if (!(v is String || v is int)) {
90+
throw ArgumentError(
91+
'The values in connectionInfo must be of type String or int.',
92+
);
93+
}
6294
}
95+
_requestData['connectionInfo'] = {...value};
6396
}
64-
_requestData['connectionInfo'] = {...value};
6597
}
6698

99+
Map<String, dynamic /*String|int*/ >? get connectionInfo =>
100+
_requestData['connectionInfo'] == null
101+
? null
102+
: UnmodifiableMapView(
103+
_requestData['connectionInfo'] as Map<String, dynamic>,
104+
);
105+
67106
/// The content length of the request, in bytes.
68107
set contentLength(int? value) {
69108
_checkAndUpdate();
@@ -74,12 +113,20 @@ final class HttpProfileRequestData {
74113
}
75114
}
76115

116+
int? get contentLength => _requestData['contentLength'] as int?;
117+
77118
/// Whether automatic redirect following was enabled for the request.
78-
set followRedirects(bool value) {
119+
set followRedirects(bool? value) {
79120
_checkAndUpdate();
80-
_requestData['followRedirects'] = value;
121+
if (value == null) {
122+
_requestData.remove('followRedirects');
123+
} else {
124+
_requestData['followRedirects'] = value;
125+
}
81126
}
82127

128+
bool? get followRedirects => _requestData['followRedirects'] as bool?;
129+
83130
/// The request headers where duplicate headers are represented using a list
84131
/// of values.
85132
///
@@ -89,7 +136,7 @@ final class HttpProfileRequestData {
89136
/// // Foo: Bar
90137
/// // Foo: Baz
91138
///
92-
/// profile?.requestData.headersListValues({'Foo', ['Bar', 'Baz']});
139+
/// profile?.requestData.headersListValues({'Foo': ['Bar', 'Baz']});
93140
/// ```
94141
set headersListValues(Map<String, List<String>>? value) {
95142
_checkAndUpdate();
@@ -109,7 +156,7 @@ final class HttpProfileRequestData {
109156
/// // Foo: Bar
110157
/// // Foo: Baz
111158
///
112-
/// profile?.requestData.headersCommaValues({'Foo', 'Bar, Baz']});
159+
/// profile?.requestData.headersCommaValues({'Foo': 'Bar, Baz']});
113160
/// ```
114161
set headersCommaValues(Map<String, String>? value) {
115162
_checkAndUpdate();
@@ -120,24 +167,81 @@ final class HttpProfileRequestData {
120167
_requestData['headers'] = splitHeaderValues(value);
121168
}
122169

170+
/// An unmodifiable map representing the request headers. Duplicate headers
171+
/// are represented using a list of values.
172+
///
173+
/// For example, the map
174+
///
175+
/// ```dart
176+
/// {'Foo': ['Bar', 'Baz']});
177+
/// ```
178+
///
179+
/// represents the headers
180+
///
181+
/// Foo: Bar
182+
/// Foo: Baz
183+
Map<String, List<String>>? get headers => _requestData['headers'] == null
184+
? null
185+
: UnmodifiableMapView(
186+
_requestData['headers'] as Map<String, List<String>>);
187+
123188
/// The maximum number of redirects allowed during the request.
124-
set maxRedirects(int value) {
189+
set maxRedirects(int? value) {
125190
_checkAndUpdate();
126-
_requestData['maxRedirects'] = value;
191+
if (value == null) {
192+
_requestData.remove('maxRedirects');
193+
} else {
194+
_requestData['maxRedirects'] = value;
195+
}
127196
}
128197

198+
int? get maxRedirects => _requestData['maxRedirects'] as int?;
199+
129200
/// The requested persistent connection state.
130-
set persistentConnection(bool value) {
201+
set persistentConnection(bool? value) {
131202
_checkAndUpdate();
132-
_requestData['persistentConnection'] = value;
203+
if (value == null) {
204+
_requestData.remove('persistentConnection');
205+
} else {
206+
_requestData['persistentConnection'] = value;
207+
}
133208
}
134209

210+
bool? get persistentConnection =>
211+
_requestData['persistentConnection'] as bool?;
212+
135213
/// Proxy authentication details for the request.
136-
set proxyDetails(HttpProfileProxyData value) {
214+
set proxyDetails(HttpProfileProxyData? value) {
137215
_checkAndUpdate();
138-
_requestData['proxyDetails'] = value._toJson();
216+
if (value == null) {
217+
_requestData.remove('proxyDetails');
218+
} else {
219+
_requestData['proxyDetails'] = value._toJson();
220+
}
139221
}
140222

223+
HttpProfileProxyData? get proxyDetails => _requestData['proxyDetails'] == null
224+
? null
225+
: HttpProfileProxyData._fromJson(
226+
_requestData['proxyDetails'] as Map<String, dynamic>,
227+
);
228+
229+
/// The time at which the request was initiated.
230+
DateTime get startTime => DateTime.fromMicrosecondsSinceEpoch(
231+
_data['requestStartTimestamp'] as int,
232+
);
233+
234+
/// The time when the request was fully sent.
235+
DateTime? get endTime => _data['requestEndTimestamp'] == null
236+
? null
237+
: DateTime.fromMicrosecondsSinceEpoch(
238+
_data['requestEndTimestamp'] as int,
239+
);
240+
241+
/// The error that the request failed with.
242+
String? get error =>
243+
_requestData['error'] == null ? null : _requestData['error'] as String;
244+
141245
HttpProfileRequestData._(
142246
this._data,
143247
this._updated,

0 commit comments

Comments
 (0)