Skip to content
Open
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
12 changes: 6 additions & 6 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
}
Expand Down
26 changes: 13 additions & 13 deletions lib/src/pusher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,20 +51,20 @@ 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;
String token;

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});
}
}
Expand All @@ -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<Response> get(String resource,
[Map<String, String> parameters]) async {
[Map<String, String>? parameters]) async {
parameters = (parameters != null) ? parameters : new Map<String, String>();
Request request =
_createAuthenticatedRequest('GET', resource, parameters, null);
Expand All @@ -100,7 +100,7 @@ class Pusher {
/// ## Triggering events
/// Response response = await pusher.trigger(['test_channel'],'my_event',data);
Future<Response> trigger(List<String> channels, String event, Map data,
[TriggerOptions options]) {
[TriggerOptions? options]) {
options = options == null ? new TriggerOptions() : options;
validateListOfChannelNames(channels);
validateSocketId(options.socketId);
Expand Down Expand Up @@ -134,12 +134,12 @@ class Pusher {
}

Request _createAuthenticatedRequest(String method, String resource,
Map<String, String> parameters, TriggerBody body) {
Map<String, String>? 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';

Expand All @@ -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");
Expand Down
16 changes: 8 additions & 8 deletions lib/src/trigger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> channels;
final List<String>? 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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class User {
final String id;

/// Arbitrary additional information about the user.
final Map<String, dynamic> info;
final Map<String, dynamic>? info;

User(this.id, [this.info]);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()}");
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: pusher
description: Pusher HTTP Dart Library
author: Adao Junior <itsjunnior@gmail.com>
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'
30 changes: 15 additions & 15 deletions test/pusher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -44,8 +44,8 @@ void main() {
});

group('TriggerOptions', () {
TriggerOptions options;
String socketId;
late TriggerOptions options;
late String socketId;

setUp(() {
socketId = '444.444';
Expand All @@ -58,11 +58,11 @@ void main() {
});

group('TriggerBody', () {
TriggerBody body;
String name;
String data;
List<String> channels;
String socketId;
late TriggerBody body;
late String name;
late String data;
late List<String> channels;
late String socketId;

setUp(() {
name = 'my-event';
Expand Down Expand Up @@ -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;
Expand All @@ -129,7 +129,7 @@ void main() {
});

group('Pusher', () {
Pusher pusher;
late Pusher pusher;

setUp(() {
pusher = new Pusher(pusherAppId, pusherAppKey, pusherAppSecret);
Expand Down Expand Up @@ -227,7 +227,7 @@ void main() {
"boolean":true,
"aObjectInstance":instance
})),
throwsA(predicate((e) => e is JsonUnsupportedObjectError))
throwsA(predicate((dynamic e) => e is JsonUnsupportedObjectError))
);

});
Expand Down