From f41fabab3f61f3d13e5ad2bf96eb5c66e18e43fa Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 27 May 2023 09:04:53 +0700 Subject: [PATCH] Rename CollectorRequest->Collector and add extras to target --- README.md | 2 +- analysis_options.yaml | 13 --------- example/vlogs_example.dart | 8 +++++- lib/src/base.dart | 20 +++++++------ lib/src/model.dart | 59 ++++++++++++++++++++++++++------------ pubspec.yaml | 2 +- test/vlogs_test.dart | 7 ++--- 7 files changed, 64 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 9ae1268..8660ef4 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ void main() async { final sdk = VLogs.create(APP_ID, API_KEY); - var request = CollectorRequest.builder() + var request = Collector.builder() .message("Hello World") .source(CollectorSource.mobile.name) .type(CollectorType.log.name) diff --git a/analysis_options.yaml b/analysis_options.yaml index dee8927..53e05a7 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,16 +1,3 @@ -# This file configures the static analysis results for your project (errors, -# warnings, and lints). -# -# This enables the 'recommended' set of lints from `package:lints`. -# This set helps identify many issues that may lead to problems when running -# or consuming Dart code, and enforces writing Dart using a single, idiomatic -# style and format. -# -# If you want a smaller set of lints you can change this to specify -# 'package:lints/core.yaml'. These are just the most critical lints -# (the recommended set includes the core lints). -# The core lints are also what is used by pub.dev for scoring packages. - include: package:lints/recommended.yaml # Uncomment the following section to specify additional rules. diff --git a/example/vlogs_example.dart b/example/vlogs_example.dart index b38521e..04e3402 100644 --- a/example/vlogs_example.dart +++ b/example/vlogs_example.dart @@ -8,12 +8,18 @@ void main() async { final sdk = VLogs.create(APP_ID, API_KEY); - var request = CollectorRequest.builder() + var request = Collector.builder() .message("Hello World") .source(CollectorSource.mobile.name) .type(CollectorType.log.name) .build(); + // Run this to test non-blocking collect + sdk.collectAsync(request); + + // Run this to test blocking collect var response = await sdk.collect(request); + + // Output the response print("Response: ${response.toJson()}"); } diff --git a/lib/src/base.dart b/lib/src/base.dart index 1cf37e2..5c64561 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -35,10 +35,13 @@ class VLogs { _service = VLogsService(_baseUrl); - _logger.i("VLogs: Initialized AppID: $_appId | SDK Version: $VERSION-$VERSION_CODE"); + _logger.i( + "VLogs: Initialized AppID: $_appId | SDK Version: $VERSION-$VERSION_CODE"); } - Future collect(CollectorRequest request) async { + Future collect(Collector request) async { + _logger.d("VLogs: Collecting logs for ${request.getId()}"); + var headers = { APP_ID_HEADER_PREFIX: _appId, API_KEY_HEADER_PREFIX: _apiKey, @@ -66,13 +69,12 @@ class VLogs { return response; } - void collectAsync(CollectorRequest request) async { - try { - var response = await collect(request); - _logger.i("VLogs: ${response.message}"); - } catch (e) { - _logger.e("VLogs: ${e.toString()}"); - } + void collectAsync(Collector request) { + collect(request) + .then((value) => {_logger.d("VLogs: Collected logs response: $value")}) + .catchError((error) { + _logger.e("VLogs: Error while collecting logs: $error"); + }); } static VLogs createWithOptions(VLogsOptions options) { diff --git a/lib/src/model.dart b/lib/src/model.dart index a135b38..c90fd80 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -13,12 +13,14 @@ class Telegram { String? chatId; TelegramParseMode? parseMode; bool? disabled; + dynamic extras; Telegram({ this.token, this.chatId, this.parseMode, this.disabled, + this.extras, }); Map toMap() { @@ -27,6 +29,7 @@ class Telegram { 'chat_id': chatId, 'parse_mode': parseMode, 'disabled': disabled, + 'extras': extras, }; } @@ -40,6 +43,7 @@ class TelegramBuilder { String? _chatId; TelegramParseMode? _parseMode; bool? _disabled; + dynamic _extras; TelegramBuilder(); @@ -63,12 +67,18 @@ class TelegramBuilder { return this; } + TelegramBuilder extras(dynamic extras) { + _extras = extras; + return this; + } + Telegram build() { return Telegram( token: _token, chatId: _chatId, parseMode: _parseMode, disabled: _disabled, + extras: _extras, ); } } @@ -78,19 +88,22 @@ class Discord { String? webhookToken; String? webhookUrl; bool? disabled; + dynamic extras; Discord({ this.webhookId, this.webhookToken, this.webhookUrl, this.disabled, + this.extras, }); Discord._builder(DiscordBuilder builder) : webhookId = builder._webhookId, webhookToken = builder._webhookToken, webhookUrl = builder._webhookUrl, - disabled = builder._disabled; + disabled = builder._disabled, + extras = builder._extras; Map toMap() { return { @@ -98,6 +111,7 @@ class Discord { 'webhook_token': webhookToken, 'webhook_url': webhookUrl, 'disabled': disabled, + 'extras': extras, }; } @@ -111,6 +125,7 @@ class DiscordBuilder { String? _webhookToken; String? _webhookUrl; bool? _disabled; + dynamic _extras; DiscordBuilder(); @@ -134,6 +149,11 @@ class DiscordBuilder { return this; } + DiscordBuilder extras(dynamic extras) { + _extras = extras; + return this; + } + Discord build() { return Discord._builder(this); } @@ -270,7 +290,7 @@ class TargetBuilder { } } -class CollectorRequest { +class Collector { String? id; String? type; String? source; @@ -281,7 +301,7 @@ class CollectorRequest { Target? target; List? tags; - CollectorRequest( + Collector( {this.id, this.type, this.source, @@ -318,12 +338,12 @@ class CollectorRequest { String toJson() => json.encode(toMap()); - static CollectorRequestBuilder builder() { - return CollectorRequestBuilder(); + static CollectorBuilder builder() { + return CollectorBuilder(); } } -class CollectorRequestBuilder { +class CollectorBuilder { String? _id; String? _type; String? _source; @@ -334,55 +354,55 @@ class CollectorRequestBuilder { Target? _target; List? _tags; - CollectorRequestBuilder(); + CollectorBuilder(); - CollectorRequestBuilder id(String? id) { + CollectorBuilder id(String? id) { _id = id; return this; } - CollectorRequestBuilder type(String? type) { + CollectorBuilder type(String? type) { _type = type; return this; } - CollectorRequestBuilder source(String? source) { + CollectorBuilder source(String? source) { _source = source; return this; } - CollectorRequestBuilder message(String? message) { + CollectorBuilder message(String? message) { _message = message; return this; } - CollectorRequestBuilder data(dynamic data) { + CollectorBuilder data(dynamic data) { _data = data; return this; } - CollectorRequestBuilder userAgent(String? userAgent) { + CollectorBuilder userAgent(String? userAgent) { _userAgent = userAgent; return this; } - CollectorRequestBuilder timestamp(int? timestamp) { + CollectorBuilder timestamp(int? timestamp) { _timestamp = timestamp; return this; } - CollectorRequestBuilder target(Target? target) { + CollectorBuilder target(Target? target) { _target = target; return this; } - CollectorRequestBuilder tags(List? tags) { + CollectorBuilder tags(List? tags) { _tags = tags; return this; } - CollectorRequest build() { - return CollectorRequest( + Collector build() { + return Collector( id: _id, type: _type, source: _source, @@ -413,6 +433,9 @@ class CollectorResponse { String toJson() => json.encode(toMap()); + @override + String toString() => toJson(); + factory CollectorResponse.fromMap(Map map) { return CollectorResponse( message: map['message'], diff --git a/pubspec.yaml b/pubspec.yaml index 0fa6543..61a6eb8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 1.0.0 repository: https://github.com/CUBETIQ/vlogs_sdk_dart.git environment: - sdk: ^3.0.2 + sdk: ">=2.19.0 <3.0.0" dependencies: http: diff --git a/test/vlogs_test.dart b/test/vlogs_test.dart index ec01288..34f624a 100644 --- a/test/vlogs_test.dart +++ b/test/vlogs_test.dart @@ -1,22 +1,21 @@ // ignore_for_file: non_constant_identifier_names import 'package:test/test.dart'; -import 'package:vlogs/src/model.dart'; import 'package:vlogs/vlogs.dart'; void main() { final APP_ID = "72bd14c306a91fa8a590330e3898ddcc"; final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ"; - group('A group of tests', () { + group('Run collecting the logs', () { final sdk = VLogs.create(APP_ID, API_KEY); setUp(() { - // Additional setup goes here. + expect(sdk, isNotNull); }); test('Emit the logs to collector', () async { - var request = CollectorRequest.builder() + var request = Collector.builder() .message("Hello World") .source(CollectorSource.mobile.name) .type(CollectorType.log.name)