postJson method
Make a POST request and return JSON response
Implementation
Future<Map<String, dynamic>> postJson(
String endpoint,
Map<String, dynamic> data,
) async {
try {
if (_logger.isLoggable(Level.FINE)) {
_logger.fine('Phind request payload: ${jsonEncode(data)}');
}
final response = await _dio.post(endpoint, data: data);
_logger.info('Phind HTTP status: ${response.statusCode}');
if (response.statusCode != 200) {
throw ProviderError(
'Phind API returned status ${response.statusCode}: ${response.data}',
);
}
// Phind returns streaming response even for non-streaming requests
final responseText = response.data as String;
final content = _parsePhindStreamResponse(responseText);
if (content.isEmpty) {
throw const ProviderError('No completion choice returned.');
}
// Return a mock JSON response with the parsed content
return {
'choices': [
{
'message': {'content': content}
}
]
};
} on DioException catch (e) {
throw DioErrorHandler.handleDioError(e, 'Phind');
}
}