-
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 5 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
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,43 @@ | ||
/*- | ||
* -\-\- | ||
* 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 org.immutables.value.Value; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Value.Immutable | ||
@GithubStyle | ||
@JsonSerialize(as = ImmutableHttpRequest.class) | ||
@JsonDeserialize(as = ImmutableHttpRequest.class) | ||
public interface HttpRequest { | ||
String method(); | ||
|
||
String url(); | ||
|
||
String body(); | ||
|
||
Map<String, List<String>> headers(); | ||
} |
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,36 @@ | ||
/*- | ||
* -\-\- | ||
* 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 { | ||
HttpRequest request(); | ||
int statusCode(); | ||
String statusMessage(); | ||
InputStream body(); | ||
String bodyString(); | ||
Map<String, List<String>> headers(); | ||
boolean isSuccessful(); | ||
void close(); | ||
} |
191 changes: 191 additions & 0 deletions
191
src/main/java/com/spotify/github/http/okhttp/OkHttpHttpClient.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,191 @@ | ||
/*- | ||
* -\-\- | ||
* 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.okhttp; | ||
|
||
import static okhttp3.MediaType.parse; | ||
|
||
import com.spotify.github.http.HttpClient; | ||
import com.spotify.github.http.HttpRequest; | ||
import com.spotify.github.http.HttpResponse; | ||
import com.spotify.github.http.ImmutableHttpRequest; | ||
import com.spotify.github.tracing.NoopTracer; | ||
import com.spotify.github.tracing.Span; | ||
import com.spotify.github.tracing.TraceHelper; | ||
import com.spotify.github.tracing.Tracer; | ||
import com.spotify.github.tracing.opencensus.OpenCensusTracer; | ||
import com.spotify.github.tracing.opentelemetry.OpenTelemetryTracer; | ||
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTelemetry; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import okhttp3.*; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class OkHttpHttpClient implements HttpClient { | ||
private final OkHttpClient client; | ||
private Tracer tracer; | ||
private Call.Factory callFactory; | ||
|
||
public OkHttpHttpClient(final OkHttpClient client) { | ||
this.client = client; | ||
this.tracer = NoopTracer.INSTANCE; | ||
this.callFactory = createTracedClient(); | ||
} | ||
|
||
public OkHttpHttpClient(final OkHttpClient client, final Tracer tracer) { | ||
this.client = client; | ||
this.tracer = tracer; | ||
this.callFactory = createTracedClient(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<HttpResponse> send(final HttpRequest httpRequest) { | ||
Request request = buildRequest(httpRequest); | ||
CompletableFuture<HttpResponse> future = new CompletableFuture<>(); | ||
try (Span span = tracer.span(httpRequest)) { | ||
if (this.callFactory == null) { | ||
this.callFactory = createTracedClient(); | ||
} | ||
this.callFactory | ||
.newCall(request) | ||
.enqueue( | ||
new Callback() { | ||
|
||
@Override | ||
public void onResponse(@NotNull final Call call, @NotNull final Response response) | ||
throws IOException { | ||
future.complete(new OkHttpHttpResponse(httpRequest, response)); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NotNull final Call call, @NotNull final IOException e) { | ||
future.completeExceptionally(e); | ||
} | ||
}); | ||
tracer.attachSpanToFuture(span, future); | ||
} | ||
return future; | ||
} | ||
|
||
@Override | ||
public void setTracer(final Tracer tracer) { | ||
this.tracer = tracer; | ||
this.callFactory = createTracedClient(); | ||
} | ||
|
||
private Request buildRequest(final HttpRequest request) { | ||
Request.Builder requestBuilder = new Request.Builder().url(request.url()); | ||
request | ||
.headers() | ||
.forEach( | ||
(key, values) -> { | ||
values.forEach(value -> requestBuilder.addHeader(key, value)); | ||
}); | ||
if (request.method().equals("GET")) { | ||
requestBuilder.get(); | ||
} else { | ||
requestBuilder.method( | ||
request.method(), | ||
RequestBody.create(parse(javax.ws.rs.core.MediaType.APPLICATION_JSON), request.body())); | ||
} | ||
return requestBuilder.build(); | ||
} | ||
|
||
private HttpRequest buildHttpRequest(final Request request) { | ||
return ImmutableHttpRequest.builder() | ||
.url(request.url().toString()) | ||
.method(request.method()) | ||
.headers(request.headers().toMultimap()) | ||
.body(Optional.ofNullable(request.body()).map(RequestBody::toString).orElse("")) | ||
.build(); | ||
} | ||
|
||
private Call.Factory createTracedClient() { | ||
if (this.tracer == null || this.tracer instanceof NoopTracer) { | ||
return createTracedClientNoopTracer(); | ||
} | ||
if (this.tracer instanceof OpenCensusTracer) { | ||
return createTracedClientOpenCensus(); | ||
} | ||
if (this.tracer instanceof OpenTelemetryTracer) { | ||
return createTracedClientOpenTelemetry(); | ||
} | ||
return createTracedClientNoopTracer(); | ||
} | ||
|
||
private Call.Factory createTracedClientNoopTracer() { | ||
return new Call.Factory() { | ||
@NotNull | ||
@Override | ||
public Call newCall(@NotNull final Request request) { | ||
return client.newCall(request); | ||
} | ||
}; | ||
} | ||
|
||
private Call.Factory createTracedClientOpenTelemetry() { | ||
return OkHttpTelemetry.builder(((OpenTelemetryTracer) this.tracer).getOpenTelemetry()) | ||
.build() | ||
.newCallFactory(client); | ||
} | ||
|
||
private Call.Factory createTracedClientOpenCensus() { | ||
return new Call.Factory() { | ||
@NotNull | ||
@Override | ||
public Call newCall(@NotNull final Request request) { | ||
CompletableFuture<Response> future = new CompletableFuture<>(); | ||
Span span = | ||
OkHttpHttpClient.this | ||
.tracer | ||
.span(buildHttpRequest(request)) | ||
.addTag(TraceHelper.TraceTags.HTTP_URL, request.url().toString()); | ||
OkHttpClient.Builder okBuilder = client.newBuilder(); | ||
okBuilder | ||
.networkInterceptors() | ||
.add( | ||
0, | ||
new Interceptor() { | ||
@NotNull | ||
@Override | ||
public Response intercept(@NotNull final Chain chain) throws IOException { | ||
try { | ||
Response response = chain.proceed(chain.request()); | ||
span.addTag(TraceHelper.TraceTags.HTTP_STATUS_CODE, response.code()) | ||
.addTag(TraceHelper.TraceTags.HTTP_STATUS_MESSAGE, response.message()) | ||
.success(); | ||
future.complete(response); | ||
return response; | ||
} catch (Exception ex) { | ||
span.failure(ex); | ||
future.completeExceptionally(ex); | ||
throw ex; | ||
} finally { | ||
span.close(); | ||
} | ||
} | ||
}); | ||
|
||
return okBuilder.build().newCall(request); | ||
} | ||
}; | ||
} | ||
} |
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.