Skip to content

Commit 4ac10b3

Browse files
feat(client): support setting base URL via env var (#397)
1 parent 386db77 commit 4ac10b3

4 files changed

Lines changed: 17 additions & 21 deletions

File tree

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import com.withorb.api.client.okhttp.OrbOkHttpClient;
4646
import com.withorb.api.models.Customer;
4747
import com.withorb.api.models.CustomerCreateParams;
4848

49-
// Configures using the `ORB_API_KEY` and `ORB_WEBHOOK_SECRET` environment variables
49+
// Configures using the `ORB_API_KEY`, `ORB_WEBHOOK_SECRET` and `ORB_BASE_URL` environment variables
5050
OrbClient client = OrbOkHttpClient.fromEnv();
5151

5252
CustomerCreateParams params = CustomerCreateParams.builder()
@@ -64,7 +64,7 @@ Configure the client using environment variables:
6464
import com.withorb.api.client.OrbClient;
6565
import com.withorb.api.client.okhttp.OrbOkHttpClient;
6666

67-
// Configures using the `ORB_API_KEY` and `ORB_WEBHOOK_SECRET` environment variables
67+
// Configures using the `ORB_API_KEY`, `ORB_WEBHOOK_SECRET` and `ORB_BASE_URL` environment variables
6868
OrbClient client = OrbOkHttpClient.fromEnv();
6969
```
7070

@@ -86,18 +86,19 @@ import com.withorb.api.client.OrbClient;
8686
import com.withorb.api.client.okhttp.OrbOkHttpClient;
8787

8888
OrbClient client = OrbOkHttpClient.builder()
89-
// Configures using the `ORB_API_KEY` and `ORB_WEBHOOK_SECRET` environment variables
89+
// Configures using the `ORB_API_KEY`, `ORB_WEBHOOK_SECRET` and `ORB_BASE_URL` environment variables
9090
.fromEnv()
9191
.apiKey("My API Key")
9292
.build();
9393
```
9494

9595
See this table for the available options:
9696

97-
| Setter | Environment variable | Required | Default value |
98-
| --------------- | -------------------- | -------- | ------------- |
99-
| `apiKey` | `ORB_API_KEY` | true | - |
100-
| `webhookSecret` | `ORB_WEBHOOK_SECRET` | false | - |
97+
| Setter | Environment variable | Required | Default value |
98+
| --------------- | -------------------- | -------- | ------------------------------ |
99+
| `apiKey` | `ORB_API_KEY` | true | - |
100+
| `webhookSecret` | `ORB_WEBHOOK_SECRET` | false | - |
101+
| `baseUrl` | `ORB_BASE_URL` | true | `"https://api.withorb.com/v1"` |
101102

102103
> [!TIP]
103104
> Don't create more than one client in the same application. Each client has a connection pool and
@@ -128,7 +129,7 @@ import com.withorb.api.models.Customer;
128129
import com.withorb.api.models.CustomerCreateParams;
129130
import java.util.concurrent.CompletableFuture;
130131

131-
// Configures using the `ORB_API_KEY` and `ORB_WEBHOOK_SECRET` environment variables
132+
// Configures using the `ORB_API_KEY`, `ORB_WEBHOOK_SECRET` and `ORB_BASE_URL` environment variables
132133
OrbClient client = OrbOkHttpClient.fromEnv();
133134

134135
CustomerCreateParams params = CustomerCreateParams.builder()
@@ -147,7 +148,7 @@ import com.withorb.api.models.Customer;
147148
import com.withorb.api.models.CustomerCreateParams;
148149
import java.util.concurrent.CompletableFuture;
149150

150-
// Configures using the `ORB_API_KEY` and `ORB_WEBHOOK_SECRET` environment variables
151+
// Configures using the `ORB_API_KEY`, `ORB_WEBHOOK_SECRET` and `ORB_BASE_URL` environment variables
151152
OrbClientAsync client = OrbOkHttpClientAsync.fromEnv();
152153

153154
CustomerCreateParams params = CustomerCreateParams.builder()

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClient.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ class OrbOkHttpClient private constructor() {
2929
class Builder internal constructor() {
3030

3131
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
32-
private var baseUrl: String = ClientOptions.PRODUCTION_URL
3332
private var timeout: Timeout = Timeout.default()
3433
private var proxy: Proxy? = null
3534

36-
fun baseUrl(baseUrl: String) = apply {
37-
clientOptions.baseUrl(baseUrl)
38-
this.baseUrl = baseUrl
39-
}
35+
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
4036

4137
/**
4238
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -177,7 +173,7 @@ class OrbOkHttpClient private constructor() {
177173
clientOptions
178174
.httpClient(
179175
OkHttpClient.builder()
180-
.baseUrl(baseUrl)
176+
.baseUrl(clientOptions.baseUrl())
181177
.timeout(timeout)
182178
.proxy(proxy)
183179
.build()

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClientAsync.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ class OrbOkHttpClientAsync private constructor() {
2929
class Builder internal constructor() {
3030

3131
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
32-
private var baseUrl: String = ClientOptions.PRODUCTION_URL
3332
private var timeout: Timeout = Timeout.default()
3433
private var proxy: Proxy? = null
3534

36-
fun baseUrl(baseUrl: String) = apply {
37-
clientOptions.baseUrl(baseUrl)
38-
this.baseUrl = baseUrl
39-
}
35+
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
4036

4137
/**
4238
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -177,7 +173,7 @@ class OrbOkHttpClientAsync private constructor() {
177173
clientOptions
178174
.httpClient(
179175
OkHttpClient.builder()
180-
.baseUrl(baseUrl)
176+
.baseUrl(clientOptions.baseUrl())
181177
.timeout(timeout)
182178
.proxy(proxy)
183179
.build()

orb-java-core/src/main/kotlin/com/withorb/api/core/ClientOptions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ private constructor(
197197

198198
fun removeAllQueryParams(keys: Set<String>) = apply { queryParams.removeAll(keys) }
199199

200+
fun baseUrl(): String = baseUrl
201+
200202
fun fromEnv() = apply {
203+
System.getenv("ORB_BASE_URL")?.let { baseUrl(it) }
201204
System.getenv("ORB_API_KEY")?.let { apiKey(it) }
202205
System.getenv("ORB_WEBHOOK_SECRET")?.let { webhookSecret(it) }
203206
}

0 commit comments

Comments
 (0)