11package  io .visual_regression_tracker .sdk_java ;
22
33import  com .google .gson .Gson ;
4+ import  okhttp3 .*;
45
56import  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 ;
107import  java .util .HashMap ;
118import  java .util .Map ;
129
1310public  class  VisualRegressionTracker  {
11+     static  final  MediaType  JSON  = MediaType .get ("application/json; charset=utf-8" );
1412    Config  config ;
1513    String  buildId ;
16-     HttpClient  client ;
14+     OkHttpClient  client ;
1715
1816    public  VisualRegressionTracker (Config  config ) {
1917        this .config  = config ;
2018
21-         this .client  = HttpClient . newHttpClient ();
19+         this .client  = new   OkHttpClient ();
2220    }
2321
24-     void  startBuild () throws  IOException ,  InterruptedException  {
22+     void  startBuild () throws  IOException  {
2523        if  (this .buildId  == null ) {
2624            Map <String , String > data  = new  HashMap <>();
2725            data .put ("projectId" , this .config .projectId );
2826            data .put ("branchName" , this .config .branchName );
2927
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 );
3629
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 ();
3935
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+             }
4140        }
4241    }
4342
44-     TestResultDTO  submitTestRun (TestRun  testRun ) throws  IOException ,  InterruptedException  {
43+     TestResultDTO  submitTestRun (TestRun  testRun ) throws  IOException  {
4544        Map <String , Object > data  = new  HashMap <>();
4645        data .put ("projectId" , this .config .projectId );
4746        data .put ("buildId" , this .buildId );
@@ -53,30 +52,30 @@ TestResultDTO submitTestRun(TestRun testRun) throws IOException, InterruptedExce
5352        data .put ("device" , testRun .getDevice ());
5453        data .put ("diffTollerancePercent" , testRun .getDiffTollerancePercent ());
5554
55+         RequestBody  body  = RequestBody .create (new  Gson ().toJson (data ), JSON );
5656
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 )
6261                .build ();
6362
64-         HttpResponse <String > response  = this .client .send (request , HttpResponse .BodyHandlers .ofString ());
6563
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+         }
6867    }
6968
70-     public  void  track (TestRun  testRun ) throws  IOException ,  InterruptedException  {
69+     public  void  track (TestRun  testRun ) throws  IOException  {
7170        this .startBuild ();
7271
7372        TestResultDTO  testResultDTO  = this .submitTestRun (testRun );
7473
75-         if (testResultDTO .getStatus ().equals ("new" )) {
74+         if   (testResultDTO .getStatus ().equals ("new" )) {
7675            throw  new  TestRunException ("No baseline: " .concat (testResultDTO .getUrl ()));
7776        }
7877
79-         if (testResultDTO .getStatus ().equals ("unresolved" )) {
78+         if   (testResultDTO .getStatus ().equals ("unresolved" )) {
8079            throw  new  TestRunException ("Difference found: " .concat (testResultDTO .getUrl ()));
8180        }
8281    }
0 commit comments