Repository navigation
feat(core): expose request body content streams #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sylvesterkaczmarek
wants to merge
2
commits into
openai:main
from
sylvesterkaczmarek:feat/http-request-body-content
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
openai-java-core/src/main/kotlin/com/openai/core/http/HttpRequestBody.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
openai-java-core/src/test/java/com/openai/core/http/HttpRequestBodyJavaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.openai.core.http; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| final class HttpRequestBodyJavaTest { | ||
|
|
||
| @Test | ||
| void contentIsJavaDefaultMethod() throws Exception { | ||
| assertThat(HttpRequestBody.class.getMethod("content").isDefault()).isTrue(); | ||
|
|
||
| HttpRequestBody body = | ||
| new HttpRequestBody() { | ||
| @Override | ||
| public void writeTo(OutputStream outputStream) { | ||
| try { | ||
| outputStream.write("body".getBytes(StandardCharsets.UTF_8)); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String contentType() { | ||
| return "text/plain"; | ||
| } | ||
|
|
||
| @Override | ||
| public long contentLength() { | ||
| return 4L; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean repeatable() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| }; | ||
|
|
||
| try (InputStream content = body.content()) { | ||
| byte[] bytes = new byte[4]; | ||
| assertThat(content.read(bytes)).isEqualTo(bytes.length); | ||
| assertThat(bytes).isEqualTo("body".getBytes(StandardCharsets.UTF_8)); | ||
| assertThat(content.read()).isEqualTo(-1); | ||
| } | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
openai-java-core/src/test/kotlin/com/openai/core/http/HttpRequestBodyContentTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.openai.core.http | ||
|
|
||
| import com.openai.core.MultipartField | ||
| import com.openai.core.jsonMapper | ||
| import java.io.ByteArrayOutputStream | ||
| import java.io.OutputStream | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class HttpRequestBodyContentTest { | ||
|
|
||
| @Test | ||
| fun content_defaultsToWriteTo() { | ||
| val body = | ||
| object : HttpRequestBody { | ||
| override fun writeTo(outputStream: OutputStream) { | ||
| outputStream.write("body".toByteArray()) | ||
| } | ||
|
|
||
| override fun contentType(): String = "text/plain" | ||
|
|
||
| override fun contentLength(): Long = 4L | ||
|
|
||
| override fun repeatable(): Boolean = true | ||
|
|
||
| override fun close() {} | ||
| } | ||
|
|
||
| body.content().use { content -> assertThat(content.readBytes()).isEqualTo("body".toByteArray()) } | ||
| } | ||
|
|
||
| @Test | ||
| fun multipartContent_matchesWriteTo() { | ||
| val body = | ||
| multipartFormData( | ||
| jsonMapper(), | ||
| mapOf( | ||
| "field" to | ||
| MultipartField.builder<String>() | ||
| .value("value") | ||
| .contentType("text/plain") | ||
| .build(), | ||
| "binary" to | ||
| MultipartField.builder<ByteArray>() | ||
| .value("abc".toByteArray()) | ||
| .contentType("application/octet-stream") | ||
| .build(), | ||
| ), | ||
| ) | ||
|
|
||
| val output = ByteArrayOutputStream() | ||
| body.writeTo(output) | ||
|
|
||
| body.content().use { content -> assertThat(content.readBytes()).isEqualTo(output.toByteArray()) } | ||
| } | ||
|
|
||
| @Test | ||
| fun multipartContent_streamsInputStreamParts() { | ||
| val body = | ||
| multipartFormData( | ||
| jsonMapper(), | ||
| mapOf( | ||
| "data" to | ||
| MultipartField.builder<java.io.InputStream>() | ||
| .value("stream content".byteInputStream().buffered()) | ||
| .contentType("application/octet-stream") | ||
| .build() | ||
| ), | ||
| ) | ||
|
|
||
| val content = body.content().use { it.readBytes().toString(Charsets.UTF_8) } | ||
|
|
||
| assertThat(body.repeatable()).isFalse() | ||
| assertThat(content).contains("Content-Disposition: form-data; name=\"data\"") | ||
| assertThat(content).contains("Content-Type: application/octet-stream") | ||
| assertThat(content).contains("stream content") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.