Add target builder and more options
This commit is contained in:
parent
f41fabab3f
commit
71ed299a68
@ -6,7 +6,7 @@ void main() async {
|
||||
final APP_ID = "72bd14c306a91fa8a590330e3898ddcc";
|
||||
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()
|
||||
.message("Hello World")
|
||||
|
@ -15,14 +15,10 @@ class VLogs {
|
||||
static final String API_KEY_HEADER_PREFIX = "x-api-key";
|
||||
static final int DEFAULT_CONNECT_TIMEOUT = 60; // seconds
|
||||
|
||||
late String _baseUrl;
|
||||
late String _appId;
|
||||
late String _apiKey;
|
||||
|
||||
late VLogsOptions _options;
|
||||
late VLogsService _service;
|
||||
|
||||
VLogs(VLogsOptions options) {
|
||||
_baseUrl = options.url ?? DEFAULT_VLOGS_URL;
|
||||
if (options.appId == null || options.apiKey == null) {
|
||||
throw Exception("AppId and ApiKey are required");
|
||||
}
|
||||
@ -30,21 +26,24 @@ class VLogs {
|
||||
if (options.apiKey!.isEmpty || options.appId!.isEmpty) {
|
||||
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(
|
||||
"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 {
|
||||
_logger.d("VLogs: Collecting logs for ${request.getId()}");
|
||||
|
||||
var headers = {
|
||||
APP_ID_HEADER_PREFIX: _appId,
|
||||
API_KEY_HEADER_PREFIX: _apiKey,
|
||||
APP_ID_HEADER_PREFIX: _options.appId!,
|
||||
API_KEY_HEADER_PREFIX: _options.apiKey!,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
@ -59,10 +58,21 @@ class VLogs {
|
||||
.build();
|
||||
|
||||
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 {
|
||||
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)";
|
||||
|
||||
var response = await _service.post(request.toJson(), headers: headers);
|
||||
@ -74,15 +84,16 @@ class VLogs {
|
||||
.then((value) => {_logger.d("VLogs: Collected logs response: $value")})
|
||||
.catchError((error) {
|
||||
_logger.e("VLogs: Error while collecting logs: $error");
|
||||
return error;
|
||||
});
|
||||
}
|
||||
|
||||
static VLogs createWithOptions(VLogsOptions options) {
|
||||
static VLogs create(VLogsOptions options) {
|
||||
return VLogs(options);
|
||||
}
|
||||
|
||||
static VLogs create(String appId, String apiKey) {
|
||||
return createWithOptions(VLogsOptions(
|
||||
static VLogs createWith(String appId, String apiKey) {
|
||||
return create(VLogsOptions(
|
||||
appId: appId,
|
||||
apiKey: apiKey,
|
||||
));
|
||||
|
@ -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() {
|
||||
return TargetBuilder();
|
||||
}
|
||||
@ -453,6 +490,7 @@ class VLogsOptions {
|
||||
String? apiKey;
|
||||
int? connectionTimeout;
|
||||
bool? testConnection;
|
||||
Target? target;
|
||||
|
||||
VLogsOptions({
|
||||
this.url,
|
||||
@ -460,6 +498,7 @@ class VLogsOptions {
|
||||
this.apiKey,
|
||||
this.connectionTimeout,
|
||||
this.testConnection,
|
||||
this.target,
|
||||
});
|
||||
|
||||
VLogsOptions._builder(VLogsOptionsBuilder builder)
|
||||
@ -467,7 +506,12 @@ class VLogsOptions {
|
||||
appId = builder._appId,
|
||||
apiKey = builder._apiKey,
|
||||
connectionTimeout = builder._connectionTimeout,
|
||||
testConnection = builder._testConnection;
|
||||
testConnection = builder._testConnection,
|
||||
target = builder._target;
|
||||
|
||||
static VLogsOptionsBuilder builder() {
|
||||
return VLogsOptionsBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
class VLogsOptionsBuilder {
|
||||
@ -476,6 +520,7 @@ class VLogsOptionsBuilder {
|
||||
String? _apiKey;
|
||||
int? _connectionTimeout;
|
||||
bool? _testConnection;
|
||||
Target? _target;
|
||||
|
||||
VLogsOptionsBuilder();
|
||||
|
||||
@ -504,6 +549,29 @@ class VLogsOptionsBuilder {
|
||||
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() {
|
||||
return VLogsOptions._builder(this);
|
||||
}
|
||||
|
@ -8,7 +8,8 @@ void main() {
|
||||
final API_KEY = "vlogs_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ";
|
||||
|
||||
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(() {
|
||||
expect(sdk, isNotNull);
|
||||
@ -16,7 +17,7 @@ void main() {
|
||||
|
||||
test('Emit the logs to collector', () async {
|
||||
var request = Collector.builder()
|
||||
.message("Hello World")
|
||||
.message("Hello from Dart SDK 1.0.0")
|
||||
.source(CollectorSource.mobile.name)
|
||||
.type(CollectorType.log.name)
|
||||
.build();
|
||||
|
Loading…
Reference in New Issue
Block a user