Skip to content

Commit 0a60b9a

Browse files
fix(api): add missing @MustBeClosed annotations (#205)
fix(api): switch `CompletableFuture<Void>` to `CompletableFuture<Void?>` fix(client): always provide a body for `PATCH` methods fix(client): add missing validation calls on response chore(internal): minor formatting/style changes chore(internal): rename some tests
1 parent 054403d commit 0a60b9a

File tree

102 files changed

+673
-726
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+673
-726
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
106106

107107
private fun HttpRequest.toRequest(client: okhttp3.OkHttpClient): Request {
108108
var body: RequestBody? = body?.toRequestBody()
109-
// OkHttpClient always requires a request body for PUT and POST methods.
110-
if (body == null && (method == HttpMethod.PUT || method == HttpMethod.POST)) {
109+
if (body == null && requiresBody(method)) {
111110
body = "".toRequestBody()
112111
}
113112

@@ -134,6 +133,15 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
134133
return builder.build()
135134
}
136135

136+
/** `OkHttpClient` always requires a request body for some methods. */
137+
private fun requiresBody(method: HttpMethod): Boolean =
138+
when (method) {
139+
HttpMethod.POST,
140+
HttpMethod.PUT,
141+
HttpMethod.PATCH -> true
142+
else -> false
143+
}
144+
137145
private fun HttpRequest.toUrl(): String {
138146
url?.let {
139147
return it

openai-java-core/src/main/kotlin/com/openai/services/async/BatchServiceAsyncImpl.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ internal constructor(
4848
.thenApply { response ->
4949
response
5050
.use { createHandler.handle(it) }
51-
.apply {
51+
.also {
5252
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
53-
validate()
53+
it.validate()
5454
}
5555
}
5656
}
@@ -75,9 +75,9 @@ internal constructor(
7575
.thenApply { response ->
7676
response
7777
.use { retrieveHandler.handle(it) }
78-
.apply {
78+
.also {
7979
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
80-
validate()
80+
it.validate()
8181
}
8282
}
8383
}
@@ -103,9 +103,9 @@ internal constructor(
103103
.thenApply { response ->
104104
response
105105
.use { listHandler.handle(it) }
106-
.apply {
106+
.also {
107107
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
108-
validate()
108+
it.validate()
109109
}
110110
}
111111
.let { BatchListPageAsync.of(this, params, it) }
@@ -136,9 +136,9 @@ internal constructor(
136136
.thenApply { response ->
137137
response
138138
.use { cancelHandler.handle(it) }
139-
.apply {
139+
.also {
140140
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
141-
validate()
141+
it.validate()
142142
}
143143
}
144144
}

openai-java-core/src/main/kotlin/com/openai/services/async/CompletionServiceAsyncImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ internal constructor(
5151
.thenApply { response ->
5252
response
5353
.use { createHandler.handle(it) }
54-
.apply {
54+
.also {
5555
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
56-
validate()
56+
it.validate()
5757
}
5858
}
5959
}

openai-java-core/src/main/kotlin/com/openai/services/async/EmbeddingServiceAsyncImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ internal constructor(
4545
.thenApply { response ->
4646
response
4747
.use { createHandler.handle(it) }
48-
.apply {
48+
.also {
4949
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
50-
validate()
50+
it.validate()
5151
}
5252
}
5353
}

openai-java-core/src/main/kotlin/com/openai/services/async/FileServiceAsync.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package com.openai.services.async
66

7+
import com.google.errorprone.annotations.MustBeClosed
78
import com.openai.core.RequestOptions
89
import com.openai.core.http.HttpResponse
910
import com.openai.models.FileContentParams
@@ -40,6 +41,7 @@ interface FileServiceAsync {
4041

4142
/** Returns the contents of the specified file. */
4243
@JvmOverloads
44+
@MustBeClosed
4345
fun content(
4446
params: FileContentParams,
4547
requestOptions: RequestOptions = RequestOptions.none()

openai-java-core/src/main/kotlin/com/openai/services/async/FileServiceAsyncImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ internal constructor(
4949
.thenApply { response ->
5050
response
5151
.use { retrieveHandler.handle(it) }
52-
.apply {
52+
.also {
5353
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
54-
validate()
54+
it.validate()
5555
}
5656
}
5757
}
@@ -77,9 +77,9 @@ internal constructor(
7777
.thenApply { response ->
7878
response
7979
.use { listHandler.handle(it) }
80-
.apply {
80+
.also {
8181
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
82-
validate()
82+
it.validate()
8383
}
8484
}
8585
.let { FileListPageAsync.of(this, params, it) }
@@ -106,9 +106,9 @@ internal constructor(
106106
.thenApply { response ->
107107
response
108108
.use { deleteHandler.handle(it) }
109-
.apply {
109+
.also {
110110
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
111-
validate()
111+
it.validate()
112112
}
113113
}
114114
}

openai-java-core/src/main/kotlin/com/openai/services/async/ImageServiceAsyncImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ internal constructor(
4444
.thenApply { response ->
4545
response
4646
.use { generateHandler.handle(it) }
47-
.apply {
47+
.also {
4848
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
49-
validate()
49+
it.validate()
5050
}
5151
}
5252
}

openai-java-core/src/main/kotlin/com/openai/services/async/ModelServiceAsyncImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ internal constructor(
5050
.thenApply { response ->
5151
response
5252
.use { retrieveHandler.handle(it) }
53-
.apply {
53+
.also {
5454
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
55-
validate()
55+
it.validate()
5656
}
5757
}
5858
}
@@ -81,9 +81,9 @@ internal constructor(
8181
.thenApply { response ->
8282
response
8383
.use { listHandler.handle(it) }
84-
.apply {
84+
.also {
8585
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
86-
validate()
86+
it.validate()
8787
}
8888
}
8989
.let { ModelListPageAsync.of(this, params, it) }
@@ -113,9 +113,9 @@ internal constructor(
113113
.thenApply { response ->
114114
response
115115
.use { deleteHandler.handle(it) }
116-
.apply {
116+
.also {
117117
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
118-
validate()
118+
it.validate()
119119
}
120120
}
121121
}

openai-java-core/src/main/kotlin/com/openai/services/async/ModerationServiceAsyncImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ internal constructor(
5252
.thenApply { response ->
5353
response
5454
.use { createHandler.handle(it) }
55-
.apply {
55+
.also {
5656
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
57-
validate()
57+
it.validate()
5858
}
5959
}
6060
}

openai-java-core/src/main/kotlin/com/openai/services/async/UploadServiceAsyncImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ internal constructor(
7171
.thenApply { response ->
7272
response
7373
.use { createHandler.handle(it) }
74-
.apply {
74+
.also {
7575
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
76-
validate()
76+
it.validate()
7777
}
7878
}
7979
}
@@ -99,9 +99,9 @@ internal constructor(
9999
.thenApply { response ->
100100
response
101101
.use { cancelHandler.handle(it) }
102-
.apply {
102+
.also {
103103
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
104-
validate()
104+
it.validate()
105105
}
106106
}
107107
}
@@ -139,9 +139,9 @@ internal constructor(
139139
.thenApply { response ->
140140
response
141141
.use { completeHandler.handle(it) }
142-
.apply {
142+
.also {
143143
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
144-
validate()
144+
it.validate()
145145
}
146146
}
147147
}

openai-java-core/src/main/kotlin/com/openai/services/async/beta/AssistantServiceAsyncImpl.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ internal constructor(
5757
.thenApply { response ->
5858
response
5959
.use { createHandler.handle(it) }
60-
.apply {
60+
.also {
6161
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
62-
validate()
62+
it.validate()
6363
}
6464
}
6565
}
@@ -85,9 +85,9 @@ internal constructor(
8585
.thenApply { response ->
8686
response
8787
.use { retrieveHandler.handle(it) }
88-
.apply {
88+
.also {
8989
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
90-
validate()
90+
it.validate()
9191
}
9292
}
9393
}
@@ -114,9 +114,9 @@ internal constructor(
114114
.thenApply { response ->
115115
response
116116
.use { updateHandler.handle(it) }
117-
.apply {
117+
.also {
118118
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
119-
validate()
119+
it.validate()
120120
}
121121
}
122122
}
@@ -143,9 +143,9 @@ internal constructor(
143143
.thenApply { response ->
144144
response
145145
.use { listHandler.handle(it) }
146-
.apply {
146+
.also {
147147
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
148-
validate()
148+
it.validate()
149149
}
150150
}
151151
.let { BetaAssistantListPageAsync.of(this, params, it) }
@@ -173,9 +173,9 @@ internal constructor(
173173
.thenApply { response ->
174174
response
175175
.use { deleteHandler.handle(it) }
176-
.apply {
176+
.also {
177177
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
178-
validate()
178+
it.validate()
179179
}
180180
}
181181
}

openai-java-core/src/main/kotlin/com/openai/services/async/beta/ThreadServiceAsyncImpl.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ internal constructor(
7777
.thenApply { response ->
7878
response
7979
.use { createHandler.handle(it) }
80-
.apply {
80+
.also {
8181
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
82-
validate()
82+
it.validate()
8383
}
8484
}
8585
}
@@ -105,9 +105,9 @@ internal constructor(
105105
.thenApply { response ->
106106
response
107107
.use { retrieveHandler.handle(it) }
108-
.apply {
108+
.also {
109109
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
110-
validate()
110+
it.validate()
111111
}
112112
}
113113
}
@@ -134,9 +134,9 @@ internal constructor(
134134
.thenApply { response ->
135135
response
136136
.use { updateHandler.handle(it) }
137-
.apply {
137+
.also {
138138
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
139-
validate()
139+
it.validate()
140140
}
141141
}
142142
}
@@ -163,9 +163,9 @@ internal constructor(
163163
.thenApply { response ->
164164
response
165165
.use { deleteHandler.handle(it) }
166-
.apply {
166+
.also {
167167
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
168-
validate()
168+
it.validate()
169169
}
170170
}
171171
}
@@ -196,9 +196,9 @@ internal constructor(
196196
.thenApply { response ->
197197
response
198198
.use { createAndRunHandler.handle(it) }
199-
.apply {
199+
.also {
200200
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
201-
validate()
201+
it.validate()
202202
}
203203
}
204204
}

0 commit comments

Comments
 (0)