Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ private constructor(
fun body(body: Body) = apply { this.body = body.toBuilder() }

/** The File object (not file name) to be uploaded. */
fun file(file: InputStream) = apply { body.file(file) }
fun file(file: InputStream) = apply { body.file(file, "file.bin") }

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: InputStream, filename: String) = apply { body.file(file, filename) }

/**
* Sets [Builder.file] to an arbitrary multipart value.
Expand All @@ -130,7 +133,10 @@ private constructor(
fun file(file: MultipartField<InputStream>) = apply { body.file(file) }

/** The File object (not file name) to be uploaded. */
fun file(file: ByteArray) = apply { body.file(file) }
fun file(file: ByteArray) = apply { body.file(file, "file.bin") }

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: ByteArray, filename: String) = apply { body.file(file, filename) }

/** The File object (not file name) to be uploaded. */
fun file(path: Path) = apply { body.file(path) }
Expand Down Expand Up @@ -362,7 +368,11 @@ private constructor(
}

/** The File object (not file name) to be uploaded. */
fun file(file: InputStream) = file(MultipartField.of(file))
fun file(file: InputStream) = file(file, "file.bin")

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: InputStream, filename: String) =
file(MultipartField.builder<InputStream>().value(file).filename(filename).build())

/**
* Sets [Builder.file] to an arbitrary multipart value.
Expand All @@ -374,7 +384,10 @@ private constructor(
fun file(file: MultipartField<InputStream>) = apply { this.file = file }

/** The File object (not file name) to be uploaded. */
fun file(file: ByteArray) = file(file.inputStream())
fun file(file: ByteArray) = file(file, "file.bin")

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: ByteArray, filename: String) = file(file.inputStream(), filename)

/** The File object (not file name) to be uploaded. */
fun file(path: Path) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ private constructor(
fun body(body: Body) = apply { this.body = body.toBuilder() }

/** The File object (not file name) to be uploaded. */
fun file(file: InputStream) = apply { body.file(file) }
fun file(file: InputStream) = apply { body.file(file, "file.bin") }

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: InputStream, filename: String) = apply { body.file(file, filename) }

/**
* Sets [Builder.file] to an arbitrary multipart value.
Expand All @@ -165,7 +168,10 @@ private constructor(
fun file(file: MultipartField<InputStream>) = apply { body.file(file) }

/** The File object (not file name) to be uploaded. */
fun file(file: ByteArray) = apply { body.file(file) }
fun file(file: ByteArray) = apply { body.file(file, "file.bin") }

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: ByteArray, filename: String) = apply { body.file(file, filename) }

/** The File object (not file name) to be uploaded. */
fun file(path: Path) = apply { body.file(path) }
Expand Down Expand Up @@ -459,7 +465,11 @@ private constructor(
}

/** The File object (not file name) to be uploaded. */
fun file(file: InputStream) = file(MultipartField.of(file))
fun file(file: InputStream) = file(file, "file.bin")

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: InputStream, filename: String) =
file(MultipartField.builder<InputStream>().value(file).filename(filename).build())

/**
* Sets [Builder.file] to an arbitrary multipart value.
Expand All @@ -471,7 +481,10 @@ private constructor(
fun file(file: MultipartField<InputStream>) = apply { this.file = file }

/** The File object (not file name) to be uploaded. */
fun file(file: ByteArray) = file(file.inputStream())
fun file(file: ByteArray) = file(file, "file.bin")

/** The File object (not file name) to be uploaded, with an explicit filename. */
fun file(file: ByteArray, filename: String) = file(file.inputStream(), filename)

/** The File object (not file name) to be uploaded. */
fun file(path: Path) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ internal class FileCreateParamsTest {
)
.isEqualTo(
mapOf(
"file" to MultipartField.of("Example data".byteInputStream()),
"file" to
MultipartField.builder<InputStream>()
.value("Example data".byteInputStream())
.filename("file.bin")
.build(),
"file_id" to MultipartField.of("file_id"),
)
.mapValues { (_, field) ->
Expand All @@ -65,4 +69,52 @@ internal class FileCreateParamsTest {

assertThat(body.filterValues { !it.value.isNull() }).isEmpty()
}

@Test
fun fileWithInputStreamUsesDefaultFilename() {
val params =
FileCreateParams.builder()
.containerId("container_id")
.file("Example data".byteInputStream())
.build()

assertThat(params._file().filename()).contains("file.bin")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithBytesUsesDefaultFilename() {
val params =
FileCreateParams.builder()
.containerId("container_id")
.file("Example data".toByteArray())
.build()

assertThat(params._file().filename()).contains("file.bin")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithInputStreamAndFilename() {
val params =
FileCreateParams.builder()
.containerId("container_id")
.file("Example data".byteInputStream(), "container-input.txt")
.build()

assertThat(params._file().filename()).contains("container-input.txt")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithBytesAndFilename() {
val params =
FileCreateParams.builder()
.containerId("container_id")
.file("Example data".toByteArray(), "container-input.txt")
.build()

assertThat(params._file().filename()).contains("container-input.txt")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ internal class FileCreateParamsTest {
)
.isEqualTo(
mapOf(
"file" to MultipartField.of("Example data".byteInputStream()),
"file" to
MultipartField.builder<InputStream>()
.value("Example data".byteInputStream())
.filename("file.bin")
.build(),
"purpose" to MultipartField.of(FilePurpose.ASSISTANTS),
"expires_after" to
MultipartField.of(
Expand Down Expand Up @@ -72,12 +76,61 @@ internal class FileCreateParamsTest {
)
.isEqualTo(
mapOf(
"file" to MultipartField.of("Example data".byteInputStream()),
"file" to
MultipartField.builder<InputStream>()
.value("Example data".byteInputStream())
.filename("file.bin")
.build(),
"purpose" to MultipartField.of(FilePurpose.ASSISTANTS),
)
.mapValues { (_, field) ->
field.map { (it as? ByteArray)?.inputStream() ?: it }
}
)
}

@Test
fun fileWithInputStreamUsesDefaultFilename() {
val params =
FileCreateParams.builder()
.file("Example data".byteInputStream())
.purpose(FilePurpose.BATCH)
.build()

assertThat(params._file().filename()).contains("file.bin")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithBytesUsesDefaultFilename() {
val params =
FileCreateParams.builder().file("Example data".toByteArray()).purpose(FilePurpose.BATCH).build()

assertThat(params._file().filename()).contains("file.bin")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithInputStreamAndFilename() {
val params =
FileCreateParams.builder()
.file("Example data".byteInputStream(), "input.jsonl")
.purpose(FilePurpose.BATCH)
.build()

assertThat(params._file().filename()).contains("input.jsonl")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}

@Test
fun fileWithBytesAndFilename() {
val params =
FileCreateParams.builder()
.file("Example data".toByteArray(), "input.jsonl")
.purpose(FilePurpose.BATCH)
.build()

assertThat(params._file().filename()).contains("input.jsonl")
assertThat(params._file().contentType).isEqualTo("application/octet-stream")
}
}