-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): Configuration overrides (#189)
* refactor(java): Thread Configuration through request builders * refactor(java): Thread Configuration through requests * feat(java): Introduce ConfigurationOverride class * test(java): Add tests on ConfigurationOverride * refactor(java): Consistent code in Configuration.override(...)
- Loading branch information
1 parent
7abf4c9
commit bf4a7c6
Showing
6 changed files
with
348 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
config/clients/java/template/config-ConfigurationOverride.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
{{>licenseInfo}} | ||
package {{invokerPackage}}; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpConnectTimeoutException; | ||
import java.net.http.HttpRequest; | ||
import java.time.Duration; | ||
|
||
/** | ||
* Configuration overrides for an api client. Values are initialized to null, and any values unset are intended to fall | ||
* through to the values of a {@link Configuration}. | ||
* <p> | ||
* More details on intended usage of this class can be found in the documentation of the {@link Configuration#override(ConfigurationOverride)} method. | ||
*/ | ||
public class ConfigurationOverride implements BaseConfiguration { | ||
private String apiUrl; | ||
private String userAgent; | ||
private Duration readTimeout; | ||
private Duration connectTimeout; | ||
public ConfigurationOverride() { | ||
this.apiUrl = null; | ||
this.userAgent = null; | ||
this.readTimeout = null; | ||
this.connectTimeout = null; | ||
} | ||
|
||
/** | ||
* Set the API URL for the http client. | ||
* | ||
* @param apiUrl The URL. | ||
* @return This object. | ||
*/ | ||
public ConfigurationOverride apiUrl(String apiUrl) { | ||
this.apiUrl = apiUrl; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the API URL that was set. | ||
* | ||
* @return The url. | ||
*/ | ||
@Override | ||
public String getApiUrl() { | ||
return apiUrl; | ||
} | ||
|
||
/** | ||
* Set the user agent. | ||
* | ||
* @param userAgent The user agent. | ||
* @return This object. | ||
*/ | ||
public ConfigurationOverride userAgent(String userAgent) { | ||
this.userAgent = userAgent; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the user agent. | ||
* | ||
* @return The user agent. | ||
*/ | ||
@Override | ||
public String getUserAgent() { | ||
return userAgent; | ||
} | ||
|
||
/** | ||
* Set the read timeout for the http client. | ||
* | ||
* <p>This is the value used by default for each request, though it can be | ||
* overridden on a per-request basis with a request interceptor.</p> | ||
* | ||
* @param readTimeout The read timeout used by default by the http client. | ||
* Setting this value to null resets the timeout to an | ||
* effectively infinite value. | ||
* @return This object. | ||
*/ | ||
public ConfigurationOverride readTimeout(Duration readTimeout) { | ||
this.readTimeout = readTimeout; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the read timeout that was set. | ||
* | ||
* @return The read timeout, or null if no timeout was set. Null represents | ||
* an infinite wait time. | ||
*/ | ||
@Override | ||
public Duration getReadTimeout() { | ||
return readTimeout; | ||
} | ||
|
||
/** | ||
* Sets the connect timeout (in milliseconds) for the http client. | ||
* | ||
* <p> In the case where a new connection needs to be established, if | ||
* the connection cannot be established within the given {@code | ||
* duration}, then {@link HttpClient#send(HttpRequest, BodyHandler) | ||
* HttpClient::send} throws an {@link HttpConnectTimeoutException}, or | ||
* {@link HttpClient#sendAsync(HttpRequest, BodyHandler) | ||
* HttpClient::sendAsync} completes exceptionally with an | ||
* {@code HttpConnectTimeoutException}. If a new connection does not | ||
* need to be established, for example if a connection can be reused | ||
* from a previous request, then this timeout duration has no effect. | ||
* | ||
* @param connectTimeout connection timeout in milliseconds | ||
* @return This object. | ||
*/ | ||
public ConfigurationOverride connectTimeout(Duration connectTimeout) { | ||
this.connectTimeout = connectTimeout; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get connection timeout (in milliseconds). | ||
* | ||
* @return Timeout in milliseconds | ||
*/ | ||
@Override | ||
public Duration getConnectTimeout() { | ||
return connectTimeout; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.