Skip to content

Commit

Permalink
fix(api): add missing @MustBeClosed annotations (#109)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Feb 5, 2025
1 parent b3d911a commit 1200422
Show file tree
Hide file tree
Showing 38 changed files with 261 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val

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

Expand All @@ -137,6 +136,15 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
return builder.build()
}

/** `OkHttpClient` always requires a request body for some methods. */
private fun requiresBody(method: HttpMethod): Boolean =
when (method) {
HttpMethod.POST,
HttpMethod.PUT,
HttpMethod.PATCH -> true
else -> false
}

private fun HttpRequest.toUrl(): String {
url?.let {
return it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -160,9 +160,9 @@ internal constructor(
.thenApply { response ->
response
.use { countTokensHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ internal constructor(
.thenApply { response ->
response
.use { retrieveHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -82,9 +82,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
.let { ModelListPageAsync.of(this, params, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -163,9 +163,9 @@ internal constructor(
.thenApply { response ->
response
.use { countTokensHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ internal constructor(
.thenApply { response ->
response
.use { retrieveHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -84,9 +84,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
.let { BetaModelListPageAsync.of(this, params, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -104,9 +104,9 @@ internal constructor(
.thenApply { response ->
response
.use { retrieveHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -137,9 +137,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
.let { BetaMessageBatchListPageAsync.of(this, params, it) }
Expand Down Expand Up @@ -174,9 +174,9 @@ internal constructor(
.thenApply { response ->
response
.use { deleteHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -212,9 +212,9 @@ internal constructor(
.thenApply { response ->
response
.use { cancelHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -93,9 +93,9 @@ internal constructor(
.thenApply { response ->
response
.use { retrieveHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -124,9 +124,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
.let { MessageBatchListPageAsync.of(this, params, it) }
Expand Down Expand Up @@ -158,9 +158,9 @@ internal constructor(
.thenApply { response ->
response
.use { deleteHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down Expand Up @@ -194,9 +194,9 @@ internal constructor(
.thenApply { response ->
response
.use { cancelHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,19 @@ internal constructor(
.body(json(clientOptions.jsonMapper, params._body()))
.build()
.prepare(clientOptions, params)
return clientOptions.httpClient
.execute(
val response =
clientOptions.httpClient.execute(
request,
requestOptions.applyDefaults(
RequestOptions.builder().timeout(Duration.ofMillis(600000)).build()
)
)
.let { response ->
response
.use { createHandler.handle(it) }
.apply {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
}
}
return response
.use { createHandler.handle(it) }
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
it.validate()
}
}
}

Expand Down Expand Up @@ -104,23 +102,21 @@ internal constructor(
)
.build()
.prepare(clientOptions, params)
return clientOptions.httpClient
.execute(
val response =
clientOptions.httpClient.execute(
request,
requestOptions.applyDefaults(
RequestOptions.builder().timeout(Duration.ofMillis(600000)).build()
)
)
.let { response ->
response
.let { createStreamingHandler.handle(it) }
.let { streamResponse ->
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
streamResponse.map { it.validate() }
} else {
streamResponse
}
}
return response
.let { createStreamingHandler.handle(it) }
.let { streamResponse ->
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
streamResponse.map { it.validate() }
} else {
streamResponse
}
}
}
}
Loading

0 comments on commit 1200422

Please sign in to comment.