-
Notifications
You must be signed in to change notification settings - Fork 252
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
Open
sylvesterkaczmarek
wants to merge
5
commits into
openai:main
Choose a base branch
from
sylvesterkaczmarek:feat/http-request-body-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4700b84
feat(core): expose request body content stream
sylvesterkaczmarek bb070b7
feat(core): stream built-in request body content
sylvesterkaczmarek b830b4a
test(core): cover request body content streams
sylvesterkaczmarek fbe81a0
test(core): verify Java request body compatibility
sylvesterkaczmarek 3d39220
test(core): keep Java compatibility test on Java 8
sylvesterkaczmarek 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
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") | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
LoggingHttpClientruns at DEBUG level, it replaces the request body withLoggingHttpRequestBody, which does not overridecontent(). A customHttpClientconsuming that body through the new API therefore invokes this fallback, causingLoggingHttpRequestBody.writeTo()to materialize the entire body in aByteArrayOutputStreambefore returning; largeInputStream-backed multipart uploads consequently lose the streaming behavior introduced here and may exhaust the heap. The logging wrapper needs a streamingcontent()implementation that preserves its logging behavior.Useful? React with 👍 / 👎.