Skip to content

Commit

Permalink
camera client builder improved
Browse files Browse the repository at this point in the history
  • Loading branch information
jveverka committed May 22, 2021
1 parent 758287f commit edcab7d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package one.microproject.rpi.camera.client;

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

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

public class CameraClientBuilder {

private static final Logger LOG = LoggerFactory.getLogger(CameraClientBuilder.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 CameraClientBuilder baseUrl(String baseURL) throws MalformedURLException {
this.baseURL = new URL(baseURL);
Expand All @@ -22,11 +33,46 @@ public CameraClientBuilder withCredentials(String clientId, String clientSecret)
return this;
}

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

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

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

public CameraClientBuilder setTimeout(Long timeout, TimeUnit timeUnit) {
this.timeout = timeout;
this.timeUnit = timeUnit;
return this;
}

public CameraClient build() {
if (clientId == null || clientSecret == null) {
throw new ClientException("Invalid client credentials !");
}
return new CameraClientImpl(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 CameraClientImpl(baseURL, clientId, clientSecret, client, mapper);
}

public static CameraClientBuilder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

public class CameraClientImpl implements CameraClient {

Expand All @@ -30,17 +29,6 @@ public class CameraClientImpl implements CameraClient {
private final OkHttpClient client;
private final ObjectMapper mapper;

public CameraClientImpl(URL baseURL, String clientId, String clientSecret) {
this.baseURL = baseURL;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.client = new OkHttpClient().newBuilder()
.connectTimeout(40, TimeUnit.SECONDS)
.callTimeout(40, TimeUnit.SECONDS)
.build();
this.mapper = new ObjectMapper();
}

public CameraClientImpl(URL baseURL, String clientId, String clientSecret, OkHttpClient client, ObjectMapper mapper) {
this.baseURL = baseURL;
this.clientId = clientId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.TimeUnit;

public class CodeExample {

Expand All @@ -20,14 +21,15 @@ public class CodeExample {
public static void main(String[] args) throws IOException {
CameraClient cameraClient = CameraClientBuilder.builder()
.baseUrl("http://192.168.44.61:8080")
.setTimeout(80L, TimeUnit.SECONDS)
.withCredentials("client-001", "ex4oo")
.build();
SystemInfo systemInfo = cameraClient.getSystemInfo();
LOG.info("systemInfo: {} {} {}", systemInfo.getId(), systemInfo.getName(), systemInfo.getVersion());
//InputStream is = cameraClient.captureImage();
//InputStream is = cameraClient.captureImage(1);
InputStream is = cameraClient.captureImage(1, ImageFormat.JPEG);
File file = new File("/home/juraj/Downloads/camera-test.jpg");
InputStream is = cameraClient.captureImage(1, ImageFormat.PNG);
File file = new File("/home/juraj/Downloads/camera-test.png");
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

Expand Down

0 comments on commit edcab7d

Please sign in to comment.