Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.7k
Adds a decorator for Okhttp requests#1013
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
hgschmie wants to merge1 commit intoscribejava:masterChoose a base branch fromhgschmie:okhttp-decorator
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 14 additions & 1 deletion...client-okhttp/src/main/java/com/github/scribejava/httpclient/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
16 changes: 16 additions & 0 deletions...-okhttp/src/main/java/com/github/scribejava/httpclient/okhttp/OkHttpHttpClientConfig.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
11 changes: 11 additions & 0 deletions...-okhttp/src/main/java/com/github/scribejava/httpclient/okhttp/OkHttpRequestDecorator.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,11 @@ | ||
package com.github.scribejava.httpclient.okhttp; | ||
import okhttp3.Request; | ||
/** | ||
* Allows interception of the request building process, e.g. to add custom headers or tags. | ||
*/ | ||
public interface OkHttpRequestDecorator { | ||
void decorate(Request.Builder requestBuilder); | ||
} |
97 changes: 97 additions & 0 deletions...ent-okhttp/src/test/java/com/github/scribejava/httpclient/okhttp/OkHttpDecoratorTest.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,97 @@ | ||
package com.github.scribejava.httpclient.okhttp; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.UUID; | ||
import com.github.scribejava.core.model.Response; | ||
import com.github.scribejava.core.model.Verb; | ||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request.Builder; | ||
import okhttp3.ResponseBody; | ||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
public class OkHttpDecoratorTest { | ||
@Test | ||
public void testHttpClient() throws Exception { | ||
final UUID trackId = UUID.randomUUID(); | ||
final OkHttpClient httpClient = new OkHttpClient.Builder() | ||
.addInterceptor(new TrackInterceptor(trackId)) | ||
.build(); | ||
final OkHttpHttpClient client = new OkHttpHttpClient(httpClient, new TrackDecorator(trackId)); | ||
final Response response = client.execute("", Collections.<String, String>emptyMap(), | ||
Verb.GET, "http://dummy/", (byte[]) null); | ||
assertNotNull(response); | ||
} | ||
@Test | ||
public void testHttpConfig() throws Exception { | ||
final UUID trackId = UUID.randomUUID(); | ||
final OkHttpClient.Builder builder = new OkHttpClient.Builder() | ||
.addInterceptor(new TrackInterceptor(trackId)); | ||
final OkHttpHttpClientConfig config = new OkHttpHttpClientConfig(builder) | ||
.withDecorator(new TrackDecorator(trackId)); | ||
final OkHttpHttpClient client = new OkHttpHttpClient(config); | ||
final Response response = client.execute("", Collections.<String, String>emptyMap(), | ||
Verb.GET, "http://dummy/", (byte[]) null); | ||
assertNotNull(response); | ||
} | ||
public static class TrackInterceptor implements Interceptor { | ||
private final UUID trackId; | ||
public TrackInterceptor(UUID trackId) { | ||
this.trackId = trackId; | ||
} | ||
@Override | ||
public okhttp3.Response intercept(Chain chain) throws IOException { | ||
final okhttp3.Request request = chain.request(); | ||
final UUID value = request.tag(UUID.class); | ||
assertNotNull(value); | ||
assertEquals(trackId, value); | ||
return new okhttp3.Response.Builder() | ||
.code(200) | ||
.message("OK") | ||
.body(ResponseBody.create("Nothing here", MediaType.parse("text/plain"))) | ||
.protocol(Protocol.HTTP_1_1) | ||
.request(request).build(); | ||
} | ||
} | ||
public static class TrackDecorator implements OkHttpRequestDecorator { | ||
private final UUID uuid; | ||
public TrackDecorator(UUID uuid) { | ||
this.uuid = uuid; | ||
} | ||
@Override | ||
public void decorate(Builder requestBuilder) { | ||
requestBuilder.tag(UUID.class, uuid); | ||
} | ||
} | ||
} |
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.