Browse Source

Merge pull request #71 from Netflix/examples-5.x

update examples to feign 5.x
pull/72/merge
Adrian Cole 11 years ago
parent
commit
ea0e4e137c
  1. 59
      core/src/test/java/feign/examples/GitHubExample.java
  2. 4
      example-github/build.gradle
  3. 47
      example-github/src/main/java/feign/example/github/GitHubExample.java
  4. 4
      example-wikipedia/build.gradle
  5. 15
      example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java
  6. 10
      example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java

59
core/src/test/java/feign/examples/GitHubExample.java

@ -17,18 +17,13 @@ package feign.examples; @@ -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 { @@ -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<Contributor> contributors = github.contributors("netflix", "feign");
@ -61,60 +60,26 @@ public class GitHubExample { @@ -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);
}
}
}

4
example-github/build.gradle

@ -1,8 +1,8 @@ @@ -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'
}

47
example-github/src/main/java/feign/example/github/GitHubExample.java

@ -19,14 +19,11 @@ import dagger.Module; @@ -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 { @@ -36,9 +33,6 @@ public class GitHubExample {
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
@RequestLine("GET /repos/{owner}/{repo}/contributors")
Observable<Contributor> observable(@Named("owner") String owner, @Named("repo") String repo);
}
static class Contributor {
@ -54,48 +48,9 @@ public class GitHubExample { @@ -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<Contributor> 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<Contributor> {
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() {

4
example-wikipedia/build.gradle

@ -1,8 +1,8 @@ @@ -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'
}

15
example-wikipedia/src/main/java/feign/example/wikipedia/ResponseDecoder.java → example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java

@ -1,13 +1,12 @@ @@ -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<X> implements Decoder.TextStream<WikipediaExample.Response<X>> {
abstract class ResponseAdapter<X> extends TypeAdapter<WikipediaExample.Response<X>> {
/**
* name of the key inside the {@code query} dict which holds the elements desired. ex. {@code pages}.
@ -35,9 +34,8 @@ abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample @@ -35,9 +34,8 @@ abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample
* the wikipedia api doesn't use json arrays, rather a series of nested objects.
*/
@Override
public WikipediaExample.Response<X> decode(Reader ireader, Type type) throws IOException {
public WikipediaExample.Response<X> read(JsonReader reader) throws IOException {
WikipediaExample.Response<X> pages = new WikipediaExample.Response<X>();
JsonReader reader = new JsonReader(ireader);
reader.beginObject();
while (reader.hasNext()) {
String nextName = reader.nextName();
@ -84,4 +82,9 @@ abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample @@ -84,4 +82,9 @@ abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample
reader.close();
return pages;
}
@Override
public void write(JsonWriter out, WikipediaExample.Response<X> response) throws IOException {
throw new UnsupportedOperationException();
}
}

10
example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java

@ -15,13 +15,13 @@ @@ -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 { @@ -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<Page>}.
* registers a gson {@link TypeAdapter} for {@code Response<Page>}.
*/
@Provides(type = SET) Decoder pagesDecoder() {
return new ResponseDecoder<Page>() {
@Provides(type = SET) TypeAdapter pagesAdapter() {
return new ResponseAdapter<Page>() {
@Override
protected String query() {

Loading…
Cancel
Save