diff --git a/core/src/test/java/feign/examples/GitHubExample.java b/core/src/test/java/feign/examples/GitHubExample.java index 02223ad5..c52308d5 100644 --- a/core/src/test/java/feign/examples/GitHubExample.java +++ b/core/src/test/java/feign/examples/GitHubExample.java @@ -17,18 +17,13 @@ package feign.examples; import com.google.gson.Gson; import com.google.gson.JsonIOException; -import com.google.gson.stream.JsonReader; -import dagger.Module; -import dagger.Provides; import feign.Feign; import feign.Logger; import feign.RequestLine; import feign.Response; import feign.codec.Decoder; -import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Singleton; import java.io.IOException; import java.io.Reader; import java.lang.reflect.Type; @@ -51,8 +46,12 @@ public class GitHubExample { int contributions; } - public static void main(String... args) throws InterruptedException { - GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GitHubModule()); + public static void main(String... args) { + GitHub github = Feign.builder() + .logger(new Logger.ErrorLogger()) + .logLevel(Logger.Level.BASIC) + .decoder(new GsonDecoder()) + .target(GitHub.class, "https://api.github.com"); System.out.println("Let's fetch and print a list of the contributors to this library."); List contributors = github.contributors("netflix", "feign"); @@ -61,60 +60,26 @@ public class GitHubExample { } } - @Module(overrides = true, library = true, includes = GsonModule.class) - static class GitHubModule { - - @Provides Logger.Level loggingLevel() { - return Logger.Level.BASIC; - } - - @Provides Logger logger() { - return new Logger.ErrorLogger(); - } - } - /** - * Here's how it looks to wire json codecs. Note, that you can always instead use {@code feign-gson}! + * Here's how it looks to write a decoder. Note: you can instead use {@code feign-gson}! */ - @Module(library = true) - static class GsonModule { - - @Provides @Singleton Gson gson() { - return new Gson(); - } - - @Provides Decoder decoder(GsonDecoder gsonDecoder) { - return gsonDecoder; - } - } - static class GsonDecoder implements Decoder { - private final Gson gson; - - @Inject GsonDecoder(Gson gson) { - this.gson = gson; - } + private final Gson gson = new Gson(); @Override public Object decode(Response response, Type type) throws IOException { - if (response.body() == null) { + if (void.class == type || response.body() == null) { return null; } Reader reader = response.body().asReader(); try { - return fromJson(new JsonReader(reader), type); - } finally { - ensureClosed(reader); - } - } - - private Object fromJson(JsonReader jsonReader, Type type) throws IOException { - try { - return gson.fromJson(jsonReader, type); + return gson.fromJson(reader, type); } catch (JsonIOException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw IOException.class.cast(e.getCause()); } throw e; + } finally { + ensureClosed(reader); } } } diff --git a/example-github/build.gradle b/example-github/build.gradle index 126b8632..24049dc0 100644 --- a/example-github/build.gradle +++ b/example-github/build.gradle @@ -1,8 +1,8 @@ apply plugin: 'java' dependencies { - compile 'com.netflix.feign:feign-core:4.3.0' - compile 'com.netflix.feign:feign-gson:4.3.0' + compile 'com.netflix.feign:feign-core:5.0.0' + compile 'com.netflix.feign:feign-gson:5.0.0' provided 'com.squareup.dagger:dagger-compiler:1.1.0' } diff --git a/example-github/src/main/java/feign/example/github/GitHubExample.java b/example-github/src/main/java/feign/example/github/GitHubExample.java index 6f897791..900bfc18 100644 --- a/example-github/src/main/java/feign/example/github/GitHubExample.java +++ b/example-github/src/main/java/feign/example/github/GitHubExample.java @@ -19,14 +19,11 @@ import dagger.Module; import dagger.Provides; import feign.Feign; import feign.Logger; -import feign.Observable; -import feign.Observer; import feign.RequestLine; import feign.gson.GsonModule; import javax.inject.Named; import java.util.List; -import java.util.concurrent.CountDownLatch; /** * adapted from {@code com.example.retrofit.GitHubClient} @@ -36,9 +33,6 @@ public class GitHubExample { interface GitHub { @RequestLine("GET /repos/{owner}/{repo}/contributors") List contributors(@Named("owner") String owner, @Named("repo") String repo); - - @RequestLine("GET /repos/{owner}/{repo}/contributors") - Observable observable(@Named("owner") String owner, @Named("repo") String repo); } static class Contributor { @@ -54,48 +48,9 @@ public class GitHubExample { for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } - - System.out.println("Let's treat our contributors as an observable."); - Observable observable = github.observable("netflix", "feign"); - - CountDownLatch latch = new CountDownLatch(2); - - System.out.println("Let's add 2 subscribers."); - observable.subscribe(new ContributorObserver(latch)); - observable.subscribe(new ContributorObserver(latch)); - - // wait for the task to complete. - latch.await(); - - System.exit(0); - } - - static class ContributorObserver implements Observer { - - private final CountDownLatch latch; - public int count; - - public ContributorObserver(CountDownLatch latch) { - this.latch = latch; - } - - // parsed directly from the text stream without an intermediate collection. - @Override public void onNext(Contributor contributor) { - count++; - } - - @Override public void onSuccess() { - System.out.println("found " + count + " contributors"); - latch.countDown(); - } - - @Override public void onFailure(Throwable cause) { - cause.printStackTrace(); - latch.countDown(); - } } - @Module(overrides = true, library = true) + @Module(overrides = true, library = true, includes = GsonModule.class) static class LogToStderr { @Provides Logger.Level loggingLevel() { diff --git a/example-wikipedia/build.gradle b/example-wikipedia/build.gradle index 816eda64..73c6b996 100644 --- a/example-wikipedia/build.gradle +++ b/example-wikipedia/build.gradle @@ -1,8 +1,8 @@ apply plugin: 'java' dependencies { - compile 'com.netflix.feign:feign-core:4.3.0' - compile 'com.netflix.feign:feign-gson:4.3.0' + compile 'com.netflix.feign:feign-core:5.0.0' + compile 'com.netflix.feign:feign-gson:5.0.0' provided 'com.squareup.dagger:dagger-compiler:1.1.0' } diff --git a/example-wikipedia/src/main/java/feign/example/wikipedia/ResponseDecoder.java b/example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java similarity index 85% rename from example-wikipedia/src/main/java/feign/example/wikipedia/ResponseDecoder.java rename to example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java index 9cb54bba..e202cc10 100644 --- a/example-wikipedia/src/main/java/feign/example/wikipedia/ResponseDecoder.java +++ b/example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java @@ -1,13 +1,12 @@ package feign.example.wikipedia; +import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; -import feign.codec.Decoder; +import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.io.Reader; -import java.lang.reflect.Type; -abstract class ResponseDecoder implements Decoder.TextStream> { +abstract class ResponseAdapter extends TypeAdapter> { /** * name of the key inside the {@code query} dict which holds the elements desired. ex. {@code pages}. @@ -35,9 +34,8 @@ abstract class ResponseDecoder implements Decoder.TextStream decode(Reader ireader, Type type) throws IOException { + public WikipediaExample.Response read(JsonReader reader) throws IOException { WikipediaExample.Response pages = new WikipediaExample.Response(); - JsonReader reader = new JsonReader(ireader); reader.beginObject(); while (reader.hasNext()) { String nextName = reader.nextName(); @@ -84,4 +82,9 @@ abstract class ResponseDecoder implements Decoder.TextStream response) throws IOException { + throw new UnsupportedOperationException(); + } } diff --git a/example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java b/example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java index 90ee6916..feb57121 100644 --- a/example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java +++ b/example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java @@ -15,13 +15,13 @@ */ package feign.example.wikipedia; +import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import dagger.Module; import dagger.Provides; import feign.Feign; import feign.Logger; import feign.RequestLine; -import feign.codec.Decoder; import feign.gson.GsonModule; import javax.inject.Named; @@ -101,14 +101,14 @@ public class WikipediaExample { }; } - @Module(library = true, includes = GsonModule.class) + @Module(includes = GsonModule.class) static class WikipediaDecoder { /** - * add to the set of Decoders one that handles {@code Response}. + * registers a gson {@link TypeAdapter} for {@code Response}. */ - @Provides(type = SET) Decoder pagesDecoder() { - return new ResponseDecoder() { + @Provides(type = SET) TypeAdapter pagesAdapter() { + return new ResponseAdapter() { @Override protected String query() {