Skip to content

Commit

Permalink
feat: customization options
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyewch committed Feb 6, 2025
1 parent 1182afe commit 8ea9227
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.google.protobuf.util.JsonFormat;
import io.github.ngyewch.twirp.*;
import java.io.IOException;
import java.time.Duration;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
Expand All @@ -22,11 +24,29 @@ public abstract class AbstractService {
private final String contentType;

protected AbstractService(String baseUri, String contentType) {
this(baseUri, contentType, (CloseableHttpClient) null);
}

protected AbstractService(String baseUri, String contentType, Duration timeout) {
this(baseUri, contentType, newHttpClient(timeout));
}

protected AbstractService(String baseUri, String contentType, CloseableHttpClient httpClient) {
super();

this.baseUri = baseUri;
this.httpClient = HttpClients.createDefault();
this.contentType = contentType;
this.httpClient = (httpClient != null) ? httpClient : HttpClients.createDefault();
}

private static CloseableHttpClient newHttpClient(Duration timeout) {
final RequestConfig requestConfig =
RequestConfig.custom()
.setConnectTimeout((int) timeout.toMillis())
.setConnectionRequestTimeout((int) timeout.toMillis())
.setSocketTimeout((int) timeout.toMillis())
.build();
return HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
}

protected void doRequest(String path, Message input, Message.Builder outputBuilder) {
Expand Down

0 comments on commit 8ea9227

Please sign in to comment.