Skip to content

Commit

Permalink
fix: add requestId to Snyk Code calls, change it to UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Oct 10, 2023
1 parent 3c5bc8f commit e6461a7
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 166 deletions.
9 changes: 5 additions & 4 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public interface DeepCodeRestApi {
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse createBundle(String orgName, FileContentRequest files);
CreateBundleResponse createBundle(String orgName, String requestId, FileContentRequest files);

/**
* Creates a new bundle for file(s) with Hash.
*
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse createBundle(String orgName, FileHashRequest files);
CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files);

/**
* Checks the status of a bundle.
Expand All @@ -33,7 +33,7 @@ public interface DeepCodeRestApi {
* @return {@link CreateBundleResponse} instance
*/
@NotNull
CreateBundleResponse checkBundle(String orgName, String bundleId);
CreateBundleResponse checkBundle(String orgName, String requestId, String bundleId);

/**
* Creates a new bundle by extending a previously uploaded one.
Expand All @@ -42,7 +42,7 @@ public interface DeepCodeRestApi {
* @return {@link CreateBundleResponse} instance
*/
@NotNull
<Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request);
<Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request);

/**
* Starts a new bundle analysis or checks its current status and available results.
Expand All @@ -52,6 +52,7 @@ public interface DeepCodeRestApi {
@NotNull
GetAnalysisResponse getAnalysis(
String orgName,
String requestId,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
Expand Down
167 changes: 67 additions & 100 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,27 @@ private interface CreateBundleCall {
@POST("bundle")
Call<CreateBundleResponse> doCreateBundle(
@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Body FileContentRequest files);

@retrofit2.http.Headers("Content-Type: application/json")
@POST("bundle")
Call<CreateBundleResponse> doCreateBundle(
@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Body FileHashRequest files);
}

private static <Req> CreateBundleResponse doCreateBundle(String orgName, Req request) {
private static <Req> CreateBundleResponse doCreateBundle(String orgName, String requestId, Req request) {
CreateBundleCall createBundleCall = retrofit.create(CreateBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
if (request instanceof FileContentRequest)
retrofitResponse =
createBundleCall.doCreateBundle(orgName, (FileContentRequest) request).execute();
createBundleCall.doCreateBundle(orgName, requestId, (FileContentRequest) request).execute();
else if (request instanceof FileHashRequest)
retrofitResponse =
createBundleCall.doCreateBundle(orgName, (FileHashRequest) request).execute();
createBundleCall.doCreateBundle(orgName, requestId, (FileHashRequest) request).execute();
else throw new IllegalArgumentException();
} catch (IOException e) {
return new CreateBundleResponse();
Expand All @@ -144,26 +146,14 @@ else if (request instanceof FileHashRequest)
result = new CreateBundleResponse();
}
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The bundle creation was successful");
break;
case 400:
result.setStatusDescription("Request content doesn't match the specifications");
break;
case 401:
result.setStatusDescription("Missing sessionToken or incomplete login process");
break;
case 403:
result.setStatusDescription("Unauthorized access to requested repository");
break;
case 404:
result.setStatusDescription("Unable to resolve requested oid");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
}
switch (retrofitResponse.code()) {
case 200 -> result.setStatusDescription("The bundle creation was successful");
case 400 -> result.setStatusDescription("Request content doesn't match the specifications");
case 401 -> result.setStatusDescription("Missing sessionToken or incomplete login process");
case 403 -> result.setStatusDescription("Unauthorized access to requested repository");
case 404 -> result.setStatusDescription("Unable to resolve requested oid");
default -> result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
}
return result;
}

Expand All @@ -174,8 +164,8 @@ else if (request instanceof FileHashRequest)
*/
@Override
@NotNull
public CreateBundleResponse createBundle(String orgName, FileContentRequest files) {
return doCreateBundle(orgName, files);
public CreateBundleResponse createBundle(String orgName, String requestId, FileContentRequest files) {
return doCreateBundle(orgName, requestId, files);
}

/**
Expand All @@ -185,15 +175,16 @@ public CreateBundleResponse createBundle(String orgName, FileContentRequest file
*/
@Override
@NotNull
public CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
return doCreateBundle(orgName, files);
public CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
return doCreateBundle(orgName, requestId, files);
}

private interface CheckBundleCall {
// @retrofit2.http.Headers("Content-Type: application/json")
@GET("bundle/{bundleId}")
Call<CreateBundleResponse> doCheckBundle(
@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Path(value = "bundleId", encoded = true) String bundleId);
}

Expand All @@ -205,11 +196,11 @@ Call<CreateBundleResponse> doCheckBundle(
*/
@Override
@NotNull
public CreateBundleResponse checkBundle(String orgName, String bundleId) {
public CreateBundleResponse checkBundle(String orgName, String requestId, String bundleId) {
CheckBundleCall checkBundleCall = retrofit.create(CheckBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
retrofitResponse = checkBundleCall.doCheckBundle(orgName, bundleId).execute();
retrofitResponse = checkBundleCall.doCheckBundle(orgName, requestId, bundleId).execute();
} catch (IOException e) {
return new CreateBundleResponse();
}
Expand All @@ -218,23 +209,13 @@ public CreateBundleResponse checkBundle(String orgName, String bundleId) {
result = new CreateBundleResponse();
}
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The bundle checked successfully");
break;
case 401:
result.setStatusDescription("Missing sessionToken or incomplete login process");
break;
case 403:
result.setStatusDescription("Unauthorized access to parent bundle");
break;
case 404:
result.setStatusDescription("Uploaded bundle has expired");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
}
switch (retrofitResponse.code()) {
case 200 -> result.setStatusDescription("The bundle checked successfully");
case 401 -> result.setStatusDescription("Missing sessionToken or incomplete login process");
case 403 -> result.setStatusDescription("Unauthorized access to parent bundle");
case 404 -> result.setStatusDescription("Uploaded bundle has expired");
default -> result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
}
return result;
}

Expand All @@ -244,13 +225,15 @@ private interface ExtendBundleCall {
Call<CreateBundleResponse> doExtendBundle(

@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Path(value = "bundleId", encoded = true) String bundleId,
@Body ExtendBundleWithHashRequest extendBundleWithHashRequest);

@retrofit2.http.Headers("Content-Type: application/json")
@PUT("bundle/{bundleId}")
Call<CreateBundleResponse> doExtendBundle(
@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Path(value = "bundleId", encoded = true) String bundleId,
@Body ExtendBundleWithContentRequest extendBundleWithContentRequest);
}
Expand All @@ -264,52 +247,43 @@ Call<CreateBundleResponse> doExtendBundle(
@Override
@NotNull
public <Req> CreateBundleResponse extendBundle(
String orgName, String bundleId, Req request) {
String orgName, String requestId, String bundleId, Req request) {
ExtendBundleCall extendBundleCall = retrofit.create(ExtendBundleCall.class);
Response<CreateBundleResponse> retrofitResponse;
try {
if (request instanceof ExtendBundleWithHashRequest)
retrofitResponse =
extendBundleCall
.doExtendBundle(orgName, bundleId, (ExtendBundleWithHashRequest) request)
.doExtendBundle(orgName, requestId, bundleId, (ExtendBundleWithHashRequest) request)
.execute();
else if (request instanceof ExtendBundleWithContentRequest)
retrofitResponse =
extendBundleCall
.doExtendBundle(orgName, bundleId, (ExtendBundleWithContentRequest) request)
.doExtendBundle(orgName, requestId, bundleId, (ExtendBundleWithContentRequest) request)
.execute();
else throw new IllegalArgumentException();
} catch (IOException e) {
return new CreateBundleResponse();
}
return getCreateBundleResponse(retrofitResponse);
}

@NotNull
private static CreateBundleResponse getCreateBundleResponse(Response<CreateBundleResponse> retrofitResponse) {
CreateBundleResponse result = retrofitResponse.body();
if (result == null) {
result = new CreateBundleResponse();
}
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The bundle extension was successful");
break;
case 400:
result.setStatusDescription(
case 200 -> result.setStatusDescription("The bundle extension was successful");
case 400 -> result.setStatusDescription(
"Attempted to extend a git bundle, or ended up with an empty bundle after the extension");
break;
case 401:
result.setStatusDescription("Missing sessionToken or incomplete login process");
break;
case 403:
result.setStatusDescription("Unauthorized access to parent bundle");
break;
case 404:
result.setStatusDescription("Parent bundle has expired");
break;
case 413:
result.setStatusDescription("Payload too large");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
case 401 -> result.setStatusDescription("Missing sessionToken or incomplete login process");
case 403 -> result.setStatusDescription("Unauthorized access to parent bundle");
case 404 -> result.setStatusDescription("Parent bundle has expired");
case 413 -> result.setStatusDescription("Payload too large");
default -> result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
}
return result;
}
Expand All @@ -319,6 +293,7 @@ private interface GetAnalysisCall {
@POST("analysis")
Call<GetAnalysisResponse> doGetAnalysis(
@Header("snyk-org-name") String orgName,
@Header("snyk-request-id") String requestId,
@Body GetAnalysisRequest filesToAnalyse);
}

Expand All @@ -331,6 +306,7 @@ Call<GetAnalysisResponse> doGetAnalysis(
@NotNull
public GetAnalysisResponse getAnalysis(
String orgName,
String requestId,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
Expand All @@ -341,31 +317,28 @@ public GetAnalysisResponse getAnalysis(
try {
Response<GetAnalysisResponse> retrofitResponse =
getAnalysisCall
.doGetAnalysis(orgName, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgName))
.doGetAnalysis(orgName, requestId, new GetAnalysisRequest(bundleId, filesToAnalyse, severity, shard, ideProductName, orgName))
.execute();
GetAnalysisResponse result = retrofitResponse.body();
if (result == null) result = new GetAnalysisResponse();
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The analysis request was successful");
break;
case 401:
result.setStatusDescription("Missing sessionToken or incomplete login process");
break;
case 403:
result.setStatusDescription("Unauthorized access to requested repository");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
}
return result;
return getGetAnalysisResponse(retrofitResponse);
} catch (IOException e) {
return new GetAnalysisResponse();
}
}

@NotNull
private static GetAnalysisResponse getGetAnalysisResponse(Response<GetAnalysisResponse> retrofitResponse) {
GetAnalysisResponse result = retrofitResponse.body();
if (result == null) result = new GetAnalysisResponse();
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200 -> result.setStatusDescription("The analysis request was successful");
case 401 -> result.setStatusDescription("Missing sessionToken or incomplete login process");
case 403 -> result.setStatusDescription("Unauthorized access to requested repository");
default -> result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
}
return result;
}

private interface GetFiltersCall {
@GET("filters")
Call<GetFiltersResponse> doGetFilters();
Expand All @@ -385,17 +358,11 @@ public GetFiltersResponse getFilters() {
GetFiltersResponse result = retrofitResponse.body();
if (result == null) result = new GetFiltersResponse();
result.setStatusCode(retrofitResponse.code());
switch (retrofitResponse.code()) {
case 200:
result.setStatusDescription("The filters request was successful");
break;
case 401:
result.setStatusDescription("Missing sessionToken or incomplete login process");
break;
default:
result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
break;
}
switch (retrofitResponse.code()) {
case 200 -> result.setStatusDescription("The filters request was successful");
case 401 -> result.setStatusDescription("Missing sessionToken or incomplete login process");
default -> result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code());
}
return result;
} catch (IOException e) {
return new GetFiltersResponse();
Expand Down
Loading

0 comments on commit e6461a7

Please sign in to comment.