rocketchat_sdk 0.1.0
rocketchat_sdk: ^0.1.0 copied to clipboard
A highly modular, strictly-typed, and enterprise-grade Dart & Flutter SDK for integrating Rocket.Chat REST APIs.
example/rocketchat_sdk_example.dart
import 'package:rocketchat_sdk/rocketchat_sdk.dart';
void main() async {
// 1. Initialize the Rocket.Chat client with your server's base URL.
// You don't need authentication credentials at this stage.
final client = RocketChatClient(
baseUrl: 'https://chat.example.com',
);
print('š Rocket.Chat SDK Client initialized at: ${client.baseUrl}');
try {
// 2. Perform a secure login request using username and password.
// Auth headers (X-Auth-Token & X-User-Id) will be dynamically managed
// and injected into subsequent network requests automatically upon success!
print('\nš Logging in...');
final loginData = await client.auth.login(
username: 'apiadmin',
password: 'password123',
);
print('ā
Login successful!');
print(' User ID: ${loginData.userId}');
print(' Token: ${loginData.authToken.substring(0, 10)}...');
// 3. Get the authenticated user's own profile details
print('\nš¤ Fetching own profile details...');
final myself = await client.auth.me();
print('ā
Profile loaded: ${myself.name} (@${myself.username})');
// 4. List all public channels in the workspace
print('\nš¬ Fetching public channels...');
final channels = await client.channels.list(count: 10);
print('ā
Channels list loaded:');
for (final room in channels) {
print(' ⢠#${room.name} (ID: ${room.id}, Messages: ${room.msgs})');
}
// 5. List the user's active direct message (DM) rooms
print('\nāļø Fetching direct message rooms...');
final dms = await client.dm.list(count: 10);
print('ā
Direct message rooms loaded:');
for (final room in dms) {
print(' ⢠Chat with ${room.usernames.join(", ")} (ID: ${room.id})');
}
if (dms.isNotEmpty) {
final activeRoom = dms.first;
// 6. Fetch the message history inside the first active DM room
print('\nš Fetching messages inside DM room: ${activeRoom.id}...');
final messages = await client.dm.messages(
roomId: activeRoom.id,
count: 5,
);
print('ā
Last ${messages.length} messages:');
for (final message in messages.reversed) {
print(' [${message.sender.username}]: ${message.msg}');
}
}
// 7. Join and explore a channel
if (channels.isNotEmpty) {
final channelToJoin = channels.first;
print('\nš¤ Joining channel #${channelToJoin.name}...');
final joined = await client.channels.join(roomId: channelToJoin.id);
if (joined) {
print('ā
Successfully joined #${channelToJoin.name}!');
// Fetch channel history
print('š Fetching history of #${channelToJoin.name}...');
final history = await client.channels.history(
roomId: channelToJoin.id,
count: 5,
);
for (final message in history.reversed) {
print(' [${message.sender.username}]: ${message.msg}');
}
// Leave the channel
print('šŖ Leaving channel #${channelToJoin.name}...');
await client.channels.leave(roomId: channelToJoin.id);
print('ā
Left #${channelToJoin.name}.');
}
}
// 8. User Management: List all workspace users (requires appropriate permissions)
print('\nš„ Fetching all workspace users...');
final allUsers = await client.users.list(count: 5);
print('ā
User list loaded:');
for (final user in allUsers) {
print(' ⢠@${user.username} (${user.name ?? "No Name"})');
}
// 9. Logout securely
print('\nš Logging out...');
final loggedOut = await client.auth.logout();
if (loggedOut) {
print('ā
Logout successful! Auth headers cleared.');
}
} catch (e) {
print('ā An error occurred: $e');
}
}