Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Streaming Rest Call - returning the response input stream without… #379

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

adigfrog
Copy link
Contributor

@adigfrog adigfrog commented Sep 11, 2023

… the wrappers

  • All tests passed. If this feature is not already covered by the tests, I added new tests.
  • This pull request is on the dev branch.

@github-actions
Copy link

github-actions bot commented Sep 11, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

Copy link

@alenon alenon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider moving the isSuccessResponse to the common class
And making the common implementation to look like the following
public static boolean isSuccessfulResponseCode(int status) { return 200 <= status && status < 300; }

@adigfrog
Copy link
Contributor Author

Please consider moving the isSuccessResponse to the common class And making the common implementation to look like the following public static boolean isSuccessfulResponseCode(int status) { return 200 <= status && status < 300; }

That was my first change but I saw that in the function getInputStreamWithHeaders:

.../services/src/main/groovy/org/jfrog/artifactory/client/impl)/ArtifactoryImpl.java:

public InputStream getInputStreamWithHeaders(String path, Map<String, String> headers) throws IOException {
        HttpResponse httpResponse = get(path, null, null, headers);
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK ||
                httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_PARTIAL_CONTENT) {
            return httpResponse.getEntity().getContent();
        }
        throw newHttpResponseException(httpResponse);
    }

So I understood that for streaming only 200 and 206 are valid

@adigfrog
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@adigfrog adigfrog force-pushed the JR-7345 branch 2 times, most recently from aec4be2 to 618959e Compare September 13, 2023 06:21

private static final ObjectMapper objectMapper = new ObjectMapper();

private HttpResponse httpResponse;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant new line

Suggested change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return status >= 200 && status < 300;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

ArtifactoryStreamingResponse response = artifactory.streamingRestCall(request);
assertFalse(response.isSuccessResponse());
assertEquals(response.getStatusLine().getStatusCode(), 404);
assertEquals(response.getStatusLine().getReasonPhrase(), "Not Found");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got an empty reason phrase on my environment:

java.lang.AssertionError: expected [Not Found] but found []

Copy link
Contributor Author

@adigfrog adigfrog Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

* ArtifactoryStreamingResponse object returned from {@link Artifactory#streamingRestCall(ArtifactoryRequest)}.
* acts as a wrapper for {@link org.apache.http.HttpResponse}.
*/
public interface ArtifactoryStreamingResponse extends BaseArtifactoryResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion - please consider making ArtifactoryStreamingResponse AutoClosable to allow using easily it with try-with-resources

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

public interface ArtifactoryStreamingResponse extends BaseArtifactoryResponse {

InputStream getInputStream() throws IOException;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

InputStream getInputStream(String path) throws IOException;

InputStream getInputStreamWithHeaders(String path, Map<String, String> headers) throws IOException;

HttpResponse execute(HttpUriRequest request) throws IOException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so

@@ -42,10 +44,14 @@ public interface Artifactory extends ApiInterface, AutoCloseable {

ArtifactoryResponse restCall(ArtifactoryRequest artifactoryRequest) throws IOException;

ArtifactoryStreamingResponse streamingRestCall(ArtifactoryRequest artifactoryRequest) throws IOException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document it to the README

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done and tested

README.md Outdated
assert response.isSuccessResponse();

// Get the response raw body using input stream
String rawBody = IOUtils.toString(response.getInputStream(),StandardCharsets.UTF_8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String rawBody = IOUtils.toString(response.getInputStream(),StandardCharsets.UTF_8);
String rawBody = IOUtils.toString(response.getInputStream(), StandardCharsets.UTF_8);

@yahavi yahavi added the feature request New feature or request label Sep 20, 2023
@yahavi yahavi merged commit 102bee9 into jfrog:dev Sep 20, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants