@@ -27,7 +27,7 @@ import 'package:http/http.dart' as http;
27
27
import './common.dart' ;
28
28
29
29
/// Send JSON POST request to API server.
30
- Future <Response ?> _makeRequest (String apiURL, String apiKey, Map <String , dynamic > qargs,
30
+ Future <Response ?> _makePOSTRequest (String apiURL, String apiKey, Map <String , dynamic > qargs,
31
31
[void Function (Map ? result)? handler]) async {
32
32
dlog ("Sending query POST request to $apiURL : ${qargs .toString ()}" );
33
33
Response ? response;
@@ -68,8 +68,8 @@ Future<Response?> _makeRequest(String apiURL, String apiKey, Map<String, dynamic
68
68
return response;
69
69
}
70
70
71
- /// Static class wrapper for functions communicating with the Embla REST API.
72
- class EmblaRESTAPI {
71
+ /// Static class wrapper for functions communicating directly with the Embla REST API.
72
+ class EmblaAPI {
73
73
/// Send request to clear query history for a given device ID.
74
74
/// Boolean [allData] param determines whether all device-specific
75
75
/// data or only query history should be deleted server-side.
@@ -83,6 +83,59 @@ class EmblaRESTAPI {
83
83
};
84
84
85
85
final String apiURL = "$serverURL $kClearHistoryEndpoint " ;
86
- await _makeRequest (apiURL, apiKey, qargs, completionHandler);
86
+ await _makePOSTRequest (apiURL, apiKey, qargs, completionHandler);
87
+ }
88
+
89
+ /// Send request to speech synthesis API (static method).
90
+ ///
91
+ /// [text] Text to be speech synthesized
92
+ /// [apiKey] Server API key
93
+ /// Returns the resulting audio file URL or null if an error occurred.
94
+ static Future <String ?> synthesize (String text, String ? apiKey,
95
+ {String voiceID = kDefaultSpeechSynthesisVoice,
96
+ double voiceSpeed = kDefaultSpeechSynthesisSpeed,
97
+ String apiURL = "$kDefaultServer $kSpeechSynthesisEndpoint " }) async {
98
+ // Set URI
99
+ Uri uri;
100
+ try {
101
+ uri = Uri .parse (apiURL);
102
+ } on FormatException {
103
+ dlog ("Invalid URL specified for TTS synthesis." );
104
+ return null ;
105
+ }
106
+ // Set request headers
107
+ Map <String , String > headers = {
108
+ "X-API-Key" : apiKey ?? "" ,
109
+ "Content-Type" : "application/json" ,
110
+ "Accept" : "application/json"
111
+ };
112
+ // Set request body
113
+ String body = json.encode ({
114
+ 'text' : text,
115
+ 'options' : {'voice_id' : voiceID, 'voice_speed' : voiceSpeed.toString ()}
116
+ });
117
+
118
+ dlog ("Sending POST request to $apiURL : $body " );
119
+ return await http.post (uri, headers: headers, body: body).timeout (kRequestTimeout).then (
120
+ (response) {
121
+ dlog ("Response status: ${response .statusCode }" );
122
+ dlog ("Response body: ${response .body }" );
123
+ if (response.statusCode != 200 ) {
124
+ dlog ("Received invalid status code from TTS service." );
125
+ return null ;
126
+ }
127
+ dynamic arg = json.decode (response.body);
128
+ if (arg is Map && arg.containsKey ("audio_url" )) {
129
+ // Valid response body
130
+ // Return the resulting audio file URL
131
+ return arg["audio_url" ];
132
+ }
133
+ dlog ("Invalid response body from TTS service." );
134
+ return null ;
135
+ }, onError: (e) {
136
+ dlog ("Error in speech synthesis: $e " );
137
+ // Errors during request, return null
138
+ return null ;
139
+ });
87
140
}
88
141
}
0 commit comments