1
1
package io .visual_regression_tracker .sdk_java ;
2
2
3
3
import com .google .gson .Gson ;
4
+ import okhttp3 .*;
4
5
5
6
import java .io .IOException ;
6
- import java .net .URI ;
7
- import java .net .http .HttpClient ;
8
- import java .net .http .HttpRequest ;
9
- import java .net .http .HttpResponse ;
10
7
import java .util .HashMap ;
11
8
import java .util .Map ;
12
9
13
10
public class VisualRegressionTracker {
11
+ static final MediaType JSON = MediaType .get ("application/json; charset=utf-8" );
14
12
Config config ;
15
13
String buildId ;
16
- HttpClient client ;
14
+ OkHttpClient client ;
17
15
18
16
public VisualRegressionTracker (Config config ) {
19
17
this .config = config ;
20
18
21
- this .client = HttpClient . newHttpClient ();
19
+ this .client = new OkHttpClient ();
22
20
}
23
21
24
- void startBuild () throws IOException , InterruptedException {
22
+ void startBuild () throws IOException {
25
23
if (this .buildId == null ) {
26
24
Map <String , String > data = new HashMap <>();
27
25
data .put ("projectId" , this .config .projectId );
28
26
data .put ("branchName" , this .config .branchName );
29
27
30
- HttpRequest request = HttpRequest .newBuilder ()
31
- .uri (URI .create (this .config .apiUrl .concat ("/builds" )))
32
- .header ("apiKey" , this .config .token )
33
- .header ("Content-Type" , "application/json" )
34
- .POST (HttpRequest .BodyPublishers .ofString (new Gson ().toJson (data )))
35
- .build ();
28
+ RequestBody body = RequestBody .create (new Gson ().toJson (data ), JSON );
36
29
37
- HttpResponse <String > response = this .client .send (request , HttpResponse .BodyHandlers .ofString ());
38
- BuildDTO buildDTO = new Gson ().fromJson (response .body (), BuildDTO .class );
30
+ Request request = new Request .Builder ()
31
+ .url (this .config .apiUrl .concat ("/builds" ))
32
+ .addHeader ("apiKey" , this .config .token )
33
+ .post (body )
34
+ .build ();
39
35
40
- this .buildId = buildDTO .getId ();
36
+ try (ResponseBody responseBody = client .newCall (request ).execute ().body ()) {
37
+ BuildDTO buildDTO = new Gson ().fromJson (responseBody .string (), BuildDTO .class );
38
+ this .buildId = buildDTO .getId ();
39
+ }
41
40
}
42
41
}
43
42
44
- TestResultDTO submitTestRun (TestRun testRun ) throws IOException , InterruptedException {
43
+ TestResultDTO submitTestRun (TestRun testRun ) throws IOException {
45
44
Map <String , Object > data = new HashMap <>();
46
45
data .put ("projectId" , this .config .projectId );
47
46
data .put ("buildId" , this .buildId );
@@ -53,30 +52,30 @@ TestResultDTO submitTestRun(TestRun testRun) throws IOException, InterruptedExce
53
52
data .put ("device" , testRun .getDevice ());
54
53
data .put ("diffTollerancePercent" , testRun .getDiffTollerancePercent ());
55
54
55
+ RequestBody body = RequestBody .create (new Gson ().toJson (data ), JSON );
56
56
57
- HttpRequest request = HttpRequest .newBuilder ()
58
- .uri (URI .create (this .config .apiUrl .concat ("/test" )))
59
- .header ("apiKey" , this .config .token )
60
- .header ("Content-Type" , "application/json" )
61
- .POST (HttpRequest .BodyPublishers .ofString (new Gson ().toJson (data )))
57
+ Request request = new Request .Builder ()
58
+ .url (this .config .apiUrl .concat ("/test" ))
59
+ .addHeader ("apiKey" , this .config .token )
60
+ .post (body )
62
61
.build ();
63
62
64
- HttpResponse <String > response = this .client .send (request , HttpResponse .BodyHandlers .ofString ());
65
63
66
- System .out .println (response .body ());
67
- return new Gson ().fromJson (response .body (), TestResultDTO .class );
64
+ try (ResponseBody responseBody = client .newCall (request ).execute ().body ()) {
65
+ return new Gson ().fromJson (responseBody .string (), TestResultDTO .class );
66
+ }
68
67
}
69
68
70
- public void track (TestRun testRun ) throws IOException , InterruptedException {
69
+ public void track (TestRun testRun ) throws IOException {
71
70
this .startBuild ();
72
71
73
72
TestResultDTO testResultDTO = this .submitTestRun (testRun );
74
73
75
- if (testResultDTO .getStatus ().equals ("new" )) {
74
+ if (testResultDTO .getStatus ().equals ("new" )) {
76
75
throw new TestRunException ("No baseline: " .concat (testResultDTO .getUrl ()));
77
76
}
78
77
79
- if (testResultDTO .getStatus ().equals ("unresolved" )) {
78
+ if (testResultDTO .getStatus ().equals ("unresolved" )) {
80
79
throw new TestRunException ("Difference found: " .concat (testResultDTO .getUrl ()));
81
80
}
82
81
}
0 commit comments