Add target builder and more options

This commit is contained in:
Sambo Chea 2023-05-27 09:47:24 +07:00
parent f41fabab3f
commit 71ed299a68
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
4 changed files with 100 additions and 20 deletions

View File

@ -6,7 +6,7 @@ void main() async {
final APP_ID = "72bd14c306a91fa8a590330e3898ddcc"; final APP_ID = "72bd14c306a91fa8a590330e3898ddcc";
final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ"; final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ";
final sdk = VLogs.create(APP_ID, API_KEY); final sdk = VLogs.createWith(APP_ID, API_KEY);
var request = Collector.builder() var request = Collector.builder()
.message("Hello World") .message("Hello World")

View File

@ -15,14 +15,10 @@ class VLogs {
static final String API_KEY_HEADER_PREFIX = "x-api-key"; static final String API_KEY_HEADER_PREFIX = "x-api-key";
static final int DEFAULT_CONNECT_TIMEOUT = 60; // seconds static final int DEFAULT_CONNECT_TIMEOUT = 60; // seconds
late String _baseUrl; late VLogsOptions _options;
late String _appId;
late String _apiKey;
late VLogsService _service; late VLogsService _service;
VLogs(VLogsOptions options) { VLogs(VLogsOptions options) {
_baseUrl = options.url ?? DEFAULT_VLOGS_URL;
if (options.appId == null || options.apiKey == null) { if (options.appId == null || options.apiKey == null) {
throw Exception("AppId and ApiKey are required"); throw Exception("AppId and ApiKey are required");
} }
@ -30,21 +26,24 @@ class VLogs {
if (options.apiKey!.isEmpty || options.appId!.isEmpty) { if (options.apiKey!.isEmpty || options.appId!.isEmpty) {
throw Exception("AppId and ApiKey are required"); throw Exception("AppId and ApiKey are required");
} }
_appId = options.appId!;
_apiKey = options.apiKey!;
_service = VLogsService(_baseUrl); // Set default options
_options = options;
_options.url ??= DEFAULT_VLOGS_URL;
// Initialize service
_service = VLogsService(_options.url!);
_logger.i( _logger.i(
"VLogs: Initialized AppID: $_appId | SDK Version: $VERSION-$VERSION_CODE"); "VLogs: Initialized AppID: ${_options.appId} | SDK Version: $VERSION-$VERSION_CODE");
} }
Future<CollectorResponse> collect(Collector request) async { Future<CollectorResponse> collect(Collector request) async {
_logger.d("VLogs: Collecting logs for ${request.getId()}"); _logger.d("VLogs: Collecting logs for ${request.getId()}");
var headers = { var headers = {
APP_ID_HEADER_PREFIX: _appId, APP_ID_HEADER_PREFIX: _options.appId!,
API_KEY_HEADER_PREFIX: _apiKey, API_KEY_HEADER_PREFIX: _options.apiKey!,
"Content-Type": "application/json", "Content-Type": "application/json",
}; };
@ -59,10 +58,21 @@ class VLogs {
.build(); .build();
if (request.target == null) { if (request.target == null) {
request.target = Target.builder().sdkInfo(sdkInfo).build(); if (_options.target != null) {
request.target = _options.target;
} else {
request.target = Target.builder().build();
}
} else { } else {
request.target!.sdkInfo = sdkInfo; if (_options.target != null) {
request.target!.merge(_options.target!);
}
} }
// Set SDK info to request
request.target!.sdkInfo = sdkInfo;
// Append user agent to request
request.userAgent ??= "vlogs-dart-sdk/$VERSION-$VERSION_CODE ($hostname)"; request.userAgent ??= "vlogs-dart-sdk/$VERSION-$VERSION_CODE ($hostname)";
var response = await _service.post(request.toJson(), headers: headers); var response = await _service.post(request.toJson(), headers: headers);
@ -74,15 +84,16 @@ class VLogs {
.then((value) => {_logger.d("VLogs: Collected logs response: $value")}) .then((value) => {_logger.d("VLogs: Collected logs response: $value")})
.catchError((error) { .catchError((error) {
_logger.e("VLogs: Error while collecting logs: $error"); _logger.e("VLogs: Error while collecting logs: $error");
return error;
}); });
} }
static VLogs createWithOptions(VLogsOptions options) { static VLogs create(VLogsOptions options) {
return VLogs(options); return VLogs(options);
} }
static VLogs create(String appId, String apiKey) { static VLogs createWith(String appId, String apiKey) {
return createWithOptions(VLogsOptions( return create(VLogsOptions(
appId: appId, appId: appId,
apiKey: apiKey, apiKey: apiKey,
)); ));

View File

@ -254,6 +254,43 @@ class Target {
}; };
} }
// Create a merge
void merge(Target? defaultTarget) {
if (defaultTarget == null) return;
telegram ??= defaultTarget.telegram;
discord ??= defaultTarget.discord;
}
static Target withTelegram(chatId,
{String? token,
TelegramParseMode? parseMode,
bool? disabled,
dynamic extras}) =>
Target(
telegram: Telegram(
chatId: chatId,
token: token,
parseMode: parseMode,
disabled: disabled,
extras: extras,
),
);
static Target withDiscord(webhookUrl,
{String? webhookId,
String? webhookToken,
bool? disabled,
dynamic extras}) =>
Target(
discord: Discord(
webhookUrl: webhookUrl,
webhookId: webhookId,
webhookToken: webhookToken,
disabled: disabled,
extras: extras,
),
);
static TargetBuilder builder() { static TargetBuilder builder() {
return TargetBuilder(); return TargetBuilder();
} }
@ -453,6 +490,7 @@ class VLogsOptions {
String? apiKey; String? apiKey;
int? connectionTimeout; int? connectionTimeout;
bool? testConnection; bool? testConnection;
Target? target;
VLogsOptions({ VLogsOptions({
this.url, this.url,
@ -460,6 +498,7 @@ class VLogsOptions {
this.apiKey, this.apiKey,
this.connectionTimeout, this.connectionTimeout,
this.testConnection, this.testConnection,
this.target,
}); });
VLogsOptions._builder(VLogsOptionsBuilder builder) VLogsOptions._builder(VLogsOptionsBuilder builder)
@ -467,7 +506,12 @@ class VLogsOptions {
appId = builder._appId, appId = builder._appId,
apiKey = builder._apiKey, apiKey = builder._apiKey,
connectionTimeout = builder._connectionTimeout, connectionTimeout = builder._connectionTimeout,
testConnection = builder._testConnection; testConnection = builder._testConnection,
target = builder._target;
static VLogsOptionsBuilder builder() {
return VLogsOptionsBuilder();
}
} }
class VLogsOptionsBuilder { class VLogsOptionsBuilder {
@ -476,6 +520,7 @@ class VLogsOptionsBuilder {
String? _apiKey; String? _apiKey;
int? _connectionTimeout; int? _connectionTimeout;
bool? _testConnection; bool? _testConnection;
Target? _target;
VLogsOptionsBuilder(); VLogsOptionsBuilder();
@ -504,6 +549,29 @@ class VLogsOptionsBuilder {
return this; return this;
} }
VLogsOptionsBuilder target(Target? target) {
_target = target;
return this;
}
VLogsOptionsBuilder telegram(Telegram? telegram) {
if (_target == null) {
_target = TargetBuilder().telegram(telegram).build();
} else {
_target!.telegram = telegram;
}
return this;
}
VLogsOptionsBuilder discord(Discord? discord) {
if (_target == null) {
_target = TargetBuilder().discord(discord).build();
} else {
_target!.discord = discord;
}
return this;
}
VLogsOptions build() { VLogsOptions build() {
return VLogsOptions._builder(this); return VLogsOptions._builder(this);
} }

View File

@ -8,7 +8,8 @@ void main() {
final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ"; final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ";
group('Run collecting the logs', () { group('Run collecting the logs', () {
final sdk = VLogs.create(APP_ID, API_KEY); final sdk = VLogs.create(
VLogsOptions.builder().appId(APP_ID).apiKey(API_KEY).build());
setUp(() { setUp(() {
expect(sdk, isNotNull); expect(sdk, isNotNull);
@ -16,7 +17,7 @@ void main() {
test('Emit the logs to collector', () async { test('Emit the logs to collector', () async {
var request = Collector.builder() var request = Collector.builder()
.message("Hello World") .message("Hello from Dart SDK 1.0.0")
.source(CollectorSource.mobile.name) .source(CollectorSource.mobile.name)
.type(CollectorType.log.name) .type(CollectorType.log.name)
.build(); .build();