77import com .pipedream .api .core .Environment ;
88import com .pipedream .api .core .OAuthTokenSupplier ;
99import com .pipedream .api .resources .oauthtokens .OauthTokensClient ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
1012import java .util .Optional ;
1113import okhttp3 .OkHttpClient ;
1214
13- public class AsyncBaseClientBuilder < T extends AsyncBaseClientBuilder < T >> {
15+ public class AsyncBaseClientBuilder {
1416 private Optional <Integer > timeout = Optional .empty ();
1517
1618 private Optional <Integer > maxRetries = Optional .empty ();
1719
20+ private final Map <String , String > customHeaders = new HashMap <>();
21+
1822 private String clientId = System .getenv ("PIPEDREAM_CLIENT_ID" );
1923
2024 private String clientSecret = System .getenv ("PIPEDREAM_CLIENT_SECRET" );
@@ -25,72 +29,84 @@ public class AsyncBaseClientBuilder<T extends AsyncBaseClientBuilder<T>> {
2529
2630 private OkHttpClient httpClient ;
2731
32+ private String projectId ;
33+
2834 /**
2935 * Sets clientId.
3036 * Defaults to the PIPEDREAM_CLIENT_ID environment variable.
3137 */
32- @ SuppressWarnings ("unchecked" )
33- public T clientId (String clientId ) {
38+ public AsyncBaseClientBuilder clientId (String clientId ) {
3439 this .clientId = clientId ;
35- return ( T ) this ;
40+ return this ;
3641 }
3742
3843 /**
3944 * Sets clientSecret.
4045 * Defaults to the PIPEDREAM_CLIENT_SECRET environment variable.
4146 */
42- @ SuppressWarnings ("unchecked" )
43- public T clientSecret (String clientSecret ) {
47+ public AsyncBaseClientBuilder clientSecret (String clientSecret ) {
4448 this .clientSecret = clientSecret ;
45- return ( T ) this ;
49+ return this ;
4650 }
4751
4852 /**
4953 * Sets projectEnvironment
5054 */
51- @ SuppressWarnings ("unchecked" )
52- public T projectEnvironment (String projectEnvironment ) {
55+ public AsyncBaseClientBuilder projectEnvironment (String projectEnvironment ) {
5356 this .projectEnvironment = projectEnvironment ;
54- return ( T ) this ;
57+ return this ;
5558 }
5659
57- @ SuppressWarnings ("unchecked" )
58- public T environment (Environment environment ) {
60+ public AsyncBaseClientBuilder environment (Environment environment ) {
5961 this .environment = environment ;
60- return ( T ) this ;
62+ return this ;
6163 }
6264
63- @ SuppressWarnings ("unchecked" )
64- public T url (String url ) {
65+ public AsyncBaseClientBuilder url (String url ) {
6566 this .environment = Environment .custom (url );
66- return ( T ) this ;
67+ return this ;
6768 }
6869
6970 /**
7071 * Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
7172 */
72- @ SuppressWarnings ("unchecked" )
73- public T timeout (int timeout ) {
73+ public AsyncBaseClientBuilder timeout (int timeout ) {
7474 this .timeout = Optional .of (timeout );
75- return ( T ) this ;
75+ return this ;
7676 }
7777
7878 /**
7979 * Sets the maximum number of retries for the client. Defaults to 2 retries.
8080 */
81- @ SuppressWarnings ("unchecked" )
82- public T maxRetries (int maxRetries ) {
81+ public AsyncBaseClientBuilder maxRetries (int maxRetries ) {
8382 this .maxRetries = Optional .of (maxRetries );
84- return ( T ) this ;
83+ return this ;
8584 }
8685
8786 /**
8887 * Sets the underlying OkHttp client
8988 */
90- @ SuppressWarnings ("unchecked" )
91- public T httpClient (OkHttpClient httpClient ) {
89+ public AsyncBaseClientBuilder httpClient (OkHttpClient httpClient ) {
9290 this .httpClient = httpClient ;
93- return (T ) this ;
91+ return this ;
92+ }
93+
94+ /**
95+ * Add a custom header to be sent with all requests.
96+ * For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead.
97+ *
98+ * @param name The header name
99+ * @param value The header value
100+ * @return This builder for method chaining
101+ */
102+ public AsyncBaseClientBuilder addHeader (String name , String value ) {
103+ this .customHeaders .put (name , value );
104+ return this ;
105+ }
106+
107+ public AsyncBaseClientBuilder projectId (String projectId ) {
108+ this .projectId = projectId ;
109+ return this ;
94110 }
95111
96112 protected ClientOptions buildClientOptions () {
@@ -102,6 +118,9 @@ protected ClientOptions buildClientOptions() {
102118 setHttpClient (builder );
103119 setTimeouts (builder );
104120 setRetries (builder );
121+ for (Map .Entry <String , String > header : this .customHeaders .entrySet ()) {
122+ builder .addHeader (header .getKey (), header .getValue ());
123+ }
105124 setAdditional (builder );
106125 return builder .build ();
107126 }
@@ -124,7 +143,7 @@ protected void setEnvironment(ClientOptions.Builder builder) {
124143 *
125144 * Example:
126145 * <pre>{@code
127- * @ Override
146+ * @ Override
128147 * protected void setAuthentication(ClientOptions.Builder builder) {
129148 * super.setAuthentication(builder); // Keep existing auth
130149 * builder.addHeader("X-API-Key", this.apiKey);
@@ -133,8 +152,12 @@ protected void setEnvironment(ClientOptions.Builder builder) {
133152 */
134153 protected void setAuthentication (ClientOptions .Builder builder ) {
135154 if (this .clientId != null && this .clientSecret != null ) {
136- OauthTokensClient authClient = new OauthTokensClient (
137- ClientOptions .builder ().environment (this .environment ).build ());
155+ ClientOptions .Builder authClientOptionsBuilder =
156+ ClientOptions .builder ().environment (this .environment );
157+ if (this .projectId != null ) {
158+ authClientOptionsBuilder .projectId (this .projectId );
159+ }
160+ OauthTokensClient authClient = new OauthTokensClient (authClientOptionsBuilder .build ());
138161 OAuthTokenSupplier oAuthTokenSupplier =
139162 new OAuthTokenSupplier (this .clientId , this .clientSecret , authClient );
140163 builder .addHeader ("Authorization" , oAuthTokenSupplier );
@@ -149,7 +172,7 @@ protected void setAuthentication(ClientOptions.Builder builder) {
149172 *
150173 * Example:
151174 * <pre>{@code
152- * @ Override
175+ * @ Override
153176 * protected void setCustomHeaders(ClientOptions.Builder builder) {
154177 * super.setCustomHeaders(builder); // Keep existing headers
155178 * builder.addHeader("X-Trace-ID", generateTraceId());
@@ -168,7 +191,11 @@ protected void setCustomHeaders(ClientOptions.Builder builder) {
168191 *
169192 * @param builder The ClientOptions.Builder to configure
170193 */
171- protected void setVariables (ClientOptions .Builder builder ) {}
194+ protected void setVariables (ClientOptions .Builder builder ) {
195+ if (this .projectId != null ) {
196+ builder .projectId (this .projectId );
197+ }
198+ }
172199
173200 /**
174201 * Sets the request timeout configuration.
@@ -215,9 +242,9 @@ protected void setHttpClient(ClientOptions.Builder builder) {
215242 *
216243 * Example:
217244 * <pre>{@code
218- * @ Override
245+ * @ Override
219246 * protected void setAdditional(ClientOptions.Builder builder) {
220- * builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
247+ * builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
221248 * builder.addHeader("X-Client-Version", "1.0.0");
222249 * }
223250 * }</pre>
@@ -231,7 +258,7 @@ protected void setAdditional(ClientOptions.Builder builder) {}
231258 *
232259 * Example:
233260 * <pre>{@code
234- * @ Override
261+ * @ Override
235262 * protected void validateConfiguration() {
236263 * super.validateConfiguration(); // Run parent validations
237264 * if (tenantId == null || tenantId.isEmpty()) {
0 commit comments