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.1.0-alpha.4"
".": "0.1.0-alpha.5"
}
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## 0.1.0-alpha.5 (2025-02-06)

Full Changelog: [v0.1.0-alpha.4...v0.1.0-alpha.5](https://github.com/terminaldotshop/terminal-sdk-java/compare/v0.1.0-alpha.4...v0.1.0-alpha.5)

### Features

* **client:** send client-side timeout headers ([#64](https://github.com/terminaldotshop/terminal-sdk-java/issues/64)) ([33875ee](https://github.com/terminaldotshop/terminal-sdk-java/commit/33875ee07c693dbf248413bee093d59f5ae07674))


### Bug Fixes

* **api:** add missing `@MustBeClosed` annotations ([#65](https://github.com/terminaldotshop/terminal-sdk-java/issues/65)) ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))
* **api:** switch `CompletableFuture&lt;Void&gt;` to `CompletableFuture<Void?>` ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))
* **client:** add missing validation calls on response ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))
* **client:** always provide a body for `PATCH` methods ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))


### Chores

* **internal:** minor formatting/style changes ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))
* **internal:** rename some tests ([a5abcfa](https://github.com/terminaldotshop/terminal-sdk-java/commit/a5abcfa612d7be9870619a45cb97c1a25280371c))


### Documentation

* fix incorrect additional properties info ([#62](https://github.com/terminaldotshop/terminal-sdk-java/issues/62)) ([5948868](https://github.com/terminaldotshop/terminal-sdk-java/commit/59488684132b21f7dfb292e4389d471ea08e3319))

## 0.1.0-alpha.4 (2025-01-30)

Full Changelog: [v0.1.0-alpha.3...v0.1.0-alpha.4](https://github.com/terminaldotshop/terminal-sdk-java/compare/v0.1.0-alpha.3...v0.1.0-alpha.4)
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/shop.terminal.api/terminal-java)](https://central.sonatype.com/artifact/shop.terminal.api/terminal-java/0.1.0-alpha.4)
[![Maven Central](https://img.shields.io/maven-central/v/shop.terminal.api/terminal-java)](https://central.sonatype.com/artifact/shop.terminal.api/terminal-java/0.1.0-alpha.5)

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

Expand All @@ -21,7 +21,7 @@ The REST API documentation can be found on [terminal.shop](https://terminal.shop
### Gradle

```kotlin
implementation("shop.terminal.api:terminal-java:0.1.0-alpha.4")
implementation("shop.terminal.api:terminal-java:0.1.0-alpha.5")
```

### Maven
Expand All @@ -30,7 +30,7 @@ implementation("shop.terminal.api:terminal-java:0.1.0-alpha.4")
<dependency>
<groupId>shop.terminal.api</groupId>
<artifactId>terminal-java</artifactId>
<version>0.1.0-alpha.4</version>
<version>0.1.0-alpha.5</version>
</dependency>
```

Expand Down Expand Up @@ -98,19 +98,7 @@ ProductListResponse product = client.product().list(params);

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

In [Example: creating a resource](#example-creating-a-resource) above, we used the `ProductListParams.builder()` to pass to the `list` method of the `product` 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 shop.terminal.api.core.JsonValue;
import shop.terminal.api.models.ProductListParams;

ProductListParams params = ProductListParams.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 @@ -252,18 +240,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 `ProductListParams.builder()` to pass to the `list` method of the `product` 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 shop.terminal.api.core.JsonValue;
import shop.terminal.api.models.ProductListParams;

ProductListParams params = ProductListParams.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 = "shop.terminal.api"
version = "0.1.0-alpha.4" // x-release-please-version
version = "0.1.0-alpha.5" // x-release-please-version
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,11 @@ class OkHttpClient
private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val baseUrl: HttpUrl) :
HttpClient {

private fun getClient(requestOptions: RequestOptions): okhttp3.OkHttpClient {
val clientBuilder = okHttpClient.newBuilder()

val logLevel =
when (System.getenv("TERMINAL_LOG")?.lowercase()) {
"info" -> HttpLoggingInterceptor.Level.BASIC
"debug" -> HttpLoggingInterceptor.Level.BODY
else -> null
}
if (logLevel != null) {
clientBuilder.addNetworkInterceptor(
HttpLoggingInterceptor().setLevel(logLevel).apply { redactHeader("Authorization") }
)
}

val timeout = requestOptions.timeout
if (timeout != null) {
clientBuilder
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
}

return clientBuilder.build()
}

override fun execute(
request: HttpRequest,
requestOptions: RequestOptions,
): HttpResponse {
val call = getClient(requestOptions).newCall(request.toRequest())
val call = newCall(request, requestOptions)

return try {
call.execute().toResponse()
Expand All @@ -81,18 +54,18 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val

request.body?.run { future.whenComplete { _, _ -> close() } }

val call = getClient(requestOptions).newCall(request.toRequest())
call.enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) {
future.complete(response.toResponse())
}
newCall(request, requestOptions)
.enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) {
future.complete(response.toResponse())
}

override fun onFailure(call: Call, e: IOException) {
future.completeExceptionally(TerminalIoException("Request failed", e))
override fun onFailure(call: Call, e: IOException) {
future.completeExceptionally(TerminalIoException("Request failed", e))
}
}
}
)
)

return future
}
Expand All @@ -103,10 +76,37 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
okHttpClient.cache?.close()
}

private fun HttpRequest.toRequest(): Request {
private fun newCall(request: HttpRequest, requestOptions: RequestOptions): Call {
val clientBuilder = okHttpClient.newBuilder()

val logLevel =
when (System.getenv("TERMINAL_LOG")?.lowercase()) {
"info" -> HttpLoggingInterceptor.Level.BASIC
"debug" -> HttpLoggingInterceptor.Level.BODY
else -> null
}
if (logLevel != null) {
clientBuilder.addNetworkInterceptor(
HttpLoggingInterceptor().setLevel(logLevel).apply { redactHeader("Authorization") }
)
}

val timeout = requestOptions.timeout
if (timeout != null) {
clientBuilder
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
}

val client = clientBuilder.build()
return client.newCall(request.toRequest(client))
}

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 @@ -115,9 +115,33 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
headers.values(name).forEach { builder.header(name, it) }
}

if (
!headers.names().contains("X-Stainless-Read-Timeout") && client.readTimeoutMillis != 0
) {
builder.header(
"X-Stainless-Read-Timeout",
Duration.ofMillis(client.readTimeoutMillis.toLong()).seconds.toString()
)
}
if (!headers.names().contains("X-Stainless-Timeout") && client.callTimeoutMillis != 0) {
builder.header(
"X-Stainless-Timeout",
Duration.ofMillis(client.callTimeoutMillis.toLong()).seconds.toString()
)
}

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 @@ -48,9 +48,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -75,9 +75,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -103,9 +103,9 @@ internal constructor(
.thenApply { response ->
response
.use { deleteHandler.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 { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -77,9 +77,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -105,9 +105,9 @@ internal constructor(
.thenApply { response ->
response
.use { deleteHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -132,9 +132,9 @@ internal constructor(
.thenApply { response ->
response
.use { getHandler.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 @@ -48,9 +48,9 @@ internal constructor(
.thenApply { response ->
response
.use { createHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -75,9 +75,9 @@ internal constructor(
.thenApply { response ->
response
.use { listHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand All @@ -103,9 +103,9 @@ internal constructor(
.thenApply { response ->
response
.use { deleteHandler.handle(it) }
.apply {
.also {
if (requestOptions.responseValidation ?: clientOptions.responseValidation) {
validate()
it.validate()
}
}
}
Expand Down
Loading
Loading