Skip to content

Commit

Permalink
[ Add ] added mechanism for reading NGINX buffered sse events as lists
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfik committed Oct 21, 2023
1 parent 42283af commit 54076b9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Binary file modified bin/langsync.exe
Binary file not shown.
17 changes: 11 additions & 6 deletions lib/src/commands/start_command/start_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,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',
'partitionId': partitionId,
'processedResponse': current.jsonFormattedResponse.toString(),
'processedResponse': current.jsonFormattedResponse,
'target_language': current.lang,
'success_file_name': '${current.lang}.json',
'LocalizationTryDate': {
Expand Down Expand Up @@ -272,16 +272,21 @@ class StartCommand extends Command<int> {
apiKey: apiKey,
langs: langs,
jsonPartitionId: partitionId,
languageLocalizationMaxDelay: languageLocalizationMaxDelay,
);

LangSyncServerResultSSE? resultSSE;

processStream.listen(
(event) {
if (event is LangSyncServerResultSSE) {
resultSSE = event;
} else {
logger.info(event.message);
(events) {
for (var i = 0; i < events.length; i++) {
final curr = events[i];

if (curr is LangSyncServerResultSSE) {
resultSSE = curr;
} else {
logger.info(curr.message);
}
}
},
onDone: () {
Expand Down
13 changes: 9 additions & 4 deletions lib/src/etc/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class NetClient extends NetClientBoilerPlate {
});
}

Stream<LangSyncServerSSE> startAIProcess({
Stream<List<LangSyncServerSSE>> startAIProcess({
required Iterable<String> langs,
required String apiKey,
required String jsonPartitionId,
required int? languageLocalizationMaxDelay,
bool includeOutput = false,
}) {
return sseStreamReq(
return sseStreamReq<List<LangSyncServerSSE>>(
'/process-translation',
'POST',
{'Authorization': 'Bearer $apiKey'},
Expand All @@ -59,9 +59,14 @@ class NetClient extends NetClientBoilerPlate {
'languageLocalizationMaxDelay': languageLocalizationMaxDelay,
},
(res) {
final decoded = jsonDecode(res) as Map<String, dynamic>;
final split = res.split('\n\n').where((element) => element.isNotEmpty);

return LangSyncServerSSE.fromJson(decoded);
return split.map((event) {
final decodedEvent = jsonDecode(event.trim().replaceAll('\n', ''))
as Map<String, dynamic>;

return LangSyncServerSSE.fromJson(decodedEvent);
}).toList();
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/etc/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Utils {

//! notice the "/"
String get baseUrl =>
isDebugMode ? 'http://192.168.0.2:5559' : 'http://test.langsync.app:5560';
isDebugMode ? 'http://192.168.0.4:5559' : 'http://api.langsync.app';

bool isValidApiKeyFormatted(String apiKey) {
final isNotEmpty = apiKey.isNotEmpty;
Expand Down

0 comments on commit 54076b9

Please sign in to comment.