Skip to content

Commit

Permalink
power controller builder imroved
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed May 22, 2021
1 parent edcab7d commit ef97674
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package one.microproject.rpi.powercontroller;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import one.microproject.rpi.powercontroller.client.PowerControllerClientImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class PowerControllerClientBuilder {

private static final Logger LOG = LoggerFactory.getLogger(PowerControllerClientBuilder.class);

private URL baseURL;
private String clientId;
private String clientSecret;
private Long timeout = 3L;
private TimeUnit timeUnit = TimeUnit.SECONDS;
private OkHttpClient client;
private ObjectMapper mapper;

public PowerControllerClientBuilder baseUrl(String baseURL) throws MalformedURLException {
this.baseURL = new URL(baseURL);
Expand All @@ -22,11 +33,40 @@ public PowerControllerClientBuilder withCredentials(String clientId, String clie
return this;
}

public PowerControllerClientBuilder setTimeouts(Long timeout, TimeUnit timeUnit) {
this.timeout = timeout;
this.timeUnit = timeUnit;
return this;
}

public PowerControllerClientBuilder setHttpClient(OkHttpClient client) {
this.client = client;
return this;
}

public PowerControllerClientBuilder setObjectMapper(ObjectMapper mapper) {
this.mapper = mapper;
return this;
}

public PowerControllerClient build() {
if (clientId == null || clientSecret == null) {
throw new ClientException("Invalid client credentials !");
}
return new PowerControllerClientImpl(baseURL, clientId, clientSecret);
if (client == null) {
LOG.info("default http client: timeouts={} {}", timeout, timeUnit);
this.client = new OkHttpClient().newBuilder()
.connectTimeout(timeout, timeUnit)
.callTimeout(timeout, timeUnit)
.readTimeout(timeout, timeUnit)
.writeTimeout(timeout, timeUnit)
.build();
}
if (mapper == null) {
LOG.info("default mapper");
this.mapper = new ObjectMapper();
}
return new PowerControllerClientImpl(baseURL, clientId, clientSecret, client, mapper);
}

public PowerControllerReadClient buildReadClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public class PowerControllerClientImpl implements PowerControllerClient {
private final String clientId;
private final String clientSecret;

public PowerControllerClientImpl(URL baseURL, String clientId, String clientSecret) {
this.client = new OkHttpClient();
this.mapper = new ObjectMapper();
this.baseURL = baseURL;
this.clientId = clientId;
this.clientSecret = clientSecret;
}

public PowerControllerClientImpl(URL baseURL, String clientId, String clientSecret, OkHttpClient client, ObjectMapper mapper) {
this.client = client;
this.mapper = mapper;
Expand Down

0 comments on commit ef97674

Please sign in to comment.