Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.169.0"
".": "0.169.1"
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 0.169.1 (2025-01-30)

Full Changelog: [v0.169.0...v0.169.1](https://github.com/Increase/increase-java/compare/v0.169.0...v0.169.1)

### Bug Fixes

* **client:** don't leak responses when retrying ([#735](https://github.com/Increase/increase-java/issues/735)) ([ffb6259](https://github.com/Increase/increase-java/commit/ffb6259df249b9e3f02fa2b4647e4fc64b0d25e0))


### Documentation

* fix incorrect additional properties info ([#737](https://github.com/Increase/increase-java/issues/737)) ([5eb497a](https://github.com/Increase/increase-java/commit/5eb497a37fa360731e0beeffc05456985bb4b9b6))

## 0.169.0 (2025-01-30)

Full Changelog: [v0.168.0...v0.169.0](https://github.com/Increase/increase-java/compare/v0.168.0...v0.169.0)
Expand Down
38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/com.increase.api/increase-java)](https://central.sonatype.com/artifact/com.increase.api/increase-java/0.169.0)
[![Maven Central](https://img.shields.io/maven-central/v/com.increase.api/increase-java)](https://central.sonatype.com/artifact/com.increase.api/increase-java/0.169.1)

<!-- x-release-please-end -->

Expand All @@ -19,7 +19,7 @@ The REST API documentation can be found on [increase.com](https://increase.com/d
### Gradle

```kotlin
implementation("com.increase.api:increase-java:0.169.0")
implementation("com.increase.api:increase-java:0.169.1")
```

### Maven
Expand All @@ -28,7 +28,7 @@ implementation("com.increase.api:increase-java:0.169.0")
<dependency>
<groupId>com.increase.api</groupId>
<artifactId>increase-java</artifactId>
<version>0.169.0</version>
<version>0.169.1</version>
</dependency>
```

Expand Down Expand Up @@ -151,19 +151,7 @@ See [Pagination](#pagination) below for more information on transparently workin

To make a request to the Increase API, you generally build an instance of the appropriate `Params` class.

In [Example: creating a resource](#example-creating-a-resource) above, we used the `AccountCreateParams.builder()` to pass to the `create` method of the `accounts` service.

Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case, you can attach them using the `putAdditionalProperty` method.

```java
import com.increase.api.core.JsonValue;
import com.increase.api.models.AccountCreateParams;

AccountCreateParams params = AccountCreateParams.builder()
// ... normal properties
.putAdditionalProperty("secret_param", JsonValue.from("4242"))
.build();
```
See [Undocumented request params](#undocumented-request-params) for how to send arbitrary parameters.

## Responses

Expand Down Expand Up @@ -371,18 +359,26 @@ This library is typed for convenient access to the documented API. If you need t

### Undocumented request params

To make requests using undocumented parameters, you can provide or override parameters on the params object while building it.
In [Example: creating a resource](#example-creating-a-resource) above, we used the `AccountCreateParams.builder()` to pass to the `create` method of the `accounts` service.

Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case, you can attach them using raw setters:

```java
FooCreateParams address = FooCreateParams.builder()
.id("my_id")
.putAdditionalProperty("secret_prop", JsonValue.from("hello"))
import com.increase.api.core.JsonValue;
import com.increase.api.models.AccountCreateParams;

AccountCreateParams params = AccountCreateParams.builder()
.putAdditionalHeader("Secret-Header", "42")
.putAdditionalQueryParam("secret_query_param", "42")
.putAdditionalBodyProperty("secretProperty", JsonValue.from("42"))
.build();
```

You can also use the `putAdditionalProperty` method on nested headers, query params, or body objects.

### Undocumented response properties

To access undocumented response properties, you can use `res._additionalProperties()` on a response object to get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like `._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class to extract it to a desired type.
To access undocumented response properties, you can use `res._additionalProperties()` on a response object to get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like `res._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class to extract it to a desired type.

## Logging

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
allprojects {
group = "com.increase.api"
version = "0.169.0" // x-release-please-version
version = "0.169.1" // x-release-please-version
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ private constructor(
}

response
} catch (t: Throwable) {
if (++retries > maxRetries || !shouldRetry(t)) {
throw t
} catch (throwable: Throwable) {
if (++retries > maxRetries || !shouldRetry(throwable)) {
throw throwable
}

null
}

val backoffMillis = getRetryBackoffMillis(retries, response)
// All responses must be closed, so close the failed one before retrying.
response?.close()
Thread.sleep(backoffMillis.toMillis())
}
}
Expand Down Expand Up @@ -113,6 +115,8 @@ private constructor(
}

val backoffMillis = getRetryBackoffMillis(retries, response)
// All responses must be closed, so close the failed one before retrying.
response?.close()
return sleepAsync(backoffMillis.toMillis()).thenCompose {
executeWithRetries(requestWithRetryCount, requestOptions)
}
Expand Down Expand Up @@ -223,23 +227,23 @@ private constructor(
return Duration.ofNanos((TimeUnit.SECONDS.toNanos(1) * backoffSeconds * jitter).toLong())
}

private fun sleepAsync(millis: Long): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
TIMER.schedule(
object : TimerTask() {
override fun run() {
future.complete(null)
}
},
millis
)
return future
}

companion object {

private val TIMER = Timer("RetryingHttpClient", true)

private fun sleepAsync(millis: Long): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
TIMER.schedule(
object : TimerTask() {
override fun run() {
future.complete(null)
}
},
millis
)
return future
}

@JvmStatic fun builder() = Builder()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
import com.github.tomakehurst.wiremock.junit5.WireMockTest
import com.github.tomakehurst.wiremock.stubbing.Scenario
import com.increase.api.client.okhttp.OkHttpClient
import com.increase.api.core.RequestOptions
import java.io.InputStream
import java.util.concurrent.CompletableFuture
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -13,11 +16,49 @@ import org.junit.jupiter.params.provider.ValueSource
@WireMockTest
internal class RetryingHttpClientTest {

private var openResponseCount = 0
private lateinit var httpClient: HttpClient

@BeforeEach
fun beforeEach(wmRuntimeInfo: WireMockRuntimeInfo) {
httpClient = OkHttpClient.builder().baseUrl(wmRuntimeInfo.httpBaseUrl).build()
val okHttpClient = OkHttpClient.builder().baseUrl(wmRuntimeInfo.httpBaseUrl).build()
httpClient =
object : HttpClient {
override fun execute(
request: HttpRequest,
requestOptions: RequestOptions
): HttpResponse = trackClose(okHttpClient.execute(request, requestOptions))

override fun executeAsync(
request: HttpRequest,
requestOptions: RequestOptions
): CompletableFuture<HttpResponse> =
okHttpClient.executeAsync(request, requestOptions).thenApply { trackClose(it) }

override fun close() = okHttpClient.close()

private fun trackClose(response: HttpResponse): HttpResponse {
openResponseCount++
return object : HttpResponse {
private var isClosed = false

override fun statusCode(): Int = response.statusCode()

override fun headers(): Headers = response.headers()

override fun body(): InputStream = response.body()

override fun close() {
response.close()
if (isClosed) {
return
}
openResponseCount--
isClosed = true
}
}
}
}
resetAllScenarios()
}

Expand All @@ -35,6 +76,7 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(1, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

@ParameterizedTest
Expand All @@ -60,6 +102,7 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(1, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -116,6 +159,7 @@ internal class RetryingHttpClientTest {
postRequestedFor(urlPathEqualTo("/something"))
.withHeader("x-stainless-retry-count", equalTo("2"))
)
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -156,6 +200,7 @@ internal class RetryingHttpClientTest {
postRequestedFor(urlPathEqualTo("/something"))
.withHeader("x-stainless-retry-count", equalTo("42"))
)
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -186,8 +231,13 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(2, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

private fun HttpClient.execute(request: HttpRequest, async: Boolean): HttpResponse =
if (async) executeAsync(request).get() else execute(request)

// When retrying, all failed responses should be closed. Only the final returned response should
// be open.
private fun assertNoResponseLeaks() = assertThat(openResponseCount).isEqualTo(1)
}