initiateTranscription method

Future<TranscriptionInitResult> initiateTranscription({
  1. required String audioUrl,
  2. String? language,
  3. TranscriptionOptions? options,
})

Sends a request for audio transcription by URL

audioUrl - URL of the audio file for transcription language - audio language (optional) options - additional transcription options Returns TranscriptionInitResult with task ID and URL for getting the result

Implementation

Future<TranscriptionInitResult> initiateTranscription({
  required String audioUrl,
  String? language,
  TranscriptionOptions? options,
}) async {
  try {
    // Prepare request parameters
    final Map<String, dynamic> requestData = {
      'audio_url': audioUrl,
    };

    // Add language if specified
    if (language != null) {
      requestData['language'] = language;
    }

    // Add options if specified
    if (options != null) {
      // Merge parameters from TranscriptionOptions
      final optionsJson = options.toJson();

      // Remove null values to avoid sending unnecessary data
      optionsJson.removeWhere((key, value) => value == null);

      requestData.addAll(optionsJson);
    }

    // Make request for transcription using the new v2/pre-recorded endpoint
    final response = await _dio.post(
      'v2/pre-recorded',
      data: requestData,
    );

    // Check that the response contains data and is of the correct type
    if (response.data == null) {
      throw GladiaApiException(message: 'Empty response from server');
    }

    // Parse response data safely
    Map<String, dynamic> responseData;

    if (response.data is Map<String, dynamic>) {
      responseData = response.data as Map<String, dynamic>;
    } else if (response.data is String) {
      try {
        // Try to parse the string as JSON
        final jsonData = json.decode(response.data as String);
        if (jsonData is Map<String, dynamic>) {
          responseData = jsonData;
        } else {
          throw GladiaApiException(
            message: 'Response JSON is not an object: ${response.data}',
          );
        }
      } catch (e) {
        throw GladiaApiException(
          message:
              'Unable to parse response as JSON: ${response.data}. Error: $e',
        );
      }
    } else {
      throw GladiaApiException(
        message:
            'Invalid response format from server. Expected Map<String, dynamic> or String, got: ${response.data.runtimeType}. Data: ${response.data}',
      );
    }

    // Validate required fields before creating the object
    if (!responseData.containsKey('id')) {
      throw GladiaApiException(
        message:
            'Response missing required field: id. Response: $responseData',
      );
    }

    if (!responseData.containsKey('result_url')) {
      throw GladiaApiException(
        message:
            'Response missing required field: result_url. Response: $responseData',
      );
    }

    return TranscriptionInitResult.fromJson(responseData);
  } on DioException catch (e) {
    throw GladiaApiException.fromDioError(e);
  } catch (e) {
    if (e is GladiaApiException) {
      rethrow;
    }
    throw GladiaApiException(message: e.toString());
  }
}