Skip to content

[package:http_profile] Add getters to classes #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkgs/http_profile/lib/src/http_client_request_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ final class HttpProfileRequestEvent {
final int _timestamp;
final String _name;

DateTime get timestamp => DateTime.fromMicrosecondsSinceEpoch(_timestamp);

String get name => _name;

HttpProfileRequestEvent({required DateTime timestamp, required String name})
: _timestamp = timestamp.microsecondsSinceEpoch,
_name = name;

static HttpProfileRequestEvent _fromJson(Map<String, dynamic> json) =>
HttpProfileRequestEvent(
timestamp:
DateTime.fromMicrosecondsSinceEpoch(json['timestamp'] as int),
name: json['event'] as String,
);

Map<String, dynamic> _toJson() => <String, dynamic>{
'timestamp': _timestamp,
'event': _name,
Expand All @@ -31,6 +42,12 @@ final class HttpClientRequestProfile {

final _data = <String, dynamic>{};

/// The HTTP request method associated with the request.
String get requestMethod => _data['requestMethod'] as String;

/// The URI to which the request was sent.
String get requestUri => _data['requestUri'] as String;

/// Records an event related to the request.
///
/// Usage example:
Expand All @@ -54,6 +71,12 @@ final class HttpClientRequestProfile {
_updated();
}

/// An unmodifiable list containing the events related to the request.
List<HttpProfileRequestEvent> get events =>
UnmodifiableListView((_data['events'] as List<Map<String, dynamic>>).map(
HttpProfileRequestEvent._fromJson,
));

/// Details about the request.
late final HttpProfileRequestData requestData;

Expand Down
2 changes: 1 addition & 1 deletion pkgs/http_profile/lib/src/http_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:collection' show UnmodifiableListView;
import 'dart:collection' show UnmodifiableListView, UnmodifiableMapView;
import 'dart:developer' show Service, addHttpClientProfilingData;
import 'dart:io' show HttpClient, HttpClientResponseCompressionState;
import 'dart:isolate' show Isolate;
Expand Down
142 changes: 123 additions & 19 deletions pkgs/http_profile/lib/src/http_profile_request_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ final class HttpProfileProxyData {
final bool? _isDirect;
final int? _port;

String? get host => _host;

String? get username => _username;

bool? get isDirect => _isDirect;

int? get port => _port;

HttpProfileProxyData({
String? host,
String? username,
Expand All @@ -20,6 +28,14 @@ final class HttpProfileProxyData {
_isDirect = isDirect,
_port = port;

static HttpProfileProxyData _fromJson(Map<String, dynamic> json) =>
HttpProfileProxyData(
host: json['host'] as String?,
username: json['username'] as String?,
isDirect: json['isDirect'] as bool?,
port: json['port'] as int?,
);

Map<String, dynamic> _toJson() => <String, dynamic>{
if (_host != null) 'host': _host,
if (_username != null) 'username': _username,
Expand Down Expand Up @@ -50,20 +66,43 @@ final class HttpProfileRequestData {
/// This information is meant to be used for debugging.
///
/// It can contain any arbitrary data as long as the values are of type
/// [String] or [int]. For example:
/// { 'localPort': 1285, 'remotePort': 443, 'connectionPoolId': '21x23' }
set connectionInfo(Map<String, dynamic /*String|int*/ > value) {
/// [String] or [int].
///
/// This field can only be modified by assigning a Map to it. That is:
/// ```dart
/// // Valid
/// profile?.requestData.connectionInfo = {
/// 'localPort': 1285,
/// 'remotePort': 443,
/// 'connectionPoolId': '21x23',
/// };
///
/// // Invalid
/// profile?.requestData.connectionInfo?['localPort'] = 1285;
/// ```
set connectionInfo(Map<String, dynamic /*String|int*/ >? value) {
_checkAndUpdate();
for (final v in value.values) {
if (!(v is String || v is int)) {
throw ArgumentError(
'The values in connectionInfo must be of type String or int.',
);
if (value == null) {
_requestData.remove('connectionInfo');
} else {
for (final v in value.values) {
if (!(v is String || v is int)) {
throw ArgumentError(
'The values in connectionInfo must be of type String or int.',
);
}
}
_requestData['connectionInfo'] = {...value};
}
_requestData['connectionInfo'] = {...value};
}

Map<String, dynamic /*String|int*/ >? get connectionInfo =>
_requestData['connectionInfo'] == null
? null
: UnmodifiableMapView(
_requestData['connectionInfo'] as Map<String, dynamic>,
);

/// The content length of the request, in bytes.
set contentLength(int? value) {
_checkAndUpdate();
Expand All @@ -74,12 +113,20 @@ final class HttpProfileRequestData {
}
}

int? get contentLength => _requestData['contentLength'] as int?;

/// Whether automatic redirect following was enabled for the request.
set followRedirects(bool value) {
set followRedirects(bool? value) {
_checkAndUpdate();
_requestData['followRedirects'] = value;
if (value == null) {
_requestData.remove('followRedirects');
} else {
_requestData['followRedirects'] = value;
}
}

bool? get followRedirects => _requestData['followRedirects'] as bool?;

/// The request headers where duplicate headers are represented using a list
/// of values.
///
Expand All @@ -89,7 +136,7 @@ final class HttpProfileRequestData {
/// // Foo: Bar
/// // Foo: Baz
///
/// profile?.requestData.headersListValues({'Foo', ['Bar', 'Baz']});
/// profile?.requestData.headersListValues({'Foo': ['Bar', 'Baz']});
/// ```
set headersListValues(Map<String, List<String>>? value) {
_checkAndUpdate();
Expand All @@ -109,7 +156,7 @@ final class HttpProfileRequestData {
/// // Foo: Bar
/// // Foo: Baz
///
/// profile?.requestData.headersCommaValues({'Foo', 'Bar, Baz']});
/// profile?.requestData.headersCommaValues({'Foo': 'Bar, Baz']});
/// ```
set headersCommaValues(Map<String, String>? value) {
_checkAndUpdate();
Expand All @@ -120,24 +167,81 @@ final class HttpProfileRequestData {
_requestData['headers'] = splitHeaderValues(value);
}

/// An unmodifiable map representing the request headers. Duplicate headers
/// are represented using a list of values.
///
/// For example, the map
///
/// ```dart
/// {'Foo': ['Bar', 'Baz']});
/// ```
///
/// represents the headers
///
/// Foo: Bar
/// Foo: Baz
Map<String, List<String>>? get headers => _requestData['headers'] == null
? null
: UnmodifiableMapView(
_requestData['headers'] as Map<String, List<String>>);

/// The maximum number of redirects allowed during the request.
set maxRedirects(int value) {
set maxRedirects(int? value) {
_checkAndUpdate();
_requestData['maxRedirects'] = value;
if (value == null) {
_requestData.remove('maxRedirects');
} else {
_requestData['maxRedirects'] = value;
}
}

int? get maxRedirects => _requestData['maxRedirects'] as int?;

/// The requested persistent connection state.
set persistentConnection(bool value) {
set persistentConnection(bool? value) {
_checkAndUpdate();
_requestData['persistentConnection'] = value;
if (value == null) {
_requestData.remove('persistentConnection');
} else {
_requestData['persistentConnection'] = value;
}
}

bool? get persistentConnection =>
_requestData['persistentConnection'] as bool?;

/// Proxy authentication details for the request.
set proxyDetails(HttpProfileProxyData value) {
set proxyDetails(HttpProfileProxyData? value) {
_checkAndUpdate();
_requestData['proxyDetails'] = value._toJson();
if (value == null) {
_requestData.remove('proxyDetails');
} else {
_requestData['proxyDetails'] = value._toJson();
}
}

HttpProfileProxyData? get proxyDetails => _requestData['proxyDetails'] == null
? null
: HttpProfileProxyData._fromJson(
_requestData['proxyDetails'] as Map<String, dynamic>,
);

/// The time at which the request was initiated.
DateTime get startTime => DateTime.fromMicrosecondsSinceEpoch(
_data['requestStartTimestamp'] as int,
);

/// The time when the request was fully sent.
DateTime? get endTime => _data['requestEndTimestamp'] == null
? null
: DateTime.fromMicrosecondsSinceEpoch(
_data['requestEndTimestamp'] as int,
);

/// The error that the request failed with.
String? get error =>
_requestData['error'] == null ? null : _requestData['error'] as String;

HttpProfileRequestData._(
this._data,
this._updated,
Expand Down
Loading