From cd6a75791094713b62634b3ccd02bd55b8001c75 Mon Sep 17 00:00:00 2001 From: Artsiom Chapialiou Date: Tue, 5 Oct 2021 13:27:34 +0300 Subject: [PATCH] feat: added param "waiting results" timeout --- CHANGELOG.md | 3 +++ build.gradle | 2 +- .../javaclient/DeepCodeRestApiTest.java | 23 ++++-------------- .../deepcode/javaclient/DeepCodeRestApi.java | 24 +++---------------- .../javaclient/core/AnalysisDataBase.java | 17 ++++++------- .../javaclient/core/DeepCodeParamsBase.java | 12 +++++++++- 6 files changed, 32 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0615c6c..0eef4ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [2.1.11] - 2020-10-05 +- added param: "waiting results" timeout. + ## [2.0.18] - 2020-10-05 - fix exception when Null markers received. diff --git a/build.gradle b/build.gradle index 3f45519..d88e9e0 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { group = "io.snyk.code.sdk" archivesBaseName = "snyk-code-client" -version = "2.1.10" +version = "2.1.11" repositories { mavenCentral() diff --git a/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java b/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java index 9b101f7..38bda7e 100644 --- a/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java +++ b/src/integTest/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java @@ -49,13 +49,11 @@ public class DeepCodeRestApiTest { @Test public void _010_newLogin() { System.out.println("\n--------------New Login----------------\n"); - LoginResponse response = null; - response = DeepCodeRestApi.newLogin(userAgent); - assertEquals(200, response.getStatusCode()); - assertTrue(response.getLoginURL().contains(response.getSessionToken())); - System.out.printf( - "New login request passed with returned: \nsession token: %1$s \nlogin URL: %2$s\n", - response.getSessionToken(), response.getLoginURL()); + try { + DeepCodeRestApi.newLogin(userAgent); + } catch (UnsupportedOperationException e) { + System.out.printf(e.getMessage()); + } } @Test @@ -157,17 +155,6 @@ public void _031_createBundle_wrong_request() { "Create Bundle call with malformed token [%1$s] is not accepted by server with Status code [%2$d].\n", brokenToken, response.getStatusCode()); - final String incompleteLoginToken = DeepCodeRestApi.newLogin(userAgent).getSessionToken(); - response = DeepCodeRestApi.createBundle(incompleteLoginToken, files); - assertNotNull(response); - assertEquals( - "Create Bundle call with incomplete login token should not be accepted by server", - 401, - response.getStatusCode()); - System.out.printf( - "Create Bundle call with incomplete login token is not accepted by server with Status code [%2$d].\n", - brokenToken, response.getStatusCode()); - // seems to be a bug on server: it returns 200 /* response = diff --git a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java index 4fd5d4b..c16ccd5 100644 --- a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java +++ b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java @@ -38,7 +38,7 @@ public final class DeepCodeRestApi { private DeepCodeRestApi() {} - private static final String API_URL = "https://www.deepcode.ai/"; + private static final String API_URL = "https://deeproxy.snyk.io/"; private static Retrofit retrofit = buildRetrofit(API_URL, false); @@ -113,26 +113,8 @@ private interface LoginCall { * @return {@link LoginResponse} instance */ @NotNull - public static LoginResponse newLogin(@NotNull String userAgent) { - final LoginCall loginCall = retrofit.create(LoginCall.class); - try { - final SourceString source = new SourceString(userAgent); - final Response retrofitResponse = loginCall.doNewLogin(source).execute(); - LoginResponse result = retrofitResponse.body(); - if (result == null) return new LoginResponse(); - result.setStatusCode(retrofitResponse.code()); - switch (retrofitResponse.code()) { - case 200: - result.setStatusDescription("The new login request was successful"); - break; - default: - result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code()); - break; - } - return result; - } catch (IOException e) { - return new LoginResponse(); - } + public static LoginResponse newLogin(@NotNull String userAgent) throws UnsupportedOperationException { + throw new UnsupportedOperationException("login request is not handled anymore"); } private interface CheckSessionCall { diff --git a/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java b/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java index 87827c0..8410fb4 100644 --- a/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/AnalysisDataBase.java @@ -548,8 +548,8 @@ private GetAnalysisResponse doGetAnalysis( List filesToAnalyse) { GetAnalysisResponse response; int counter = 0; - final int timeout = 100; // seconds - final int attempts = timeout * 1000 / pdUtils.DEFAULT_DELAY; + final long timeout = deepCodeParams.getTimeoutForGettingAnalysesMs(); + final long attempts = timeout / pdUtils.DEFAULT_DELAY; do { if (counter > 0) pdUtils.delay(pdUtils.DEFAULT_DELAY, progress); response = @@ -566,19 +566,20 @@ private GetAnalysisResponse doGetAnalysis( return new GetAnalysisResponse(); double responseProgress = response.getProgress(); - if (responseProgress <= 0 || responseProgress > 1) + if (responseProgress <= 0 || responseProgress > 1) { responseProgress = ((double) counter) / attempts; + } pdUtils.progressSetFraction(progress, responseProgress); pdUtils.progressSetText( progress, WAITING_FOR_ANALYSIS_TEXT + (int) (responseProgress * 100) + "% done"); if (counter >= attempts) { dcLogger.logWarn("Timeout expire for waiting analysis results."); - /* - DeepCodeNotifications.showWarn( - "Can't get analysis results from the server. Network or server internal error. Please, try again later.", - project); - */ + pdUtils.showWarn( + "Can't get analysis results from the server. Timeout of " + timeout/1000 + " sec. is reached." + + " Please, increase timeout or try again later.", + project, + false); break; } diff --git a/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java b/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java index c244db0..1b7a5ef 100644 --- a/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java +++ b/src/main/java/ai/deepcode/javaclient/core/DeepCodeParamsBase.java @@ -3,6 +3,8 @@ import ai.deepcode.javaclient.DeepCodeRestApi; import org.jetbrains.annotations.NotNull; +import java.util.function.Supplier; + public abstract class DeepCodeParamsBase { // Settings @@ -16,6 +18,7 @@ public abstract class DeepCodeParamsBase { // Inner params private String loginUrl; private String ideProductName; + private Supplier getTimeoutForGettingAnalysesMs; protected DeepCodeParamsBase( boolean isEnable, @@ -25,7 +28,9 @@ protected DeepCodeParamsBase( int minSeverity, String sessionToken, String loginUrl, - String ideProductName) { + String ideProductName, + Supplier getTimeoutForGettingAnalysesMs + ) { this.isEnable = isEnable; this.apiUrl = apiUrl; this.disableSslVerification = disableSslVerification; @@ -34,6 +39,7 @@ protected DeepCodeParamsBase( this.sessionToken = sessionToken; this.loginUrl = loginUrl; this.ideProductName = ideProductName; + this.getTimeoutForGettingAnalysesMs = getTimeoutForGettingAnalysesMs; } public void clearLoginParams() { @@ -116,4 +122,8 @@ public void setEnable(boolean isEnable) { public String getIdeProductName() { return ideProductName; } + + public long getTimeoutForGettingAnalysesMs() { + return getTimeoutForGettingAnalysesMs.get(); + } }