Skip to content

Commit

Permalink
* Add login exception on authentication failed
Browse files Browse the repository at this point in the history
* Add example
  • Loading branch information
Sebastien Dugene committed Dec 29, 2020
1 parent 5026b8b commit cca1d20
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 0.0.9
## 0.0.11

* Add login exception on authentication failed
* Add example

## 0.0.10

* Fix http dependency again

Expand Down
40 changes: 40 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:rocket_chat_connector_flutter/models/channel.dart';
import 'package:rocket_chat_connector_flutter/models/channel_counters.dart';
import 'package:rocket_chat_connector_flutter/models/channel_messages.dart';
import 'package:rocket_chat_connector_flutter/models/filters/channel_counters_filter.dart';
import 'package:rocket_chat_connector_flutter/models/filters/channel_history_filter.dart';
import 'package:rocket_chat_connector_flutter/models/new/message_new.dart';
import 'package:rocket_chat_connector_flutter/models/response/message_new_response.dart';
import 'package:rocket_chat_connector_flutter/services/authentication_service.dart';
import 'package:rocket_chat_connector_flutter/services/channel_service.dart';
import 'package:rocket_chat_connector_flutter/services/http_service.dart' as rocket_http_service;
import 'package:rocket_chat_connector_flutter/services/message_service.dart';

final String serverUrl = "myServerUrl";
final String username = "myUserName";
final String password = "myPassword";
final Channel channel = Channel(id: "myChannelId");

final rocket_http_service.HttpService rocketHttpService = rocket_http_service.HttpService(serverUrl);
final AuthenticationService authenticationService = AuthenticationService(rocketHttpService);
final ChannelService channelService = ChannelService(rocketHttpService);
final MessageService messageService = MessageService(rocketHttpService);

Future main(List<String> args) async {
rocket_http_service.HttpService.authentication = await authenticationService.login(username, password);

// get channel message counter
ChannelCountersFilter filter = ChannelCountersFilter(channel);
ChannelCounters counters = await channelService.counters(filter);
print("Channel have ${counters.unreads} unread messages");

// get channel message list
ChannelHistoryFilter channelHistoryFilter = ChannelHistoryFilter(channel, count: 50);
ChannelMessages channelMessages = await channelService.history(channelHistoryFilter);
print("Last message : ${channelMessages.messages.first.ts} : ${channelMessages.messages.first.msg}");

// send message
MessageNew messageNew = MessageNew(roomId: channel.id, text: "my message");
MessageNewResponse response = await messageService.postMessage(messageNew);
print("Message send success : ${response.success}");
}
9 changes: 9 additions & 0 deletions lib/exceptions/login_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class LoginException implements Exception {
String message;
LoginException(this.message);

String toString() {
if (message == null) return "LoginException";
return "LoginException: $message";
}
}
3 changes: 2 additions & 1 deletion lib/services/authentication_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:rocket_chat_connector_flutter/exceptions/login_exception.dart';
import 'package:rocket_chat_connector_flutter/models/authentication.dart';
import 'package:rocket_chat_connector_flutter/models/user.dart';
import 'package:rocket_chat_connector_flutter/services/http_service.dart';
Expand All @@ -18,7 +19,7 @@ class AuthenticationService {
if (response?.statusCode == 200 && response.body?.isNotEmpty == true) {
return Authentication.fromMap(jsonDecode(response.body));
}
return null;
throw LoginException("Rocket chat authentication failed");
}

Future<User> me() async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: rocket_chat_connector_flutter
description: Flutter Rocket.chat connector. This library is developed to allow the use of a Rocket.chat server as a messaging system.
homepage: https://github.com/sdugene/rocket-chat-connector-flutter
version: 0.0.10
version: 0.0.11

environment:
sdk: ">=2.1.0 <3.0.0"
Expand Down

0 comments on commit cca1d20

Please sign in to comment.