-
-
Notifications
You must be signed in to change notification settings - Fork 277
Refacto : add a Transport class #123
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
Changes from 1 commit
a10d740
623c904
0dcb613
c97c98b
477fdac
b8fb322
fd92e3f
0944c68
46829fc
01d4e19
fbd001e
4d39531
34fb528
2437fa4
57798f5
789e154
1aa05d6
9895142
cb5e309
e563d2c
71b53c8
be3f0a2
43b798c
19dc91d
cec1f21
909d4f9
8b600dc
4ff58a6
5493e19
621d6c4
e11f653
88526e2
796b633
fb8b9e0
1dbcba4
8b56bce
daeb29e
098d58c
2833a94
35671d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- move the postUri logic to Dsn
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,15 +42,14 @@ class SentryBrowserClient extends SentryClient { | |
| ); | ||
| } | ||
|
|
||
| SentryBrowserClient._(SentryOptions options, | ||
| {String origin, @required String platform}) | ||
| : super.base( | ||
| SentryBrowserClient._( | ||
| SentryOptions options, { | ||
| String origin, | ||
| @required String platform, | ||
| }) : super.base( | ||
| options, | ||
| transport: Transport( | ||
| dsn: options.dsn, | ||
| httpClient: options.httpClient, | ||
| clock: options.clock, | ||
| compressPayload: false, | ||
| options: options, | ||
| sdk: Sdk(name: browserSdkName, version: sdkVersion), | ||
| platform: platform, | ||
|
||
| origin: origin, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,13 +25,6 @@ class SentryIOClient extends SentryClient { | |
| SentryIOClient._(SentryOptions options, {@required String platform}) | ||
| : super.base( | ||
| options, | ||
| transport: Transport( | ||
| compressPayload: options.compressPayload, | ||
| httpClient: options.httpClient, | ||
| clock: options.clock, | ||
| sdk: sdk, | ||
| dsn: options.dsn, | ||
| platform: platform, | ||
| ), | ||
| transport: Transport(options: options, sdk: sdk, platform: platform), | ||
|
||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:http/http.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:sentry/src/utils.dart'; | ||
|
|
||
|
|
@@ -18,92 +17,45 @@ typedef BodyEncoder = List<int> Function( | |
|
|
||
| /// A transport is in charge of sending the event to the Sentry server. | ||
| class Transport { | ||
| final Client httpClient; | ||
| final SentryOptions _options; | ||
|
|
||
| final Dsn _dsn; | ||
|
|
||
| final Sdk sdk; | ||
| @visibleForTesting | ||
| final Dsn dsn; | ||
|
|
||
| final bool compressPayload; | ||
| /// Use for browser stacktrace | ||
| final String origin; | ||
|
|
||
| /// Used by sentry to differentiate browser from io environment | ||
| final String platform; | ||
|
|
||
| final ClockProvider _clock; | ||
|
|
||
| /// Use for browser stacktrace | ||
| final String origin; | ||
| final Sdk sdk; | ||
|
|
||
| Transport({ | ||
| @required String dsn, | ||
| @required this.compressPayload, | ||
| @required this.httpClient, | ||
| @required SentryOptions options, | ||
| @required this.sdk, | ||
| @required ClockProvider clock, | ||
| @required this.platform, | ||
| this.origin, | ||
| }) : _dsn = Dsn.parse(dsn), | ||
| _clock = clock ?? getUtcDateTime; | ||
|
|
||
| /// The DSN URI. | ||
| @visibleForTesting | ||
| Uri get dsnUri => _dsn.uri; | ||
|
|
||
| /// The Sentry.io public key for the project. | ||
| @visibleForTesting | ||
| // ignore: invalid_use_of_visible_for_testing_member | ||
| String get publicKey => _dsn.publicKey; | ||
|
|
||
| /// The Sentry.io secret key for the project. | ||
| @visibleForTesting | ||
| // ignore: invalid_use_of_visible_for_testing_member | ||
| String get secretKey => _dsn.secretKey; | ||
|
|
||
| /// The ID issued by Sentry.io to your project. | ||
| /// | ||
| /// Attached to the event payload. | ||
| String get projectId => _dsn.projectId; | ||
|
|
||
| String get clientId => sdk.identifier; | ||
|
|
||
| @visibleForTesting | ||
| String get postUri { | ||
| final port = dsnUri.hasPort && | ||
| ((dsnUri.scheme == 'http' && dsnUri.port != 80) || | ||
| (dsnUri.scheme == 'https' && dsnUri.port != 443)) | ||
| ? ':${dsnUri.port}' | ||
| : ''; | ||
|
|
||
| final pathLength = dsnUri.pathSegments.length; | ||
|
|
||
| String apiPath; | ||
| if (pathLength > 1) { | ||
| // some paths would present before the projectID in the dsnUri | ||
| apiPath = | ||
| (dsnUri.pathSegments.sublist(0, pathLength - 1) + ['api']).join('/'); | ||
| } else { | ||
| apiPath = 'api'; | ||
| } | ||
| return '${dsnUri.scheme}://${dsnUri.host}$port/$apiPath/$projectId/store/'; | ||
| } | ||
| }) : _options = options, | ||
| dsn = Dsn.parse(options.dsn); | ||
|
|
||
| Future<SentryId> send(Map<String, dynamic> data) async { | ||
|
||
| final now = _clock(); | ||
| final now = _options.clock(); | ||
|
|
||
| var authHeader = 'Sentry sentry_version=6, sentry_client=$clientId, ' | ||
| 'sentry_timestamp=${now.millisecondsSinceEpoch}, sentry_key=$publicKey'; | ||
| if (secretKey != null) { | ||
| authHeader += ', sentry_secret=$secretKey'; | ||
| } | ||
| var authHeader = dsn.buildAuthHeader( | ||
| timestamp: now.millisecondsSinceEpoch, clientId: sdk.identifier); | ||
|
|
||
| mergeAttributes(_getContext(now), into: data); | ||
|
|
||
| final headers = buildHeaders(authHeader, sdk: sdk); | ||
|
|
||
| final body = bodyEncoder(data, headers, compressPayload: compressPayload); | ||
| final body = bodyEncoder( | ||
| data, | ||
| headers, | ||
| compressPayload: _options.compressPayload, | ||
| ); | ||
|
|
||
| final response = await httpClient.post( | ||
| postUri, | ||
| final response = await _options.httpClient.post( | ||
| dsn.postUri, | ||
| headers: headers, | ||
| body: body, | ||
| ); | ||
|
|
@@ -117,7 +69,7 @@ class Transport { | |
| } | ||
|
|
||
| Map<String, dynamic> _getContext(DateTime now) => { | ||
| 'project': projectId, | ||
| 'project': dsn.projectId, | ||
| 'timestamp': formatDateAsIso8601WithSecondPrecision(now), | ||
| 'platform': platform, | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options.sdkVersionalready exists, but its probably null, we'd need to find a way to instantiate it based on the platform (dart, flutter or web), but ideally, we just read this field from the options hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on a second thought, shouldn't this be just either:
sentry.dartorsentry.flutter? Given that this is what the user installed at the end?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we need only the identifier of the client, yes, only a String would be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ fixed