createImageVariation method

Future<ImagesResult> createImageVariation({
  1. required Uint8List imageBytes,
  2. required String filename,
  3. String? model,
  4. int? n,
  5. ImageResponseFormat? responseFormat,
  6. ImageOutputSize? size,
  7. String? user,
})

Implementation

Future<ImagesResult> createImageVariation({
  required Uint8List imageBytes,
  required String filename,
  String? model,
  int? n,
  ImageResponseFormat? responseFormat,
  ImageOutputSize? size,
  String? user,
}) async {
  final boundary = '----dart-openai-image-var-${DateTime.now().microsecondsSinceEpoch.toRadixString(16)}';

  final body = _buildMultipartBody(
    boundary: boundary,
    fileField: 'image',
    filename: filename,
    fileBytes: imageBytes,
    fields: {
      if (model != null) 'model': model,
      if (n != null) 'n': '$n',
      if (responseFormat != null) 'response_format': responseFormat.toJson(),
      if (size != null) 'size': size.toJson(),
      if (user != null) 'user': user,
    },
  );

  final resp = await httpClient.post(
    baseUrl.resolve('images/variations'),
    headers: getHeaders({'Content-Type': 'multipart/form-data; boundary=$boundary'}),
    body: body,
  );

  if (resp.statusCode == 200) {
    return ImagesResult.fromJson(jsonDecode(resp.body));
  }
  throw OpenAIRequestException.fromHttpResponse(resp);
}