-
Notifications
You must be signed in to change notification settings - Fork 97
BREAKING CHANGE: Remove shaded dependencies and use HttpClient interface #219
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cfbf149
BREAKING CHANGE: Remove shaded dependencies
Abhi347 4ef2a54
feat: Use HttpClient interface
Abhi347 19754fe
test: Fix tests
Abhi347 4f4ad39
fix: Fix compile errors
Abhi347 2048a9b
fix: Fix all tests
Abhi347 c0ef3f3
fix: Fix NPE
Abhi347 1924023
test: Add more tests
Abhi347 f736c36
test: Add more tests and doc comments
Abhi347 da107fc
test: More tests and minor refactoring
Abhi347 67f94b8
Merge branch 'master' into breaking-change/remove-shading-http
Abhi347 c64c195
Merge branch 'master' into breaking-change/remove-shading-http
Abhi347 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/main/java/com/spotify/github/http/BaseHttpResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/*- | ||
* -\-\- | ||
* github-api | ||
* -- | ||
* Copyright (C) 2021 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.github.http; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** BaseHttpResponse is the base implementation of HttpResponse. */ | ||
public abstract class BaseHttpResponse implements HttpResponse { | ||
private static final int HTTP_OK = 200; | ||
private static final int HTTP_BAD_REQUEST = 400; | ||
|
||
protected final HttpRequest request; | ||
protected final int statusCode; | ||
protected final String statusMessage; | ||
protected final Map<String, List<String>> headers; | ||
|
||
public BaseHttpResponse( | ||
final HttpRequest request, | ||
final int statusCode, | ||
final String statusMessage, | ||
final Map<String, List<String>> headers) { | ||
this.request = request; | ||
this.statusCode = statusCode; | ||
this.statusMessage = statusMessage; | ||
this.headers = headers; | ||
} | ||
|
||
/** | ||
* Returns the request that generated this response. | ||
* | ||
* @return HttpRequest the request that generated this response | ||
*/ | ||
@Override | ||
public HttpRequest request() { | ||
return request; | ||
} | ||
|
||
/** | ||
* Returns the HTTP status code of the response. | ||
* | ||
* @return the status code of the response | ||
*/ | ||
@Override | ||
public int statusCode() { | ||
return statusCode; | ||
} | ||
|
||
/** | ||
* Returns the HTTP status message of the response. | ||
* | ||
* @return the status message of the response | ||
*/ | ||
@Override | ||
public String statusMessage() { | ||
return statusMessage; | ||
} | ||
|
||
/** | ||
* Returns the headers of the response. | ||
* | ||
* @return the headers of the response as a Map of strings | ||
*/ | ||
@Override | ||
public Map<String, List<String>> headers() { | ||
return this.headers; | ||
} | ||
|
||
/** | ||
* Returns the values of the header with the given name. If the header is not present, this method | ||
* returns null. | ||
* | ||
* @param headerName the name of the header | ||
* @return the values of the header with the given name as a List of strings, or null if the | ||
* header is not present | ||
*/ | ||
@Override | ||
public List<String> headers(final String headerName) { | ||
return this.headers.get(headerName); | ||
} | ||
|
||
/** | ||
* Returns the first value of the header with the given name. If the header is not present, this | ||
* method returns null. | ||
* | ||
* @param headerName the name of the header | ||
* @return the first value of the header with the given name, or null if the header is not present | ||
*/ | ||
@Override | ||
public String header(final String headerName) { | ||
List<String> headerValues = this.headers(headerName); | ||
if (headerValues == null || headerValues.isEmpty()) { | ||
return null; | ||
} | ||
if (headerValues.size() == 1) { | ||
return headerValues.get(0); | ||
} else { | ||
return String.join(",", headerValues); | ||
} | ||
} | ||
|
||
/** | ||
* Was the request successful? | ||
* | ||
* @return true if the status code is in the range [200, 400) | ||
*/ | ||
@Override | ||
public boolean isSuccessful() { | ||
return this.statusCode() >= HTTP_OK && this.statusCode() < HTTP_BAD_REQUEST; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*- | ||
* -\-\- | ||
* github-api | ||
* -- | ||
* Copyright (C) 2021 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.github.http; | ||
|
||
import com.spotify.github.tracing.Tracer; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface HttpClient { | ||
CompletableFuture<HttpResponse> send(HttpRequest request); | ||
void setTracer(Tracer tracer); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*- | ||
* -\-\- | ||
* github-api | ||
* -- | ||
* Copyright (C) 2021 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.github.http; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.spotify.github.GithubStyle; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.immutables.value.Value; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@Value.Immutable | ||
@GithubStyle | ||
@JsonSerialize(as = ImmutableHttpRequest.class) | ||
@JsonDeserialize(as = ImmutableHttpRequest.class) | ||
public interface HttpRequest { | ||
@Value.Default | ||
default String method() { | ||
return "GET"; | ||
} | ||
|
||
String url(); | ||
|
||
@Nullable | ||
String body(); | ||
|
||
@Value.Default | ||
default Map<String, List<String>> headers() { | ||
return Map.of(); | ||
} | ||
|
||
default List<String> headers(String headerName) { | ||
return headers().get(headerName); | ||
} | ||
|
||
default String header(String headerName) { | ||
List<String> headerValues = this.headers(headerName); | ||
if (headerValues == null || headerValues.isEmpty()) { | ||
return null; | ||
} | ||
if (headerValues.size() == 1) { | ||
return headerValues.get(0); | ||
} else { | ||
return String.join(",", headerValues); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*- | ||
* -\-\- | ||
* github-api | ||
* -- | ||
* Copyright (C) 2021 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.github.http; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface HttpResponse { | ||
// Returns the request that was sent to the server | ||
HttpRequest request(); | ||
// Returns the HTTP status code | ||
int statusCode(); | ||
// Returns the HTTP status message | ||
String statusMessage(); | ||
// Returns the response body as an InputStream | ||
InputStream body(); | ||
// Returns the response body as a String | ||
String bodyString(); | ||
// Returns the response headers as a Map | ||
Map<String, List<String>> headers(); | ||
// Returns the response headers for a specific header name as a list of Strings | ||
List<String> headers(String headerName); | ||
// Returns the response headers for a specific header name as a single String | ||
String header(String headerName); | ||
// Returns true if the response was successful | ||
boolean isSuccessful(); | ||
// Closes the response | ||
void close(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.