intron_voice 0.1.0
intron_voice: ^0.1.0 copied to clipboard
A typed Dart SDK contract for the Intron Voice API.
import 'dart:convert';
import 'dart:typed_data';
import 'package:intron_voice/intron_voice.dart';
Future<void> main() async {
final httpTransport = ExampleHttpTransport();
final client = IntronClient(
config: IntronClientConfig(tokenProvider: _ExampleTokenProvider()),
httpTransport: httpTransport,
);
try {
final job = await client.uploadAudioFile(
audioFile: SttAudioFile(
ExampleUploadSource(
fileName: 'visit.wav',
bytes: Uint8List.fromList(<int>[82, 73, 70, 70]),
),
),
options: SttUploadOptions.forLanguage(
IntronLanguage.english,
enableDiarization: true,
),
);
final transcript = await client.waitForTranscription(
fileId: job.fileId,
pollingInterval: const Duration(seconds: 2),
timeout: const Duration(minutes: 10),
onStatus: (status) {
print('Transcription status: ${status.status.value}');
},
);
print('Transcript: ${transcript.transcript}');
print('HTTP requests sent: ${httpTransport.requests.length}');
} on IntronRateLimitException catch (error) {
print('Rate limited. Retry after: ${error.retryAfter}.');
} on IntronApiException catch (error) {
print('Intron request failed: ${error.message}');
} finally {
await client.dispose();
await httpTransport.close();
}
}
final class ExampleHttpTransport implements IntronHttpTransport {
final List<IntronHttpRequest> requests = <IntronHttpRequest>[];
@override
Future<IntronHttpResponse> send(IntronHttpRequest request) async {
requests.add(request);
switch (request.url.path) {
case '/file/v1/upload':
return _jsonResponse(<String, Object?>{
'file_id': 'file_123',
'processing_status': 'FILE_QUEUED',
'audio_file_name': request.multipartFiles.single.source.fileName,
});
case '/file/v1/status/file_123':
return _jsonResponse(<String, Object?>{
'file_id': 'file_123',
'processing_status': 'FILE_TRANSCRIBED',
'audio_transcript': 'Patient reports feeling well today.',
'audio_file_name': 'visit.wav',
});
default:
return _jsonResponse(
<String, Object?>{'message': 'Unexpected example request.'},
statusCode: 404,
);
}
}
@override
Future<void> close() async {}
IntronHttpResponse _jsonResponse(
Map<String, Object?> body, {
int statusCode = 200,
}) {
return IntronHttpResponse(
statusCode: statusCode,
headers: <String, String>{'content-type': 'application/json'},
body: Uint8List.fromList(utf8.encode(jsonEncode(body))),
);
}
}
final class ExampleUploadSource implements IntronFileUploadSource {
ExampleUploadSource({required this.fileName, required Uint8List bytes})
: _bytes = bytes;
final Uint8List _bytes;
@override
final String fileName;
@override
int? get length => _bytes.length;
@override
Stream<Uint8List> openRead() => Stream<Uint8List>.value(_bytes);
}
final class _ExampleTokenProvider implements IntronTokenProvider {
@override
Future<String> resolveToken() async {
return 'short-lived-token-from-your-backend';
}
}