Skip to content

Commit 02d79e1

Browse files
committed
Timeout config added
#39
1 parent 827c0c6 commit 02d79e1

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.visual_regression_tracker.sdk_java;
22

3-
import java.io.IOException;
4-
import java.util.Optional;
5-
63
import com.google.gson.Gson;
74
import io.visual_regression_tracker.sdk_java.request.BuildRequest;
85
import io.visual_regression_tracker.sdk_java.request.TestRunRequest;
@@ -15,12 +12,16 @@
1512
import okhttp3.RequestBody;
1613
import okhttp3.Response;
1714

15+
import java.io.IOException;
16+
import java.util.Optional;
17+
import java.util.concurrent.TimeUnit;
18+
1819
@Slf4j
1920
public class VisualRegressionTracker {
2021

21-
private static final String TRACKER_NOT_STARTED = "Visual Regression Tracker has not been started";
2222
protected static final String API_KEY_HEADER = "apiKey";
2323
protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
24+
private static final String TRACKER_NOT_STARTED = "Visual Regression Tracker has not been started";
2425
protected Gson gson;
2526
protected VisualRegressionTrackerConfig configuration;
2627
protected PathProvider paths;
@@ -35,22 +36,31 @@ public VisualRegressionTracker(VisualRegressionTrackerConfig trackerConfig) {
3536
gson = new Gson();
3637
}
3738

39+
public VisualRegressionTracker(VisualRegressionTrackerConfig trackerConfig, long timeoutInSeconds) {
40+
this(trackerConfig);
41+
client = new OkHttpClient.Builder()
42+
.connectTimeout(timeoutInSeconds, TimeUnit.SECONDS)
43+
.readTimeout(timeoutInSeconds, TimeUnit.SECONDS)
44+
.writeTimeout(timeoutInSeconds, TimeUnit.SECONDS)
45+
.build();
46+
}
47+
3848
public void start() throws IOException {
3949
String projectName = configuration.getProject();
4050
String branch = configuration.getBranchName();
4151

4252
BuildRequest newBuild = BuildRequest.builder()
43-
.branchName(branch)
44-
.project(projectName)
45-
.build();
53+
.branchName(branch)
54+
.project(projectName)
55+
.build();
4656

4757
RequestBody body = RequestBody.create(JSON, gson.toJson(newBuild));
4858

4959
Request request = new Request.Builder()
50-
.url(paths.getBuildPath())
51-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
52-
.post(body)
53-
.build();
60+
.url(paths.getBuildPath())
61+
.addHeader(API_KEY_HEADER, configuration.getApiKey())
62+
.post(body)
63+
.build();
5464

5565
log.info("Starting Visual Regression Tracker for project <{}> and branch <{}>", projectName, branch);
5666

@@ -62,7 +72,7 @@ public void start() throws IOException {
6272
projectId = buildResponse.getProjectId();
6373

6474
log.info("Visual Regression Tracker is started for project <{}>: buildId <{}>, projectId <{}>",
65-
projectName, projectId, buildId);
75+
projectName, projectId, buildId);
6676
}
6777

6878
public void stop() throws IOException {
@@ -71,10 +81,10 @@ public void stop() throws IOException {
7181
}
7282

7383
Request request = new Request.Builder()
74-
.url(paths.getBuildPathForBuild(buildId))
75-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
76-
.patch(RequestBody.create(JSON, ""))
77-
.build();
84+
.url(paths.getBuildPathForBuild(buildId))
85+
.addHeader(API_KEY_HEADER, configuration.getApiKey())
86+
.patch(RequestBody.create(JSON, ""))
87+
.build();
7888

7989
log.info("Stopping Visual Regression Tracker for buildId <{}>", buildId);
8090

@@ -128,34 +138,34 @@ protected TestRunResponse submitTestRun(String name, String imageBase64,
128138
}
129139

130140
TestRunRequest newTestRun = TestRunRequest.builder()
131-
.projectId(projectId)
132-
.buildId(buildId)
133-
.branchName(configuration.getBranchName())
134-
.name(name)
135-
.imageBase64(imageBase64)
136-
.os(testRunOptions.getOs())
137-
.browser(testRunOptions.getBrowser())
138-
.viewport(testRunOptions.getViewport())
139-
.device(testRunOptions.getDevice())
140-
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
141-
.build();
141+
.projectId(projectId)
142+
.buildId(buildId)
143+
.branchName(configuration.getBranchName())
144+
.name(name)
145+
.imageBase64(imageBase64)
146+
.os(testRunOptions.getOs())
147+
.browser(testRunOptions.getBrowser())
148+
.viewport(testRunOptions.getViewport())
149+
.device(testRunOptions.getDevice())
150+
.diffTollerancePercent(testRunOptions.getDiffTollerancePercent())
151+
.build();
142152

143153
RequestBody body = RequestBody.create(JSON, gson.toJson(newTestRun));
144154

145155
Request request = new Request.Builder()
146-
.url(paths.getTestRunPath())
147-
.addHeader(API_KEY_HEADER, configuration.getApiKey())
148-
.post(body)
149-
.build();
156+
.url(paths.getTestRunPath())
157+
.addHeader(API_KEY_HEADER, configuration.getApiKey())
158+
.post(body)
159+
.build();
150160

151161
Response response = client.newCall(request).execute();
152162
return handleResponse(response, TestRunResponse.class);
153163
}
154164

155165
protected <T> T handleResponse(Response response, Class<T> classOfT) throws IOException {
156166
String responseBody = Optional.ofNullable(response.body())
157-
.orElseThrow(() -> new TestRunException("Cannot get response body"))
158-
.string();
167+
.orElseThrow(() -> new TestRunException("Cannot get response body"))
168+
.string();
159169

160170
if (!response.isSuccessful()) {
161171
throw new TestRunException(responseBody);

0 commit comments

Comments
 (0)