Skip to content

Commit

Permalink
Add Highlevel Classes to Push API (#104)
Browse files Browse the repository at this point in the history
* feat: add channels api functions

* feat: add Channel to Push API
-Added Alias highlevel class
-aded delegate highlevel clas

* feat: add notification highlevel api class

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>
  • Loading branch information
gbogboadePush and Gbogboade1 authored Jun 11, 2024
1 parent ad5d63c commit 37c3647
Show file tree
Hide file tree
Showing 38 changed files with 1,663 additions and 34 deletions.
32 changes: 32 additions & 0 deletions examples/demo_app/lib/models/signer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ class EthersSigner extends push.Signer {
return message;
}
}

@override
Future<num> getChainId() {
// TODO: implement getChainId
throw UnimplementedError();
}

@override
Future<String> signTypedData(
{required push.DataDomain domain,
required Map<String, List<push.DataField>> types,
required Map<String, dynamic> values,
String? primaryType}) {
// TODO: implement signTypedData
throw UnimplementedError();
}
}

class Web3Signer extends push.Signer {
Expand Down Expand Up @@ -81,4 +97,20 @@ class Web3Signer extends push.Signer {
credentials.signPersonalMessageToUint8List(Uint8List.fromList(m));
return utf8.decode(sig);
}

@override
Future<num> getChainId() {
// TODO: implement getChainId
throw UnimplementedError();
}

@override
Future<String> signTypedData(
{required push.DataDomain domain,
required Map<String, List<push.DataField>> types,
required Map<String, dynamic> values,
String? primaryType}) {
// TODO: implement signTypedData
throw UnimplementedError();
}
}
32 changes: 32 additions & 0 deletions examples/use_cases/lib/models/signer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ class EthersSigner extends push.Signer {
return message;
}
}

@override
Future<num> getChainId() {
// TODO: implement getChainId
throw UnimplementedError();
}

@override
Future<String> signTypedData(
{required push.DataDomain domain,
required Map<String, List<push.DataField>> types,
required Map<String, dynamic> values,
String? primaryType}) {
// TODO: implement signTypedData
throw UnimplementedError();
}
}

class Web3Signer extends push.Signer {
Expand Down Expand Up @@ -82,4 +98,20 @@ class Web3Signer extends push.Signer {
credentials.signPersonalMessageToUint8List(Uint8List.fromList(m));
return utf8.decode(sig);
}

@override
Future<num> getChainId() {
// TODO: implement getChainId
throw UnimplementedError();
}

@override
Future<String> signTypedData(
{required push.DataDomain domain,
required Map<String, List<push.DataField>> types,
required Map<String, dynamic> values,
String? primaryType}) {
// TODO: implement signTypedData
throw UnimplementedError();
}
}
4 changes: 3 additions & 1 deletion lib/push_restapi_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ export 'src/sockets/sockets.dart';

export 'src/pushapi/pushapi.dart';
export 'src/pushstream/push_stream.dart';

export 'src/channels/channels.dart';
export 'src/push_notification/push_notification.dart';
export 'src/main.dart';
export 'src/alias/alias.dart';
1 change: 1 addition & 0 deletions lib/src/alias/alias.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/get_alias_Info.dart';
28 changes: 28 additions & 0 deletions lib/src/alias/src/get_alias_Info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import '../../../push_restapi_dart.dart';

/// @description - fetches alias information
/// @param {AliasOptions} options - options related to alias
/// @returns Alias details
Future<dynamic> getAliasInfo({required GetAliasInfoOptionsType options}) async {
final alias = ALIAS_CHAIN_ID[options.alias.toString()]?[options.env];

final apiEndpoint = "/v1/alias/$alias/channel";
return await http.get(
path: apiEndpoint,
baseUrl: Api.getAPIBaseUrls(options.env),
);
}

class GetAliasInfoOptionsType {
// Properties
String alias;
ALIAS_CHAIN aliasChain;
ENV env;

// Constructor
GetAliasInfoOptionsType({
required this.alias,
required this.aliasChain,
this.env = ENV.prod,
});
}
10 changes: 10 additions & 0 deletions lib/src/channels/channels.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export 'src/get_channel.dart';
export 'src/get_channels.dart';
export 'src/get_channel_notifications.dart';
export 'src/get_subscribers.dart';
export 'src/search.dart';
export 'src/signature.helpers.dart';
export 'src/subscribe.dart';
export 'src/subscribeV2.dart';
export 'src/unsubscribe.dart';
export 'src/unsubscribeV2.dart';
26 changes: 26 additions & 0 deletions lib/src/channels/src/_get_subscribers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:push_restapi_dart/push_restapi_dart.dart';

Future<List<String>?> getSubscribers(
{required String channel, ENV? env}) async {
final channelAddress = await getCAIPAddress(address: channel);
final channelCAIPDetails = getCAIPDetails(channelAddress);
if (channelCAIPDetails == null) {
throw Exception('Invalid Channel CAIP!');
}

final chainId = channelCAIPDetails.networkId;
final body = {
'channel':
channelCAIPDetails.address, // deprecated API expects ETH address format
'blockchain': chainId,
'op': 'read',
};

final result = await http.post(
path: '/channels/_get_subscribers',
data: body,
baseUrl: Api.getAPIBaseUrls(env),
);

return result['subscribers'];
}
17 changes: 17 additions & 0 deletions lib/src/channels/src/get_channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../../../push_restapi_dart.dart';

Future<dynamic> getChannel({
required String channel,
ENV? env,
}) async {
try {
final channelAddress = await getCAIPAddress(address: channel);
final result = await http.get(
path: '/v1/channels/$channelAddress',
baseUrl: Api.getAPIBaseUrls(env),
);
return result;
} catch (e) {
log('[Push SDK] - API getChannel: $e');
}
}
62 changes: 62 additions & 0 deletions lib/src/channels/src/get_channel_notifications.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// ignore_for_file: constant_identifier_names

import '../../../push_restapi_dart.dart';

class GetChannelNotificationOptions {
String channel;
ENV? env;
int page;
int limit;
NotificationType? filter;
bool? raw;

GetChannelNotificationOptions({
required this.channel,
this.env,
this.page = Pagination.INITIAL_PAGE,
this.limit = Pagination.LIMIT_MAX,
this.filter,
this.raw,
});
}

Future<dynamic> getChannelNotifications(
GetChannelNotificationOptions options) async {
final channel = await getCAIPAddress(address: options.channel);
final query = getQueryParams(
options.filter != null
? {
"page": options.page,
'limit': options.limit,
'notificationType': options.filter?.value,
}
: {
"page": options.page,
'limit': options.limit,
},
);
final requestUrl = '/$channel/notifications?$query';
return http.get(
path: requestUrl,
baseUrl: Api.getAPIBaseUrls(options.env),
);
}

enum NotificationType {
BROADCAST,
TARGETED,
SUBSET,
}

extension NotificationTypeExtension on NotificationType {
int get value {
switch (this) {
case NotificationType.BROADCAST:
return 1;
case NotificationType.TARGETED:
return 3;
case NotificationType.SUBSET:
return 4;
}
}
}
28 changes: 28 additions & 0 deletions lib/src/channels/src/get_channels.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:push_restapi_dart/push_restapi_dart.dart';

class GetChannelOptions {
ENV? env;
int page;
int limit;

String? sort;
String? order;

GetChannelOptions({
this.env,
this.page = Pagination.INITIAL_PAGE,
this.limit = Pagination.LIMIT_MAX,
this.order = ChannelListOrderType.DESCENDING,
this.sort = ChannelListSortType.SUBSCRIBER,
});
}

Future<dynamic> getChannels(GetChannelOptions options) async {
final requestUrl = '/v1/channels?page=${options.page}'
'&limit=${options.limit}&sort=${options.sort}&order=${options.order}';

return http.get(
path: requestUrl,
baseUrl: Api.getAPIBaseUrls(options.env),
);
}
13 changes: 13 additions & 0 deletions lib/src/channels/src/get_delegates.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:push_restapi_dart/push_restapi_dart.dart';

Future<dynamic> getDelegates({
required String channel,
ENV? env,
}) async {
final channelAddress = await getCAIPAddress(address: channel);
final requestUrl = '/v1/channels/$channelAddress/delegates';
return http.get(
path: requestUrl,
baseUrl: Api.getAPIBaseUrls(env),
);
}
56 changes: 56 additions & 0 deletions lib/src/channels/src/get_subscribers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import '../../../push_restapi_dart.dart';

class GetChannelSubscribersOptionsType {
String channel; // plain ETH Format only
int page;
int limit;
int? category;
bool? setting;
ENV? env;
bool? raw;

GetChannelSubscribersOptionsType({
required this.channel,
this.page = 1,
this.limit = 10,
this.category,
this.setting,
this.env,
this.raw,
});
}

Future<dynamic> getSubscribers(GetChannelSubscribersOptionsType options) async {
try {
if (options.channel.isEmpty) {
throw Exception('channel cannot be null or empty');
}

if (options.page <= 0) {
throw Exception('page must be greater than 0');
}

if (options.limit <= 0) {
throw Exception('limit must be greater than 0');
}
if (options.limit > 30) {
throw Exception('limit must be lesser than or equal to 30');
}

final channelAddress = await getCAIPAddress(address: options.channel);
var apiEndpoint =
'/v1/channels/$channelAddress/subscribers?page=${options.page}'
'&limit=${options.limit}&setting=${options.setting}';
if (options.category != null) {
apiEndpoint = 'apiEndpoint&category=${options.category}';
}

return http.get(
path: apiEndpoint,
baseUrl: Api.getAPIBaseUrls(options.env),
);
} catch (e) {
log('[Push SDK] - API - Error - API send() -: $e');
rethrow;
}
}
19 changes: 19 additions & 0 deletions lib/src/channels/src/search.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import '../../../push_restapi_dart.dart';

Future<dynamic> search({
required String query,
int page = 1,
int limit = 10,
ENV? env,
}) async {
final queryObj = {
'page': page,
'limit': limit,
'query': query,
};
var apiEndpoint = '/v1/channels/search/?${getQueryParams(queryObj)}';
return http.get(
path: apiEndpoint,
baseUrl: Api.getAPIBaseUrls(env),
);
}
Loading

0 comments on commit 37c3647

Please sign in to comment.