Skip to content

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

Merged
yahavi merged 1 commit into
jfrog:devfrom
adigfrog:JR-7345
Sep 20, 2023
Merged

Add Streaming Rest Call - returning the response input stream without…#379
yahavi merged 1 commit into
jfrog:devfrom
adigfrog:JR-7345

Conversation

@adigfrog

@adigfrog adigfrog commented Sep 11, 2023

Copy link
Copy Markdown
Contributor

… 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

github-actions Bot commented Sep 11, 2023

Copy link
Copy Markdown

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

@alenon alenon left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 []

@adigfrog adigfrog Sep 20, 2023

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


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

HttpResponse execute(HttpUriRequest request) throws IOException;

Copy link
Copy Markdown
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
Copy Markdown
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


ArtifactoryResponse restCall(ArtifactoryRequest artifactoryRequest) throws IOException;

ArtifactoryStreamingResponse streamingRestCall(ArtifactoryRequest artifactoryRequest) throws IOException;

Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread 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
Copy Markdown
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
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