Skip to content

Commit

Permalink
wip(biyi_api_client): Support pro engines
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jun 22, 2024
1 parent cd68210 commit aabf3ea
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
4 changes: 3 additions & 1 deletion apps/biyi_app/lib/services/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:biyi_api_client/biyi_api_client.dart';
export 'package:biyi_api_client/biyi_api_client.dart' hide ApiClient;

ApiClient apiClient = ApiClient();
ApiClient apiClient = ApiClient(
// baseUrl: 'http://localhost:3000',
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class ProOcrEngine extends OcrEngine {

@override
Future<RecognizeTextResponse> recognizeText(RecognizeTextRequest request) {
throw UnsupportedError('recognizeText');
return apiClient.ocrEngine(identifier).recognizeText(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class ProTranslationEngine extends TranslationEngine {

@override
Future<DetectLanguageResponse> detectLanguage(DetectLanguageRequest request) {
throw UnsupportedError('detectLanguage');
return apiClient.engine(identifier).detectLanguage(request);
}

@override
Future<LookUpResponse> lookUp(LookUpRequest request) {
throw UnsupportedError('lookUp');
return apiClient.engine(identifier).lookUp(request);
}

@override
Future<TranslateResponse> translate(TranslateRequest request) {
throw UnsupportedError('translate');
return apiClient.engine(identifier).translate(request);
}
}
40 changes: 40 additions & 0 deletions packages/biyi_api_client/lib/src/apis/engines.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'package:biyi_api_client/src/models/translation_engine_config.dart';
import 'package:biyi_api_client/src/utils/to_uni_translate_client_error.dart';
import 'package:dio/dio.dart';
import 'package:uni_translate_client/uni_translate_client.dart';

class EnginesApi {
EnginesApi(this._dio, {this.engineId});
Expand All @@ -25,4 +27,42 @@ class EnginesApi {
final response = await _dio.get('/api/engines/$engineId');
return TranslationEngineConfig.fromJson(response.data);
}

Future<DetectLanguageResponse> detectLanguage(
DetectLanguageRequest request,
) async {
try {
final response = await _dio.get(
'/api/engines/$engineId/detectLanguage',
queryParameters: {'text': request.texts.first},
);
return DetectLanguageResponse.fromJson(response.data);
} on DioException catch (error) {
throw toUniTranslateClientError(error);
}
}

Future<LookUpResponse> lookUp(LookUpRequest request) async {
try {
final response = await _dio.get(
'/api/engines/$engineId/lookUp',
queryParameters: request.toJson(),
);
return LookUpResponse.fromJson(response.data);
} on DioException catch (error) {
throw toUniTranslateClientError(error);
}
}

Future<TranslateResponse> translate(TranslateRequest request) async {
try {
final response = await _dio.get(
'/api/engines/$engineId/translate',
queryParameters: request.toJson(),
);
return TranslateResponse.fromJson(response.data);
} on DioException catch (error) {
throw toUniTranslateClientError(error);
}
}
}
19 changes: 19 additions & 0 deletions packages/biyi_api_client/lib/src/apis/ocr_engines.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'package:biyi_api_client/src/models/ocr_engine_config.dart';
import 'package:biyi_api_client/src/utils/to_uni_ocr_client_error.dart';
import 'package:dio/dio.dart';
import 'package:uni_ocr_client/uni_ocr_client.dart';

class OcrEnginesApi {
OcrEnginesApi(this._dio, {this.engineId});
Expand All @@ -25,4 +27,21 @@ class OcrEnginesApi {
final response = await _dio.get('/api/ocr-engines/$engineId');
return OcrEngineConfig.fromJson(response.data);
}

/// Recognize text from an image.
Future<RecognizeTextResponse> recognizeText(
RecognizeTextRequest request,
) async {
try {
final response = await _dio.post(
'/api/ocr-engines/$engineId/recognizeText',
data: {
'base64Image': request.getBase64Image(),
},
);
return RecognizeTextResponse.fromJson(response.data);
} on DioException catch (error) {
throw toUniOcrClientError(error);
}
}
}
4 changes: 4 additions & 0 deletions packages/biyi_api_client/lib/src/biyi_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class ApiClient {

OcrEnginesApi get ocrEngines => _ocrEnginesApi;

OcrEnginesApi ocrEngine(String engineId) {
return OcrEnginesApi(_dio, engineId: engineId);
}

VersionsApi get versions => _versionsApi;

VersionsApi version(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:dio/dio.dart';
import 'package:uni_ocr_client/uni_ocr_client.dart';

UniOcrClientError toUniOcrClientError(DioException error) {
Response? response = error.response;
if (response?.data == null && response?.data is! Map) {
return UniOcrClientError(
code: 'unknown',
message: response?.data ?? error.message,
);
}
return UniOcrClientError(
code: '${response?.data['code']}',
message: response?.data['message'],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:dio/dio.dart';
import 'package:uni_translate_client/uni_translate_client.dart';

UniTranslateClientError toUniTranslateClientError(DioException error) {
Response? response = error.response;
if (response?.data == null || response?.data is! Map) {
return UniTranslateClientError(
code: 'unknown',
message: response?.data ?? error.message,
);
}
return UniTranslateClientError(
code: '${response?.data['code']}',
message: response?.data['message'],
);
}

0 comments on commit aabf3ea

Please sign in to comment.