1
1
package ai .deepcode .javaclient ;
2
2
3
+ import ai .deepcode .javaclient .core .Base64EncodeRequestInterceptor ;
3
4
import ai .deepcode .javaclient .requests .ExtendBundleWithContentRequest ;
4
5
import ai .deepcode .javaclient .requests .ExtendBundleWithHashRequest ;
5
6
import ai .deepcode .javaclient .requests .FileContentRequest ;
@@ -47,7 +48,7 @@ public class DeepCodeRestApiImpl implements DeepCodeRestApi {
47
48
48
49
// Create simple REST adapter which points the baseUrl.
49
50
private static Retrofit buildRetrofit (
50
- String baseUrl , boolean disableSslVerification , boolean requestLogging ) {
51
+ String baseUrl , boolean disableSslVerification , boolean requestLogging ) {
51
52
HttpLoggingInterceptor logging = new HttpLoggingInterceptor ();
52
53
// set your desired log level
53
54
if (requestLogging ) {
@@ -57,15 +58,16 @@ private static Retrofit buildRetrofit(
57
58
}
58
59
59
60
OkHttpClient .Builder builder =
60
- new OkHttpClient .Builder ()
61
- .connectTimeout (100 , TimeUnit .SECONDS )
62
- .writeTimeout (100 , TimeUnit .SECONDS )
63
- .readTimeout (100 , TimeUnit .SECONDS )
64
- .addInterceptor (logging );
61
+ new OkHttpClient .Builder ()
62
+ .connectTimeout (100 , TimeUnit .SECONDS )
63
+ .writeTimeout (100 , TimeUnit .SECONDS )
64
+ .readTimeout (100 , TimeUnit .SECONDS )
65
+ .addInterceptor (logging )
66
+ .addInterceptor (new Base64EncodeRequestInterceptor ());
65
67
66
68
if (disableSslVerification ) {
67
69
X509TrustManager x509TrustManager = buildUnsafeTrustManager ();
68
- final TrustManager [] trustAllCertificates = new TrustManager [] {x509TrustManager };
70
+ final TrustManager [] trustAllCertificates = new TrustManager []{x509TrustManager };
69
71
70
72
try {
71
73
final String sslProtocol = "SSL" ;
@@ -82,24 +84,26 @@ private static Retrofit buildRetrofit(
82
84
83
85
OkHttpClient client = builder .build ();
84
86
return new Retrofit .Builder ()
85
- .baseUrl (baseUrl )
86
- .client (client )
87
- .addConverterFactory (GsonConverterFactory .create ())
88
- .build ();
87
+ .baseUrl (baseUrl )
88
+ .client (client )
89
+ .addConverterFactory (GsonConverterFactory .create ())
90
+ .build ();
89
91
}
90
92
91
93
@ NotNull
92
94
private static X509TrustManager buildUnsafeTrustManager () {
93
95
return new X509TrustManager () {
94
96
@ Override
95
- public void checkClientTrusted (X509Certificate [] chain , String authType ) {}
97
+ public void checkClientTrusted (X509Certificate [] chain , String authType ) {
98
+ }
96
99
97
100
@ Override
98
- public void checkServerTrusted (X509Certificate [] chain , String authType ) {}
101
+ public void checkServerTrusted (X509Certificate [] chain , String authType ) {
102
+ }
99
103
100
104
@ Override
101
105
public X509Certificate [] getAcceptedIssuers () {
102
- return new X509Certificate [] {};
106
+ return new X509Certificate []{};
103
107
}
104
108
};
105
109
}
@@ -108,28 +112,28 @@ public X509Certificate[] getAcceptedIssuers() {
108
112
* Re-set baseUrl for retrofit instance
109
113
*
110
114
* @param baseUrl new baseUrl. <b>Null</b> or empty "" value will reset to default {@code
111
- * #API_URL}
115
+ * #API_URL}
112
116
*/
113
117
@ Override
114
118
public void setBaseUrl (
115
119
@ Nullable String baseUrl , boolean disableSslVerification , boolean requestLogging ) {
116
120
retrofit =
117
- buildRetrofit (
118
- (baseUrl == null || baseUrl .isEmpty ()) ? API_URL : baseUrl ,
119
- disableSslVerification ,
120
- requestLogging );
121
+ buildRetrofit (
122
+ (baseUrl == null || baseUrl .isEmpty ()) ? API_URL : baseUrl ,
123
+ disableSslVerification ,
124
+ requestLogging );
121
125
}
122
126
123
127
private interface CreateBundleCall {
124
128
@ retrofit2 .http .Headers ("Content-Type: application/json" )
125
129
@ POST ("bundle" )
126
130
Call <CreateBundleResponse > doCreateBundle (
127
- @ Header ("Session-Token" ) String token , @ Body FileContentRequest files );
131
+ @ Header ("Session-Token" ) String token , @ Body FileContentRequest files );
128
132
129
133
@ retrofit2 .http .Headers ("Content-Type: application/json" )
130
134
@ POST ("bundle" )
131
135
Call <CreateBundleResponse > doCreateBundle (
132
- @ Header ("Session-Token" ) String token , @ Body FileHashRequest files );
136
+ @ Header ("Session-Token" ) String token , @ Body FileHashRequest files );
133
137
}
134
138
135
139
private static <Req > CreateBundleResponse doCreateBundle (String token , Req request ) {
@@ -138,10 +142,10 @@ private static <Req> CreateBundleResponse doCreateBundle(String token, Req reque
138
142
try {
139
143
if (request instanceof FileContentRequest )
140
144
retrofitResponse =
141
- createBundleCall .doCreateBundle (token , (FileContentRequest ) request ).execute ();
145
+ createBundleCall .doCreateBundle (token , (FileContentRequest ) request ).execute ();
142
146
else if (request instanceof FileHashRequest )
143
147
retrofitResponse =
144
- createBundleCall .doCreateBundle (token , (FileHashRequest ) request ).execute ();
148
+ createBundleCall .doCreateBundle (token , (FileHashRequest ) request ).execute ();
145
149
else throw new IllegalArgumentException ();
146
150
} catch (IOException e ) {
147
151
return new CreateBundleResponse ();
@@ -200,8 +204,8 @@ private interface CheckBundleCall {
200
204
// @retrofit2.http.Headers("Content-Type: application/json")
201
205
@ GET ("bundle/{bundleId}" )
202
206
Call <CreateBundleResponse > doCheckBundle (
203
- @ Header ("Session-Token" ) String token ,
204
- @ Path (value = "bundleId" , encoded = true ) String bundleId );
207
+ @ Header ("Session-Token" ) String token ,
208
+ @ Path (value = "bundleId" , encoded = true ) String bundleId );
205
209
}
206
210
207
211
/**
@@ -249,16 +253,16 @@ private interface ExtendBundleCall {
249
253
@ retrofit2 .http .Headers ("Content-Type: application/json" )
250
254
@ PUT ("bundle/{bundleId}" )
251
255
Call <CreateBundleResponse > doExtendBundle (
252
- @ Header ("Session-Token" ) String token ,
253
- @ Path (value = "bundleId" , encoded = true ) String bundleId ,
254
- @ Body ExtendBundleWithHashRequest extendBundleWithHashRequest );
256
+ @ Header ("Session-Token" ) String token ,
257
+ @ Path (value = "bundleId" , encoded = true ) String bundleId ,
258
+ @ Body ExtendBundleWithHashRequest extendBundleWithHashRequest );
255
259
256
260
@ retrofit2 .http .Headers ("Content-Type: application/json" )
257
261
@ PUT ("bundle/{bundleId}" )
258
262
Call <CreateBundleResponse > doExtendBundle (
259
- @ Header ("Session-Token" ) String token ,
260
- @ Path (value = "bundleId" , encoded = true ) String bundleId ,
261
- @ Body ExtendBundleWithContentRequest extendBundleWithContentRequest );
263
+ @ Header ("Session-Token" ) String token ,
264
+ @ Path (value = "bundleId" , encoded = true ) String bundleId ,
265
+ @ Body ExtendBundleWithContentRequest extendBundleWithContentRequest );
262
266
}
263
267
264
268
/**
@@ -276,14 +280,14 @@ public <Req> CreateBundleResponse extendBundle(
276
280
try {
277
281
if (request instanceof ExtendBundleWithHashRequest )
278
282
retrofitResponse =
279
- extendBundleCall
280
- .doExtendBundle (token , bundleId , (ExtendBundleWithHashRequest ) request )
281
- .execute ();
283
+ extendBundleCall
284
+ .doExtendBundle (token , bundleId , (ExtendBundleWithHashRequest ) request )
285
+ .execute ();
282
286
else if (request instanceof ExtendBundleWithContentRequest )
283
287
retrofitResponse =
284
- extendBundleCall
285
- .doExtendBundle (token , bundleId , (ExtendBundleWithContentRequest ) request )
286
- .execute ();
288
+ extendBundleCall
289
+ .doExtendBundle (token , bundleId , (ExtendBundleWithContentRequest ) request )
290
+ .execute ();
287
291
else throw new IllegalArgumentException ();
288
292
} catch (IOException e ) {
289
293
return new CreateBundleResponse ();
@@ -299,7 +303,7 @@ else if (request instanceof ExtendBundleWithContentRequest)
299
303
break ;
300
304
case 400 :
301
305
result .setStatusDescription (
302
- "Attempted to extend a git bundle, or ended up with an empty bundle after the extension" );
306
+ "Attempted to extend a git bundle, or ended up with an empty bundle after the extension" );
303
307
break ;
304
308
case 401 :
305
309
result .setStatusDescription ("Missing sessionToken or incomplete login process" );
@@ -324,7 +328,7 @@ private interface GetAnalysisCall {
324
328
@ retrofit2 .http .Headers ("Content-Type: application/json" )
325
329
@ POST ("analysis" )
326
330
Call <GetAnalysisResponse > doGetAnalysis (
327
- @ Header ("Session-Token" ) String token , @ Body GetAnalysisRequest filesToAnalyse );
331
+ @ Header ("Session-Token" ) String token , @ Body GetAnalysisRequest filesToAnalyse );
328
332
}
329
333
330
334
/**
@@ -346,9 +350,9 @@ public GetAnalysisResponse getAnalysis(
346
350
GetAnalysisCall getAnalysisCall = retrofit .create (GetAnalysisCall .class );
347
351
try {
348
352
Response <GetAnalysisResponse > retrofitResponse =
349
- getAnalysisCall
350
- .doGetAnalysis (token , new GetAnalysisRequest (bundleId , filesToAnalyse , severity , shard , ideProductName , orgDisplayName ))
351
- .execute ();
353
+ getAnalysisCall
354
+ .doGetAnalysis (token , new GetAnalysisRequest (bundleId , filesToAnalyse , severity , shard , ideProductName , orgDisplayName ))
355
+ .execute ();
352
356
GetAnalysisResponse result = retrofitResponse .body ();
353
357
if (result == null ) result = new GetAnalysisResponse ();
354
358
result .setStatusCode (retrofitResponse .code ());
0 commit comments