Skip to content

Commit

Permalink
[ Add ] added auth implmenttaions for each users, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfik committed Dec 18, 2023
1 parent 9f252e6 commit d051fe6
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@
## 0.9.5

- Added support for the AI instruction field which allow for your own custom instructions to be executed during the localization process.

## 1.0.0

- Expose unique users authentication via their API keys got from <https://my.langsync.app>
- Bug Fixes
7 changes: 1 addition & 6 deletions bin/langsync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ Future<void> main(List<String> args) async {

Hive.init(langSyncDir.path);

final box = await Hive.openBox<dynamic>('config');

await box.put(
'apiKey',
'f3c5d5721020da969a0a1f65686723a1484ba4bd167b0b08daeaa6bb6312fefc',
);
await Hive.openBox<dynamic>('config');

await _flushThenExit(await LangsyncCommandRunner().run(args));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:cli_completion/cli_completion.dart';
import 'package:langsync/src/commands/auth_command/auth.dart';
import 'package:langsync/src/commands/config_command/config_command.dart';
import 'package:langsync/src/commands/debug_info/debug_info.dart';
import 'package:langsync/src/commands/start_command/start_command.dart';
Expand Down Expand Up @@ -43,9 +44,8 @@ class LangsyncCommandRunner extends CompletionCommandRunner<int> {
help: 'Print verbose output.',
);

// addCommand(AccountCommand(logger: _logger));
addCommand(AuthCommand(logger: _logger));
addCommand(ConfigCommand(logger: _logger));
// addCommand(SupportedLangsCommand(logger: _logger));
addCommand(StartCommand(logger: _logger));

if (utils.isDebugMode) addCommand(DebugInfoCommand(logger: _logger));
Expand Down
63 changes: 63 additions & 0 deletions lib/src/commands/auth_command/auth.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:args/command_runner.dart';
import 'package:hive/hive.dart';
import 'package:langsync/src/etc/extensions.dart';
import 'package:mason_logger/mason_logger.dart';

class AuthCommand extends Command<int> {
final Logger logger;

AuthCommand({
required this.logger,
}) {
argParser.addOption(
'apiKey',
abbr: 'k',
help: 'The API key to use for authentication.',
valueHelp: 'API_KEY',
);
}

@override
String get description =>
'Authenticate with the LangSync CLI using an API key. (You can get an API key from https://my.langsync.app)';

@override
String get name => 'auth';

@override
Future<int> run() async {
final apiKey = argResults!['apiKey'] as String?;

if (apiKey == null) {
logger.info('Please provide an API key.');
return ExitCode.usage.code;
}

final progress = logger.progress('Saving Your API Key...');

try {
await _saveApiKey(apiKey);

progress.complete('Your API key has been saved successfully.');

return ExitCode.success.code;
} catch (e, stack) {
logger.customErr(
update:
'An error occurred while saving your API key, please try again.',
progress: progress,
error: e,
stacktrace: stack,
);

return ExitCode.software.code;
}
}

Future<void> _saveApiKey(String apiKey) async {
final box = Hive.box<dynamic>('config');

await box.put('apiKey', apiKey);
}
}
6 changes: 3 additions & 3 deletions lib/src/commands/start_command/start_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class StartCommand extends Command<int> {
for (var index = 0; index < outputList.length; index++) {
final current = outputList[index];

final isError = current.jsonFormattedResponse['langsyncError'] != null;
final isError = current.objectDecodedResponse['langsyncError'] != null;

if (isError) {
final fileName = '${current.lang}.error.json';
Expand All @@ -123,7 +123,7 @@ class StartCommand extends Command<int> {
'message':
'Please, if you think that this is an unexpected bug in LangSync, contact us so we can help',
'operationId': operationId,
'processedResponse': current.jsonFormattedResponse,
'processedResponse': current.objectDecodedResponse,
'target_language': current.lang,
'success_file_name': '${current.lang}.json',
'LocalizationTryDate': {
Expand All @@ -147,7 +147,7 @@ class StartCommand extends Command<int> {
await file.create();
await file.writeAsString(
const JsonEncoder.withIndent(' ')
.convert(current.jsonFormattedResponse),
.convert(current.objectDecodedResponse),
);

progress.complete(
Expand Down
32 changes: 19 additions & 13 deletions lib/src/etc/models/lang_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@ import 'package:equatable/equatable.dart';
/// A Language Output model, it holds the language name, the localized at date and the json formatted response.
/// {@endtemplate}
class LangOutput extends Equatable {
/// The language name.
final String lang;

/// The localized at date.
final DateTime localizedAt;

/// The json formatted response.
final Map<String, dynamic> jsonFormattedResponse;

/// {@macro lang_output}
const LangOutput({
required this.lang,
required this.localizedAt,
required this.jsonFormattedResponse,
required this.objectDecodedResponse,
required this.adaptedResponse,
});

/// Creates a [LangOutput] from a [Map].
factory LangOutput.fromJson(Map<String, dynamic> json) {
return LangOutput(
lang: json['lang'] as String,
localizedAt: DateTime.parse(json['localizedAt'] as String),
jsonFormattedResponse:
json['jsonDecodedResponse'] as Map<String, dynamic>,
objectDecodedResponse:
json['objectDecodedResponse'] as Map<String, dynamic>,
adaptedResponse: json['adaptedResponse'] as String,
);
}

/// The language name.
final String lang;

/// The localized at date.
final DateTime localizedAt;

/// The json formatted response.
final Map<String, dynamic> objectDecodedResponse;

/// The adapted response.
final String adaptedResponse;

@override
List<Object?> get props => [
lang,
localizedAt,
jsonFormattedResponse,
objectDecodedResponse,
adaptedResponse,
];
}
6 changes: 3 additions & 3 deletions lib/src/etc/models/operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import 'package:equatable/equatable.dart';
/// An Operation model, it holds the operation id and the operation type.
/// {@endtemplate}
class SaveFileOperation extends Equatable {
/// The operation id.
final String operationId;

/// {@macro operation}
const SaveFileOperation({
required this.operationId,
Expand All @@ -19,6 +16,9 @@ class SaveFileOperation extends Equatable {
);
}

/// The operation id.
final String operationId;

@override
List<Object?> get props => [
operationId,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/etc/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'dart:math';
final utils = Utils();

class Utils {
final isDebugMode = false;
final isDebugMode = true;

//! notice the "/"
String get baseUrl =>
isDebugMode ? 'http://192.168.0.11:5559' : 'http://api.langsync.app';
isDebugMode ? 'http://192.168.0.5:5559' : 'http://api.langsync.app';

bool isValidApiKeyFormatted(String apiKey) {
final isNotEmpty = apiKey.isNotEmpty;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const packageVersion = '0.9.51';
const packageVersion = '1.0.0';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: langsync
description: an AI powered localization tool for any platform and language.
version: 0.9.52
version: 1.0.0
homepage: https://langsync.app
repository: https://github.com/LangSync/cli

Expand Down

0 comments on commit d051fe6

Please sign in to comment.