Skip to content

Commit 8f26ee5

Browse files
feat(client): allow configuring env via system properties
1 parent 589e149 commit 8f26ee5

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@ import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
5353
import org.onebusaway.models.currenttime.CurrentTimeRetrieveParams;
5454
import org.onebusaway.models.currenttime.CurrentTimeRetrieveResponse;
5555

56-
// Configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
56+
// Configures using the `onebusawaysdk.onebusawayApiKey` and `onebusawaysdk.baseUrl` system properties
57+
// Or configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
5758
OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.fromEnv();
5859

5960
CurrentTimeRetrieveResponse currentTime = client.currentTime().retrieve();
6061
```
6162

6263
## Client configuration
6364

64-
Configure the client using environment variables:
65+
Configure the client using system properties or environment variables:
6566

6667
```java
6768
import org.onebusaway.client.OnebusawaySdkClient;
6869
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
6970

70-
// Configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
71+
// Configures using the `onebusawaysdk.onebusawayApiKey` and `onebusawaysdk.baseUrl` system properties
72+
// Or configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
7173
OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.fromEnv();
7274
```
7375

@@ -89,18 +91,21 @@ import org.onebusaway.client.OnebusawaySdkClient;
8991
import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
9092

9193
OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.builder()
92-
// Configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
94+
// Configures using the `onebusawaysdk.onebusawayApiKey` and `onebusawaysdk.baseUrl` system properties
95+
Or configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
9396
.fromEnv()
9497
.apiKey("My API Key")
9598
.build();
9699
```
97100

98101
See this table for the available options:
99102

100-
| Setter | Environment variable | Required | Default value |
101-
| --------- | ------------------------- | -------- | ----------------------------------------- |
102-
| `apiKey` | `ONEBUSAWAY_API_KEY` | true | - |
103-
| `baseUrl` | `ONEBUSAWAY_SDK_BASE_URL` | true | `"https://api.pugetsound.onebusaway.org"` |
103+
| Setter | System property | Environment variable | Required | Default value |
104+
| --------- | -------------------------------- | ------------------------- | -------- | ----------------------------------------- |
105+
| `apiKey` | `onebusawaysdk.onebusawayApiKey` | `ONEBUSAWAY_API_KEY` | true | - |
106+
| `baseUrl` | `onebusawaysdk.baseUrl` | `ONEBUSAWAY_SDK_BASE_URL` | true | `"https://api.pugetsound.onebusaway.org"` |
107+
108+
System properties take precedence over environment variables.
104109

105110
> [!TIP]
106111
> Don't create more than one client in the same application. Each client has a connection pool and
@@ -146,7 +151,8 @@ import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClient;
146151
import org.onebusaway.models.currenttime.CurrentTimeRetrieveParams;
147152
import org.onebusaway.models.currenttime.CurrentTimeRetrieveResponse;
148153

149-
// Configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
154+
// Configures using the `onebusawaysdk.onebusawayApiKey` and `onebusawaysdk.baseUrl` system properties
155+
// Or configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
150156
OnebusawaySdkClient client = OnebusawaySdkOkHttpClient.fromEnv();
151157

152158
CompletableFuture<CurrentTimeRetrieveResponse> currentTime = client.async().currentTime().retrieve();
@@ -161,7 +167,8 @@ import org.onebusaway.client.okhttp.OnebusawaySdkOkHttpClientAsync;
161167
import org.onebusaway.models.currenttime.CurrentTimeRetrieveParams;
162168
import org.onebusaway.models.currenttime.CurrentTimeRetrieveResponse;
163169

164-
// Configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
170+
// Configures using the `onebusawaysdk.onebusawayApiKey` and `onebusawaysdk.baseUrl` system properties
171+
// Or configures using the `ONEBUSAWAY_API_KEY` and `ONEBUSAWAY_SDK_BASE_URL` environment variables
165172
OnebusawaySdkClientAsync client = OnebusawaySdkOkHttpClientAsync.fromEnv();
166173

167174
CompletableFuture<CurrentTimeRetrieveResponse> currentTime = client.currentTime().retrieve();

onebusaway-sdk-java-core/src/main/kotlin/org/onebusaway/core/ClientOptions.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,12 @@ private constructor(
220220
fun timeout(): Timeout = timeout
221221

222222
fun fromEnv() = apply {
223-
System.getenv("ONEBUSAWAY_SDK_BASE_URL")?.let { baseUrl(it) }
224-
System.getenv("ONEBUSAWAY_API_KEY")?.let { apiKey(it) }
223+
(System.getProperty("onebusawaysdk.baseUrl")
224+
?: System.getenv("ONEBUSAWAY_SDK_BASE_URL"))
225+
?.let { baseUrl(it) }
226+
(System.getProperty("onebusawaysdk.onebusawayApiKey")
227+
?: System.getenv("ONEBUSAWAY_API_KEY"))
228+
?.let { apiKey(it) }
225229
}
226230

227231
/**

0 commit comments

Comments
 (0)