Skip to content

Commit 177d9c6

Browse files
authored
Add builder extension docs to Java custom code (#164)
1 parent b0ee83c commit 177d9c6

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

fern/products/sdks/overview/java/custom-code.mdx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,78 @@ description: Augment your Java SDK with custom utilities
104104
</Steps>
105105

106106

107+
## Adding custom client configuration
108+
109+
When you need to intercept and transform client configuration before the SDK client is created, you can extend the generated builder classes. This allows you to add custom code and logic during the client initialization process.
110+
111+
Common use cases include:
112+
- **Dynamic URL construction**: Replace placeholders with runtime values (e.g., `https://api.${DEV_NAMESPACE}.example.com`)
113+
- **Custom authentication**: Implement complex auth flows beyond basic token authentication
114+
- **Request transformation**: Add custom headers or modify requests globally
115+
116+
<Steps>
117+
118+
### Create a custom client that extends the base client
119+
120+
This client will serve as the entry point for your customized SDK.
121+
122+
```java title="src/main/java/com/example/MyClient.java"
123+
package com.example;
124+
125+
import com.example.client.BaseClient;
126+
import com.example.client.ClientOptions;
127+
128+
public class MyClient extends BaseClient {
129+
public MyClient(ClientOptions clientOptions) {
130+
super(clientOptions);
131+
}
132+
133+
public static MyClientBuilder builder() {
134+
return new MyClientBuilder();
135+
}
136+
}
137+
```
138+
139+
### Create a custom builder with your transformation logic
140+
141+
Override the `buildClientOptions()` method to intercept and modify the configuration before the client is created.
142+
143+
```java title="src/main/java/com/example/MyClientBuilder.java" {10}
144+
package com.example;
145+
146+
import com.example.client.BaseClient.BaseClientBuilder;
147+
import com.example.client.ClientOptions;
148+
import com.example.environment.Environment;
149+
150+
public class MyClientBuilder extends BaseClientBuilder {
151+
@Override
152+
protected ClientOptions buildClientOptions() {
153+
ClientOptions base = super.buildClientOptions();
154+
155+
// Transform configuration as needed
156+
String transformedUrl = transformEnvironmentVariables(
157+
base.environment().getUrl()
158+
);
159+
160+
return ClientOptions.builder()
161+
.from(base)
162+
.environment(Environment.custom(transformedUrl))
163+
.build();
164+
}
165+
}
166+
```
167+
168+
### Update `.fernignore`
169+
170+
Add the `MyClient.java` and `MyClientBuilder.java` to `.fernignore`.
171+
172+
```diff title=".fernignore"
173+
+ src/main/java/com/example/MyClient.java
174+
+ src/main/java/com/example/MyClientBuilder.java
175+
```
176+
177+
</Steps>
178+
107179
## Adding custom dependencies
108180

109181
<Markdown src="/snippets/pro-callout.mdx"/>

0 commit comments

Comments
 (0)