Skip to content

Commit

Permalink
Add configuration for setting custom HTTP headers for requests to the…
Browse files Browse the repository at this point in the history
… collector (close #34)
  • Loading branch information
matus-tomlein committed Jul 12, 2023
1 parent 2b0076f commit 5950764
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NetworkConfigurationReader(values: Map<String, Any>) {
val endpoint: String by values
val method: String? by valuesDefault
val customPostPath: String? by valuesDefault
val requestHeaders: Map<String, String>? by valuesDefault

fun toConfiguration(): NetworkConfiguration {
val networkConfig: NetworkConfiguration = if (method != null) {
Expand All @@ -31,6 +32,7 @@ class NetworkConfigurationReader(values: Map<String, Any>) {
NetworkConfiguration(endpoint)
}
customPostPath?.let { networkConfig.customPostPath(it) }
requestHeaders?.let { networkConfig.requestHeaders(it) }
return networkConfig
}
}
1 change: 1 addition & 0 deletions doc/02-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The only required attributes of the `Snowplow.createTracker` method are `namespa
| `endpoint` | `String` | URI for the Snowplow collector endpoint. |
| `method` | `Method?` | HTTP method to use: `Method.get` or `Method.post` (`Method.post` is default). |
| `customPostPath` | `String?` | Custom POST path. |
| `requestHeaders` | `Map<String, String>?` | Map of custom HTTP headers to add to requests to the collector. |
| `trackerConfig` | `TrackerConfiguration?` | Configuration of the tracker and the core tracker properties. |
| `gdprConfig` | `GdprConfiguration?` | Determines the GDPR context that will be attached to all events sent by the tracker. |
| `subjectConfig` | `SubjectConfiguration?` | Subject information about tracked user and device that is added to events. |
Expand Down
17 changes: 17 additions & 0 deletions example/integration_test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,21 @@ void main() {
return true;
});
});

testWidgets("sets custom request headers", (WidgetTester tester) async {
SnowplowTracker tracker = await Snowplow.createTracker(
namespace: 'custom-headers',
endpoint: SnowplowTests.microEndpoint,
requestHeaders: {'Warning': 'works'});

await tracker
.track(const Structured(category: 'category', action: 'action'));

expect(
await SnowplowTests.checkMicroGood((events) =>
(events.length == 1) &&
(events[0]['rawEvent']['context']['headers']
.contains('Warning: works'))),
isTrue);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct NetworkConfigurationReader: Decodable {
let endpoint: String
let method: String?
let customPostPath: String?
let requestHeaders: [String: String]?
}

extension NetworkConfigurationReader {
Expand All @@ -31,6 +32,9 @@ extension NetworkConfigurationReader {
if let c = customPostPath {
networkConfig.customPostPath(c)
}
if let h = requestHeaders {
networkConfig.requestHeaders(h)
}
return networkConfig
}
}
11 changes: 9 additions & 2 deletions lib/configurations/network_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ class NetworkConfiguration {
/// You will need to configure your collector to accept your custom path.
final String? customPostPath;

/// Custom HTTP headers to add to HTTP requests to the collector.
final Map<String, String>? requestHeaders;

const NetworkConfiguration(
{required this.endpoint, this.method, this.customPostPath});
{required this.endpoint,
this.method,
this.customPostPath,
this.requestHeaders});

Map<String, Object?> toMap() {
final conf = <String, Object?>{
'endpoint': endpoint,
'method': method?.name,
'customPostPath': customPostPath?[0] == '/'
? customPostPath?.substring(1)
: customPostPath
: customPostPath,
'requestHeaders': requestHeaders
};
conf.removeWhere((key, value) => value == null);
return conf;
Expand Down
7 changes: 6 additions & 1 deletion lib/snowplow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@ class Snowplow {
/// [endpoint] refers to the Snowplow collector endpoint.
/// [method] is the HTTP method used to send events to collector and it defaults to POST.
/// [customPostPath] is an optional string for custom POST collector paths.
/// [requestHeaders] is an optional map of custom HTTP headers to add to requests to the collector.
static Future<SnowplowTracker> createTracker(
{required String namespace,
required String endpoint,
Method? method,
String? customPostPath,
Map<String, String>? requestHeaders,
TrackerConfiguration? trackerConfig,
SubjectConfiguration? subjectConfig,
GdprConfiguration? gdprConfig,
EmitterConfiguration? emitterConfig}) async {
final configuration = Configuration(
namespace: namespace,
networkConfig: NetworkConfiguration(
endpoint: endpoint, method: method, customPostPath: customPostPath),
endpoint: endpoint,
method: method,
customPostPath: customPostPath,
requestHeaders: requestHeaders),
trackerConfig: trackerConfig,
subjectConfig: subjectConfig,
gdprConfig: gdprConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class NetworkConfigurationReader extends NetworkConfiguration {
method: map['method'] == null
? null
: Method.values.byName(map['method']),
customPostPath: map['customPostPath']);
customPostPath: map['customPostPath'],
requestHeaders: map['requestHeaders'] == null
? null
: (map['requestHeaders'] as Map<Object?, Object?>).cast());

void addTrackerOptions(dynamic options) {
if (method != null) {
Expand All @@ -27,5 +30,8 @@ class NetworkConfigurationReader extends NetworkConfiguration {
if (customPostPath != null) {
options['postPath'] = '/$customPostPath';
}
if (requestHeaders != null) {
options['customHeaders'] = requestHeaders;
}
}
}
17 changes: 17 additions & 0 deletions test/snowplow_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ void main() {
}));
});

test('createsTrackerWithCustomRequestHeaders', () async {
await Snowplow.createTracker(
namespace: 'tns1',
endpoint: 'https://snowplowanalytics.com',
requestHeaders: {'header1': 'value1', 'header2': 'value2'});

expect(
methodCall,
isMethodCall('createTracker', arguments: {
'namespace': 'tns1',
'networkConfig': {
'endpoint': 'https://snowplowanalytics.com',
'requestHeaders': {'header1': 'value1', 'header2': 'value2'}
}
}));
});

test('createsTrackerWithEmitterConfig', () async {
await Snowplow.createTracker(
namespace: 'tns1',
Expand Down

0 comments on commit 5950764

Please sign in to comment.