Skip to content

Commit c7d9006

Browse files
committed
Add getters to classes
1 parent c1d3481 commit c7d9006

7 files changed

+769
-91
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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, UnmodifiableMapView;
67
import 'dart:developer' show Service, addHttpClientProfilingData;
78
import 'dart:io' show HttpClient, HttpClientResponseCompressionState;
89
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,
@@ -46,20 +62,43 @@ final class HttpProfileRequestData {
4662
/// This information is meant to be used for debugging.
4763
///
4864
/// It can contain any arbitrary data as long as the values are of type
49-
/// [String] or [int]. For example:
50-
/// { 'localPort': 1285, 'remotePort': 443, 'connectionPoolId': '21x23' }
51-
set connectionInfo(Map<String, dynamic /*String|int*/ > value) {
65+
/// [String] or [int].
66+
///
67+
/// This field can only be modified by assigning a Map to it. That is:
68+
/// ```dart
69+
/// // Valid
70+
/// profile?.requestData.connectionInfo = {
71+
/// 'localPort': 1285,
72+
/// 'remotePort': 443,
73+
/// 'connectionPoolId': '21x23',
74+
/// };
75+
///
76+
/// // Invalid
77+
/// profile?.requestData.connectionInfo?['localPort'] = 1285;
78+
/// ```
79+
set connectionInfo(Map<String, dynamic /*String|int*/ >? value) {
5280
_checkAndUpdate();
53-
for (final v in value.values) {
54-
if (!(v is String || v is int)) {
55-
throw ArgumentError(
56-
'The values in connectionInfo must be of type String or int.',
57-
);
81+
if (value == null) {
82+
_requestData.remove('connectionInfo');
83+
} else {
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+
}
5890
}
91+
_requestData['connectionInfo'] = {...value};
5992
}
60-
_requestData['connectionInfo'] = {...value};
6193
}
6294

95+
Map<String, dynamic /*String|int*/ >? get connectionInfo =>
96+
_requestData['connectionInfo'] == null
97+
? null
98+
: UnmodifiableMapView(
99+
_requestData['connectionInfo'] as Map<String, dynamic>,
100+
);
101+
63102
/// The content length of the request, in bytes.
64103
set contentLength(int? value) {
65104
_checkAndUpdate();
@@ -70,12 +109,20 @@ final class HttpProfileRequestData {
70109
}
71110
}
72111

112+
int? get contentLength => _requestData['contentLength'] as int?;
113+
73114
/// Whether automatic redirect following was enabled for the request.
74-
set followRedirects(bool value) {
115+
set followRedirects(bool? value) {
75116
_checkAndUpdate();
76-
_requestData['followRedirects'] = value;
117+
if (value == null) {
118+
_requestData.remove('followRedirects');
119+
} else {
120+
_requestData['followRedirects'] = value;
121+
}
77122
}
78123

124+
bool? get followRedirects => _requestData['followRedirects'] as bool?;
125+
79126
/// The request headers where duplicate headers are represented using a list
80127
/// of values.
81128
///
@@ -85,7 +132,7 @@ final class HttpProfileRequestData {
85132
/// // Foo: Bar
86133
/// // Foo: Baz
87134
///
88-
/// profile?.requestData.headersListValues({'Foo', ['Bar', 'Baz']});
135+
/// profile?.requestData.headersListValues({'Foo': ['Bar', 'Baz']});
89136
/// ```
90137
set headersListValues(Map<String, List<String>>? value) {
91138
_checkAndUpdate();
@@ -105,7 +152,7 @@ final class HttpProfileRequestData {
105152
/// // Foo: Bar
106153
/// // Foo: Baz
107154
///
108-
/// profile?.requestData.headersCommaValues({'Foo', 'Bar, Baz']});
155+
/// profile?.requestData.headersCommaValues({'Foo': 'Bar, Baz']});
109156
/// ```
110157
set headersCommaValues(Map<String, String>? value) {
111158
_checkAndUpdate();
@@ -116,24 +163,81 @@ final class HttpProfileRequestData {
116163
_requestData['headers'] = splitHeaderValues(value);
117164
}
118165

166+
/// An unmodifiable map representing the request headers. Duplicate headers
167+
/// are represented using a list of values.
168+
///
169+
/// For example, the map
170+
///
171+
/// ```dart
172+
/// {'Foo': ['Bar', 'Baz']});
173+
/// ```
174+
///
175+
/// represents the headers
176+
///
177+
/// Foo: Bar
178+
/// Foo: Baz
179+
Map<String, List<String>>? get headers => _requestData['headers'] == null
180+
? null
181+
: UnmodifiableMapView(
182+
_requestData['headers'] as Map<String, List<String>>);
183+
119184
/// The maximum number of redirects allowed during the request.
120-
set maxRedirects(int value) {
185+
set maxRedirects(int? value) {
121186
_checkAndUpdate();
122-
_requestData['maxRedirects'] = value;
187+
if (value == null) {
188+
_requestData.remove('maxRedirects');
189+
} else {
190+
_requestData['maxRedirects'] = value;
191+
}
123192
}
124193

194+
int? get maxRedirects => _requestData['maxRedirects'] as int?;
195+
125196
/// The requested persistent connection state.
126-
set persistentConnection(bool value) {
197+
set persistentConnection(bool? value) {
127198
_checkAndUpdate();
128-
_requestData['persistentConnection'] = value;
199+
if (value == null) {
200+
_requestData.remove('persistentConnection');
201+
} else {
202+
_requestData['persistentConnection'] = value;
203+
}
129204
}
130205

206+
bool? get persistentConnection =>
207+
_requestData['persistentConnection'] as bool?;
208+
131209
/// Proxy authentication details for the request.
132-
set proxyDetails(HttpProfileProxyData value) {
210+
set proxyDetails(HttpProfileProxyData? value) {
133211
_checkAndUpdate();
134-
_requestData['proxyDetails'] = value._toJson();
212+
if (value == null) {
213+
_requestData.remove('proxyDetails');
214+
} else {
215+
_requestData['proxyDetails'] = value._toJson();
216+
}
135217
}
136218

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

0 commit comments

Comments
 (0)