diff --git a/lib/src/options.dart b/lib/src/options.dart index 23b6d1f..4d19452 100644 --- a/lib/src/options.dart +++ b/lib/src/options.dart @@ -5,13 +5,13 @@ const int defaultHttpPort = 80; /// Options to be set on the [Pusher] instance. class PusherOptions { /// Defines a value indicating whether call to the API are over HTTP or HTTPS. - bool _encrypted; + bool? _encrypted; int _port = defaultHttpPort; - String _cluster; + String? _cluster; - PusherOptions({bool encrypted: false, int port, String cluster}) { + PusherOptions({bool encrypted: false, int? port, String? cluster}) { this._encrypted = encrypted; if (port != null) this._port = port; @@ -22,19 +22,19 @@ class PusherOptions { } /// Indicates whether calls to the Pusher REST API are over HTTP or HTTPS. - bool get encrypted => _encrypted; + bool? get encrypted => _encrypted; /// port that the HTTP calls will be made to. int get port => _port; - String get cluster => _cluster; + String? get cluster => _cluster; bool get hasCluster => _cluster != null; String get host => !hasCluster ? defaultHost : "api-${_cluster}.pusher.com"; String getBaseUrl() { - String schema = encrypted ? 'https' : 'http'; + String schema = encrypted! ? 'https' : 'http'; String port = _port == 80 ? '' : ":${_port}"; return "$schema://$host$port"; } diff --git a/lib/src/pusher.dart b/lib/src/pusher.dart index 8f475aa..e058739 100644 --- a/lib/src/pusher.dart +++ b/lib/src/pusher.dart @@ -12,15 +12,15 @@ import 'options.dart'; /// Provides access to functionality within the Pusher service such as Trigger to trigger events /// and authenticating subscription requests to private and presence channels. class Pusher { - String _id; + String? _id; - String _key; + String? _key; - String _secret; + String? _secret; - PusherOptions _options; + late PusherOptions _options; - Pusher(String id, String key, String secret, [PusherOptions options]) { + Pusher(String? id, String? key, String? secret, [PusherOptions? options]) { this._id = id; this._secret = secret; this._key = key; @@ -51,7 +51,7 @@ class Pusher { /// String auth = pusher.authenticate('presence-test_channel',socketId,user); /// /// Throws a [JsonUnsupportedObjectError] if [User] cannot be serialized - String authenticate(String channel, String socketId, [User user]) { + String authenticate(String channel, String socketId, [User? user]) { validateChannelName(channel); validateSocketId(socketId); String signature; @@ -59,12 +59,12 @@ class Pusher { if (user == null) { signature = "$socketId:$channel"; - token = "$_key:${hmac256(_secret, signature)}"; + token = "$_key:${hmac256(_secret!, signature)}"; return json.encode({'auth': token}); } else { String data = json.encode(user.toMap()); signature = "$socketId:$channel:$data"; - token = "$_key:${hmac256(_secret, signature)}"; + token = "$_key:${hmac256(_secret!, signature)}"; return json.encode({'auth': token, 'channel_data': data}); } } @@ -84,7 +84,7 @@ class Pusher { /// Retrive a list of users that are on a presence channel: /// Response result = await pusher.get('/channels/presence-channel/users'); Future get(String resource, - [Map parameters]) async { + [Map? parameters]) async { parameters = (parameters != null) ? parameters : new Map(); Request request = _createAuthenticatedRequest('GET', resource, parameters, null); @@ -100,7 +100,7 @@ class Pusher { /// ## Triggering events /// Response response = await pusher.trigger(['test_channel'],'my_event',data); Future trigger(List channels, String event, Map data, - [TriggerOptions options]) { + [TriggerOptions? options]) { options = options == null ? new TriggerOptions() : options; validateListOfChannelNames(channels); validateSocketId(options.socketId); @@ -134,12 +134,12 @@ class Pusher { } Request _createAuthenticatedRequest(String method, String resource, - Map parameters, TriggerBody body) { + Map? parameters, TriggerBody? body) { resource = resource.startsWith('/') ? resource.substring(1) : resource; parameters = parameters == null ? new SplayTreeMap() : new SplayTreeMap.from(parameters); - parameters['auth_key'] = this._key; + parameters['auth_key'] = this._key!; parameters['auth_timestamp'] = _secondsSinceEpoch().toString(); parameters['auth_version'] = '1.0'; @@ -151,7 +151,7 @@ class Pusher { String path = "/apps/${this._id}/$resource"; String toSign = "$method\n$path\n$queryString"; - String authSignature = hmac256(this._secret, toSign); + String authSignature = hmac256(this._secret!, toSign); Uri uri = Uri.parse( "${_options.getBaseUrl()}$path?$queryString&auth_signature=$authSignature"); diff --git a/lib/src/trigger.dart b/lib/src/trigger.dart index 22bfa92..a2ca297 100644 --- a/lib/src/trigger.dart +++ b/lib/src/trigger.dart @@ -5,32 +5,32 @@ import 'validation.dart'; /// Options to be set on the trigger method. class TriggerOptions { /// Socket id to be excluded from receiving event. - String _socketId; + String? _socketId; - TriggerOptions({String socketId}) { + TriggerOptions({String? socketId}) { this._socketId = socketId; } /// Socket id to be excluded from receiving event. - String get socketId => _socketId; + String? get socketId => _socketId; } /// Represents the payload to be sent when triggering events class TriggerBody { /// The name of the event - final String name; + final String? name; /// The event data - final String data; + final String? data; /// The channels the event should be triggered on. - final List channels; + final List? channels; /// The id of a socket to be excluded from receiving the event. - final String socketId; + final String? socketId; TriggerBody({this.name, this.data, this.channels, this.socketId}) { - validateListOfChannelNames(this.channels); + validateListOfChannelNames(this.channels!); validateSocketId(this.socketId); } diff --git a/lib/src/user.dart b/lib/src/user.dart index 9ff5ecf..2e2bedc 100644 --- a/lib/src/user.dart +++ b/lib/src/user.dart @@ -6,7 +6,7 @@ class User { final String id; /// Arbitrary additional information about the user. - final Map info; + final Map? info; User(this.id, [this.info]); diff --git a/lib/src/validation.dart b/lib/src/validation.dart index e238b28..c7acfd2 100644 --- a/lib/src/validation.dart +++ b/lib/src/validation.dart @@ -6,7 +6,7 @@ final RegExp channelNameRegex = new RegExp(r'^[A-Za-z0-9_\-=@,.;]+$'); final int channelNameMaxLength = 164; /// Validate a socket_id value -void validateSocketId(String socketId) { +void validateSocketId(String? socketId) { if (socketId != null && socketIdRegex.hasMatch(socketId) == false) throw new FormatException( "socket_id $socketId was not in the form: ${socketIdRegex.toString()}"); diff --git a/pubspec.yaml b/pubspec.yaml index 543954c..fea8fef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,11 +2,11 @@ name: pusher description: Pusher HTTP Dart Library author: Adao Junior homepage: https://github.com/adaojunior/pusher -version: 1.0.0 +version: 2.0.0 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: - crypto: "^2.0.6" - http: "^0.12.0+1" + crypto: "^3.0.1" + http: "^0.13.2" dev_dependencies: - test: '^1.5.3' + test: '^1.17.3' diff --git a/test/pusher_test.dart b/test/pusher_test.dart index 1a4446f..d4ca6cf 100644 --- a/test/pusher_test.dart +++ b/test/pusher_test.dart @@ -9,9 +9,9 @@ import 'dart:convert' show json , JsonUnsupportedObjectError; import 'dart:io' show Platform; import 'utils.dart' as utils; -final String pusherAppId = Platform.environment['PUSHER_APP_ID']; -final String pusherAppKey = Platform.environment['PUSHER_APP_KEY']; -final String pusherAppSecret = Platform.environment['PUSHER_APP_SECRET']; +final String? pusherAppId = Platform.environment['PUSHER_APP_ID']; +final String? pusherAppKey = Platform.environment['PUSHER_APP_KEY']; +final String? pusherAppSecret = Platform.environment['PUSHER_APP_SECRET']; void main() { group('PusherOptions', () { @@ -44,8 +44,8 @@ void main() { }); group('TriggerOptions', () { - TriggerOptions options; - String socketId; + late TriggerOptions options; + late String socketId; setUp(() { socketId = '444.444'; @@ -58,11 +58,11 @@ void main() { }); group('TriggerBody', () { - TriggerBody body; - String name; - String data; - List channels; - String socketId; + late TriggerBody body; + late String name; + late String data; + late List channels; + late String socketId; setUp(() { name = 'my-event'; @@ -104,9 +104,9 @@ void main() { }); group('Response', () { - Response result; - int status; - String message; + late Response result; + late int status; + late String message; setUp(() { status = 200; @@ -129,7 +129,7 @@ void main() { }); group('Pusher', () { - Pusher pusher; + late Pusher pusher; setUp(() { pusher = new Pusher(pusherAppId, pusherAppKey, pusherAppSecret); @@ -227,7 +227,7 @@ void main() { "boolean":true, "aObjectInstance":instance })), - throwsA(predicate((e) => e is JsonUnsupportedObjectError)) + throwsA(predicate((dynamic e) => e is JsonUnsupportedObjectError)) ); });