Commit ee2e0a05 authored by Greg Messner's avatar Greg Messner
Browse files

Added setRequestTimeout() (#532)

parent 626ed2a5
......@@ -15,6 +15,7 @@ GitLab4J™ API (gitlab4j-api) provides a full featured and easy to consume
* [Javadocs](#javadocs)<br/>
* [Project Set Up](#project-set-up)<br/>
* [Usage Examples](#usage-examples)<br/>
* [Setting Request Timeouts](#setting-reuest-timeouts)<br/>
* [Connecting Through a Proxy Server](#connecting-through-a-proxy-server)<br/>
* [GitLab API V3 and V4 Support](#gitLab-api-v3-and-v4-support)<br/>
* [Logging of API Requests and Responses](#logging-of-api-requests-and-responses)<br/>
......@@ -103,6 +104,16 @@ gitLabApi.sudo("johndoe")
gitLabApi.unsudo();
```
---
### **Setting Request Timeouts**
As of GitLab4J-API 4.14.21 support has been added for setting the conect and read timeouts for the API client:
```java
GitLabApi gitLabApi = new GitLabApi("http://your.gitlab.com", "YOUR_PERSONAL_ACCESS_TOKEN", proxyConfig);
// Set the connect timeout to 1 second and the read timeout to 5 seconds
gitLabApi.setRequestTimeout(1000, 5000);
```
---
### **Connecting Through a Proxy Server**
As of GitLab4J-API 4.8.2 support has been added for connecting to the GitLab server using an HTTP proxy server:
......
......@@ -408,6 +408,17 @@ public class GitLabApi implements AutoCloseable {
this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, personalAccessToken, secretToken, clientConfigProperties);
}
/**
* Constructs a GitLabApi instance set up to interact with the GitLab server using GitLab API version 4.
*
* @param hostUrl the URL of the GitLab server
* @param personalAccessToken the private token to use for access to the API
* @param clientConfigProperties Map instance with additional properties for the Jersey client connection
*/
public GitLabApi(String hostUrl, String personalAccessToken, Map<String, Object> clientConfigProperties) {
this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, personalAccessToken, null, clientConfigProperties);
}
/**
* Constructs a GitLabApi instance set up to interact with the GitLab server specified by GitLab API version.
*
......@@ -447,6 +458,38 @@ public class GitLabApi implements AutoCloseable {
return (gitLabApi);
}
/**
* Close the underlying {@link javax.ws.rs.client.Client} and its associated resources.
*/
@Override
public void close() {
if (apiClient != null) {
apiClient.close();
}
}
/**
* Sets the per request connect and read timeout.
*
* @param connectTimeout the per request connect timeout in milliseconds, can be null to use default
* @param readTimeout the per request read timeout in milliseconds, can be null to use default
*/
public void setRequestTimeout(Integer connectTimeout, Integer readTimeout) {
apiClient.setRequestTimeout(connectTimeout, readTimeout);
}
/**
* Fluent method that sets the per request connect and read timeout.
*
* @param connectTimeout the per request connect timeout in milliseconds, can be null to use default
* @param readTimeout the per request read timeout in milliseconds, can be null to use default
* @return this GitLabApi instance
*/
public GitLabApi withRequestTimeout(Integer connectTimeout, Integer readTimeout) {
apiClient.setRequestTimeout(connectTimeout, readTimeout);
return (this);
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance and Level.FINE as the level.
......@@ -482,17 +525,6 @@ public class GitLabApi implements AutoCloseable {
return (this);
}
/**
* Close the underlying {@link javax.ws.rs.client.Client} and its associated resources.
*/
@Override
public void close() {
if (apiClient != null) {
apiClient.close();
}
}
/**
* Enable the logging of the requests to and the responses from the GitLab server API
* using the GitLab4J shared Logger instance and Level.FINE as the level.
......
......@@ -66,6 +66,8 @@ public class GitLabApiClient implements AutoCloseable {
private SSLContext openSslContext;
private HostnameVerifier openHostnameVerifier;
private Integer sudoAsId;
private Integer connectTimeout;
private Integer readTimeout;
/**
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
......@@ -274,6 +276,17 @@ public class GitLabApiClient implements AutoCloseable {
}
}
/**
* Sets the per request connect and read timeout.
*
* @param connectTimeout the per request connect timeout in milliseconds, can be null to use default
* @param readTimeout the per request read timeout in milliseconds, can be null to use default
*/
void setRequestTimeout(Integer connectTimeout, Integer readTimeout) {
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}
/**
* Get the auth token being used by this client.
*
......@@ -791,6 +804,16 @@ public class GitLabApiClient implements AutoCloseable {
if (sudoAsId != null && sudoAsId.intValue() > 0)
builder = builder.header(SUDO_HEADER, sudoAsId);
// Set the per request connect timeout
if (connectTimeout != null) {
builder.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
}
// Set the per request read timeout
if (readTimeout != null) {
builder.property(ClientProperties.READ_TIMEOUT, readTimeout);
}
return (builder);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment