Skip to content

Commit d4be915

Browse files
author
adriancole
committed
update examples to feign 5.x
1 parent ec2f1ec commit d4be915

File tree

6 files changed

+31
-108
lines changed

6 files changed

+31
-108
lines changed

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

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@
1717

1818
import com.google.gson.Gson;
1919
import com.google.gson.JsonIOException;
20-
import com.google.gson.stream.JsonReader;
21-
import dagger.Module;
22-
import dagger.Provides;
2320
import feign.Feign;
2421
import feign.Logger;
2522
import feign.RequestLine;
2623
import feign.Response;
2724
import feign.codec.Decoder;
2825

29-
import javax.inject.Inject;
3026
import javax.inject.Named;
31-
import javax.inject.Singleton;
3227
import java.io.IOException;
3328
import java.io.Reader;
3429
import java.lang.reflect.Type;
@@ -51,8 +46,12 @@ static class Contributor {
5146
int contributions;
5247
}
5348

54-
public static void main(String... args) throws InterruptedException {
55-
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GitHubModule());
49+
public static void main(String... args) {
50+
GitHub github = Feign.builder()
51+
.logger(new Logger.ErrorLogger())
52+
.logLevel(Logger.Level.BASIC)
53+
.decoder(new GsonDecoder())
54+
.target(GitHub.class, "https://api.github.com");
5655

5756
System.out.println("Let's fetch and print a list of the contributors to this library.");
5857
List<Contributor> contributors = github.contributors("netflix", "feign");
@@ -61,60 +60,26 @@ public static void main(String... args) throws InterruptedException {
6160
}
6261
}
6362

64-
@Module(overrides = true, library = true, includes = GsonModule.class)
65-
static class GitHubModule {
66-
67-
@Provides Logger.Level loggingLevel() {
68-
return Logger.Level.BASIC;
69-
}
70-
71-
@Provides Logger logger() {
72-
return new Logger.ErrorLogger();
73-
}
74-
}
75-
7663
/**
77-
* Here's how it looks to wire json codecs. Note, that you can always instead use {@code feign-gson}!
64+
* Here's how it looks to write a decoder. Note: you can instead use {@code feign-gson}!
7865
*/
79-
@Module(library = true)
80-
static class GsonModule {
81-
82-
@Provides @Singleton Gson gson() {
83-
return new Gson();
84-
}
85-
86-
@Provides Decoder decoder(GsonDecoder gsonDecoder) {
87-
return gsonDecoder;
88-
}
89-
}
90-
9166
static class GsonDecoder implements Decoder {
92-
private final Gson gson;
93-
94-
@Inject GsonDecoder(Gson gson) {
95-
this.gson = gson;
96-
}
67+
private final Gson gson = new Gson();
9768

9869
@Override public Object decode(Response response, Type type) throws IOException {
99-
if (response.body() == null) {
70+
if (void.class == type || response.body() == null) {
10071
return null;
10172
}
10273
Reader reader = response.body().asReader();
10374
try {
104-
return fromJson(new JsonReader(reader), type);
105-
} finally {
106-
ensureClosed(reader);
107-
}
108-
}
109-
110-
private Object fromJson(JsonReader jsonReader, Type type) throws IOException {
111-
try {
112-
return gson.fromJson(jsonReader, type);
75+
return gson.fromJson(reader, type);
11376
} catch (JsonIOException e) {
11477
if (e.getCause() != null && e.getCause() instanceof IOException) {
11578
throw IOException.class.cast(e.getCause());
11679
}
11780
throw e;
81+
} finally {
82+
ensureClosed(reader);
11883
}
11984
}
12085
}

example-github/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'java'
22

33
dependencies {
4-
compile 'com.netflix.feign:feign-core:4.3.0'
5-
compile 'com.netflix.feign:feign-gson:4.3.0'
4+
compile 'com.netflix.feign:feign-core:5.0.0'
5+
compile 'com.netflix.feign:feign-gson:5.0.0'
66
provided 'com.squareup.dagger:dagger-compiler:1.1.0'
77
}
88

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

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import dagger.Provides;
2020
import feign.Feign;
2121
import feign.Logger;
22-
import feign.Observable;
23-
import feign.Observer;
2422
import feign.RequestLine;
2523
import feign.gson.GsonModule;
2624

2725
import javax.inject.Named;
2826
import java.util.List;
29-
import java.util.concurrent.CountDownLatch;
3027

3128
/**
3229
* adapted from {@code com.example.retrofit.GitHubClient}
@@ -36,9 +33,6 @@ public class GitHubExample {
3633
interface GitHub {
3734
@RequestLine("GET /repos/{owner}/{repo}/contributors")
3835
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
39-
40-
@RequestLine("GET /repos/{owner}/{repo}/contributors")
41-
Observable<Contributor> observable(@Named("owner") String owner, @Named("repo") String repo);
4236
}
4337

4438
static class Contributor {
@@ -54,48 +48,9 @@ public static void main(String... args) throws InterruptedException {
5448
for (Contributor contributor : contributors) {
5549
System.out.println(contributor.login + " (" + contributor.contributions + ")");
5650
}
57-
58-
System.out.println("Let's treat our contributors as an observable.");
59-
Observable<Contributor> observable = github.observable("netflix", "feign");
60-
61-
CountDownLatch latch = new CountDownLatch(2);
62-
63-
System.out.println("Let's add 2 subscribers.");
64-
observable.subscribe(new ContributorObserver(latch));
65-
observable.subscribe(new ContributorObserver(latch));
66-
67-
// wait for the task to complete.
68-
latch.await();
69-
70-
System.exit(0);
71-
}
72-
73-
static class ContributorObserver implements Observer<Contributor> {
74-
75-
private final CountDownLatch latch;
76-
public int count;
77-
78-
public ContributorObserver(CountDownLatch latch) {
79-
this.latch = latch;
80-
}
81-
82-
// parsed directly from the text stream without an intermediate collection.
83-
@Override public void onNext(Contributor contributor) {
84-
count++;
85-
}
86-
87-
@Override public void onSuccess() {
88-
System.out.println("found " + count + " contributors");
89-
latch.countDown();
90-
}
91-
92-
@Override public void onFailure(Throwable cause) {
93-
cause.printStackTrace();
94-
latch.countDown();
95-
}
9651
}
9752

98-
@Module(overrides = true, library = true)
53+
@Module(overrides = true, library = true, includes = GsonModule.class)
9954
static class LogToStderr {
10055

10156
@Provides Logger.Level loggingLevel() {

example-wikipedia/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'java'
22

33
dependencies {
4-
compile 'com.netflix.feign:feign-core:4.3.0'
5-
compile 'com.netflix.feign:feign-gson:4.3.0'
4+
compile 'com.netflix.feign:feign-core:5.0.0'
5+
compile 'com.netflix.feign:feign-gson:5.0.0'
66
provided 'com.squareup.dagger:dagger-compiler:1.1.0'
77
}
88

example-wikipedia/src/main/java/feign/example/wikipedia/ResponseDecoder.java renamed to example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package feign.example.wikipedia;
22

3+
import com.google.gson.TypeAdapter;
34
import com.google.gson.stream.JsonReader;
4-
import feign.codec.Decoder;
5+
import com.google.gson.stream.JsonWriter;
56

67
import java.io.IOException;
7-
import java.io.Reader;
8-
import java.lang.reflect.Type;
98

10-
abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample.Response<X>> {
9+
abstract class ResponseAdapter<X> extends TypeAdapter<WikipediaExample.Response<X>> {
1110

1211
/**
1312
* 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
3534
* the wikipedia api doesn't use json arrays, rather a series of nested objects.
3635
*/
3736
@Override
38-
public WikipediaExample.Response<X> decode(Reader ireader, Type type) throws IOException {
37+
public WikipediaExample.Response<X> read(JsonReader reader) throws IOException {
3938
WikipediaExample.Response<X> pages = new WikipediaExample.Response<X>();
40-
JsonReader reader = new JsonReader(ireader);
4139
reader.beginObject();
4240
while (reader.hasNext()) {
4341
String nextName = reader.nextName();
@@ -84,4 +82,9 @@ public WikipediaExample.Response<X> decode(Reader ireader, Type type) throws IOE
8482
reader.close();
8583
return pages;
8684
}
85+
86+
@Override
87+
public void write(JsonWriter out, WikipediaExample.Response<X> response) throws IOException {
88+
throw new UnsupportedOperationException();
89+
}
8790
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
package feign.example.wikipedia;
1717

18+
import com.google.gson.TypeAdapter;
1819
import com.google.gson.stream.JsonReader;
1920
import dagger.Module;
2021
import dagger.Provides;
2122
import feign.Feign;
2223
import feign.Logger;
2324
import feign.RequestLine;
24-
import feign.codec.Decoder;
2525
import feign.gson.GsonModule;
2626

2727
import javax.inject.Named;
@@ -101,14 +101,14 @@ public void remove() {
101101
};
102102
}
103103

104-
@Module(library = true, includes = GsonModule.class)
104+
@Module(includes = GsonModule.class)
105105
static class WikipediaDecoder {
106106

107107
/**
108-
* add to the set of Decoders one that handles {@code Response<Page>}.
108+
* registers a gson {@link TypeAdapter} for {@code Response<Page>}.
109109
*/
110-
@Provides(type = SET) Decoder pagesDecoder() {
111-
return new ResponseDecoder<Page>() {
110+
@Provides(type = SET) TypeAdapter pagesAdapter() {
111+
return new ResponseAdapter<Page>() {
112112

113113
@Override
114114
protected String query() {

0 commit comments

Comments
 (0)