Skip to content

Commit ca43f5e

Browse files
committed
feat: add Builder pattern for DefaultClient with deprecated legacy constructor
1 parent 3a031f1 commit ca43f5e

File tree

7 files changed

+626
-76
lines changed

7 files changed

+626
-76
lines changed

EXAMPLES.md

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,10 +3201,12 @@ The Auth0 class can be configured with a `NetworkingClient`, which will be used
32013201
### Timeout configuration
32023202

32033203
```kotlin
3204-
val netClient = DefaultClient(
3205-
connectTimeout = 30,
3206-
readTimeout = 30
3207-
)
3204+
val netClient = DefaultClient.Builder()
3205+
.connectTimeout(30)
3206+
.readTimeout(30)
3207+
.writeTimeout(30)
3208+
.callTimeout(120)
3209+
.build()
32083210

32093211
val account = Auth0.getInstance("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}")
32103212
account.networkingClient = netClient
@@ -3214,43 +3216,78 @@ account.networkingClient = netClient
32143216
<summary>Using Java</summary>
32153217

32163218
```java
3217-
DefaultClient netClient = new DefaultClient(30, 30);
3219+
DefaultClient netClient = new DefaultClient.Builder()
3220+
.connectTimeout(30)
3221+
.readTimeout(30)
3222+
.writeTimeout(30)
3223+
.callTimeout(120)
3224+
.build();
32183225
Auth0 account = Auth0.getInstance("client id", "domain");
32193226
account.setNetworkingClient(netClient);
32203227
```
32213228
</details>
32223229

3223-
### Logging configuration
3230+
<details>
3231+
<summary>Legacy constructor (still supported)</summary>
32243232

32253233
```kotlin
32263234
val netClient = DefaultClient(
3227-
enableLogging = true
3235+
connectTimeout = 30,
3236+
readTimeout = 30
32283237
)
3238+
```
3239+
</details>
3240+
3241+
### Logging configuration
3242+
3243+
```kotlin
3244+
val netClient = DefaultClient.Builder()
3245+
.enableLogging(true)
3246+
.build()
32293247

32303248
val account = Auth0.getInstance("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}")
32313249
account.networkingClient = netClient
32323250
```
32333251

3252+
You can also customize the log level and provide a custom logger:
3253+
3254+
```kotlin
3255+
val netClient = DefaultClient.Builder()
3256+
.enableLogging(true)
3257+
.logLevel(HttpLoggingInterceptor.Level.HEADERS) // NONE, BASIC, HEADERS, or BODY (default)
3258+
.logger(HttpLoggingInterceptor.Logger { message -> Log.d("Auth0Http", message) })
3259+
.build()
3260+
```
3261+
32343262
<details>
32353263
<summary>Using Java</summary>
32363264

32373265
```java
3238-
import java.util.HashMap;
3239-
3240-
DefaultClient netClient = new DefaultClient(
3241-
10, 10, new HashMap<>() ,true
3242-
);
3266+
DefaultClient netClient = new DefaultClient.Builder()
3267+
.enableLogging(true)
3268+
.logLevel(HttpLoggingInterceptor.Level.HEADERS)
3269+
.build();
32433270
Auth0 account = Auth0.getInstance("client id", "domain");
32443271
account.setNetworkingClient(netClient);
32453272
```
32463273
</details>
32473274

3248-
### Set additional headers for all requests
3275+
<details>
3276+
<summary>Legacy constructor (still supported)</summary>
32493277

32503278
```kotlin
32513279
val netClient = DefaultClient(
3252-
defaultHeaders = mapOf("{HEADER-NAME}" to "{HEADER-VALUE}")
3280+
enableLogging = true
32533281
)
3282+
```
3283+
</details>
3284+
3285+
### Set additional headers for all requests
3286+
3287+
```kotlin
3288+
val netClient = DefaultClient.Builder()
3289+
.defaultHeaders(mapOf("{HEADER-NAME}" to "{HEADER-VALUE}"))
3290+
.build()
32543291

32553292
val account = Auth0.getInstance("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}")
32563293
account.networkingClient = netClient
@@ -3263,14 +3300,45 @@ account.networkingClient = netClient
32633300
Map<String, String> defaultHeaders = new HashMap<>();
32643301
defaultHeaders.put("{HEADER-NAME}", "{HEADER-VALUE}");
32653302

3266-
DefaultClient netClient = new DefaultClient(
3267-
10,10 , defaultHeaders
3268-
);
3303+
DefaultClient netClient = new DefaultClient.Builder()
3304+
.defaultHeaders(defaultHeaders)
3305+
.build();
32693306
Auth0 account = Auth0.getInstance("client id", "domain");
32703307
account.setNetworkingClient(netClient);
32713308
```
32723309
</details>
32733310

3311+
<details>
3312+
<summary>Legacy constructor (still supported)</summary>
3313+
3314+
```kotlin
3315+
val netClient = DefaultClient(
3316+
defaultHeaders = mapOf("{HEADER-NAME}" to "{HEADER-VALUE}")
3317+
)
3318+
```
3319+
</details>
3320+
3321+
### Custom interceptors
3322+
3323+
You can add custom OkHttp interceptors to the `DefaultClient` for use cases such as auth token injection, analytics, or certificate pinning:
3324+
3325+
```kotlin
3326+
val netClient = DefaultClient.Builder()
3327+
.addInterceptor(Interceptor { chain ->
3328+
val request = chain.request().newBuilder()
3329+
.addHeader("X-Request-Id", UUID.randomUUID().toString())
3330+
.build()
3331+
chain.proceed(request)
3332+
})
3333+
.addInterceptor(myAnalyticsInterceptor)
3334+
.build()
3335+
3336+
val account = Auth0.getInstance("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}")
3337+
account.networkingClient = netClient
3338+
```
3339+
3340+
Interceptors are invoked in the order they were added, after the built-in retry interceptor and before the logging interceptor.
3341+
32743342
### Advanced configuration
32753343

32763344
For more advanced configuration of the networking client, you can provide a custom implementation of `NetworkingClient`. This may be useful when you wish to reuse your own networking client, configure a proxy, etc.

V4_MIGRATION_GUIDE.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ implementation 'com.google.code.gson:gson:2.8.9' // your preferred version
9696

9797
> **Note:** Pinning or excluding is not recommended long-term, as the SDK has been tested and validated against Gson 2.11.0.
9898
99+
### DefaultClient.Builder
100+
101+
v4 introduces a `DefaultClient.Builder` for configuring the HTTP client. This replaces the constructor-based approach with a more flexible builder pattern that supports additional options such as write/call timeouts, custom interceptors, and custom loggers.
102+
103+
**v3 (constructor-based — deprecated):**
104+
105+
```kotlin
106+
// ⚠️ Deprecated: still compiles but shows a warning
107+
val client = DefaultClient(
108+
connectTimeout = 30,
109+
readTimeout = 30,
110+
enableLogging = true
111+
)
112+
```
113+
114+
**v4 (builder pattern — recommended):**
115+
116+
```kotlin
117+
val client = DefaultClient.Builder()
118+
.connectTimeout(30)
119+
.readTimeout(30)
120+
.writeTimeout(30)
121+
.callTimeout(120)
122+
.enableLogging(true)
123+
.logLevel(HttpLoggingInterceptor.Level.HEADERS)
124+
.addInterceptor(myCustomInterceptor)
125+
.build()
126+
```
127+
128+
The legacy constructor is deprecated but **not removed** — existing code will continue to compile and run. Your IDE will show a deprecation warning with a suggested `ReplaceWith` quick-fix to migrate to the Builder.
129+
99130
## Getting Help
100131

101132
If you encounter issues during migration:

0 commit comments

Comments
 (0)