Skip to content

Commit 167f143

Browse files
author
adriancole
committed
update examples to feign 5.x
1 parent 9452565 commit 167f143

6 files changed

Lines changed: 32 additions & 120 deletions

File tree

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

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import com.google.gson.Gson;
2121
import com.google.gson.JsonIOException;
22-
import com.google.gson.stream.JsonReader;
23-
import dagger.Module;
24-
import dagger.Provides;
2522
import feign.Feign;
2623
import feign.Logger;
2724
import feign.RequestLine;
@@ -31,9 +28,7 @@
3128
import java.io.Reader;
3229
import java.lang.reflect.Type;
3330
import java.util.List;
34-
import javax.inject.Inject;
3531
import javax.inject.Named;
36-
import javax.inject.Singleton;
3732

3833
/** adapted from {@code com.example.retrofit.GitHubClient} */
3934
public class GitHubExample {
@@ -48,8 +43,13 @@ static class Contributor {
4843
int contributions;
4944
}
5045

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

5454
System.out.println("Let's fetch and print a list of the contributors to this library.");
5555
List<Contributor> contributors = github.contributors("netflix", "feign");
@@ -58,68 +58,25 @@ public static void main(String... args) throws InterruptedException {
5858
}
5959
}
6060

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

10265
@Override
10366
public Object decode(Response response, Type type) throws IOException {
104-
if (response.body() == null) {
67+
if (void.class == type || response.body() == null) {
10568
return null;
10669
}
10770
Reader reader = response.body().asReader();
10871
try {
109-
return fromJson(new JsonReader(reader), type);
110-
} finally {
111-
ensureClosed(reader);
112-
}
113-
}
114-
115-
private Object fromJson(JsonReader jsonReader, Type type) throws IOException {
116-
try {
117-
return gson.fromJson(jsonReader, type);
72+
return gson.fromJson(reader, type);
11873
} catch (JsonIOException e) {
11974
if (e.getCause() != null && e.getCause() instanceof IOException) {
12075
throw IOException.class.cast(e.getCause());
12176
}
12277
throw e;
78+
} finally {
79+
ensureClosed(reader);
12380
}
12481
}
12582
}

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 & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
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
import java.util.List;
27-
import java.util.concurrent.CountDownLatch;
2825
import javax.inject.Named;
2926

3027
/** adapted from {@code com.example.retrofit.GitHubClient} */
@@ -33,9 +30,6 @@ public class GitHubExample {
3330
interface GitHub {
3431
@RequestLine("GET /repos/{owner}/{repo}/contributors")
3532
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
36-
37-
@RequestLine("GET /repos/{owner}/{repo}/contributors")
38-
Observable<Contributor> observable(@Named("owner") String owner, @Named("repo") String repo);
3933
}
4034

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

99-
@Module(overrides = true, library = true)
51+
@Module(overrides = true, library = true, includes = GsonModule.class)
10052
static class LogToStderr {
10153

10254
@Provides

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,12 +1,11 @@
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
import java.io.IOException;
6-
import java.io.Reader;
7-
import java.lang.reflect.Type;
87

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

1110
/**
1211
* name of the key inside the {@code query} dict which holds the elements desired. ex. {@code
@@ -35,9 +34,8 @@ abstract class ResponseDecoder<X> implements Decoder.TextStream<WikipediaExample
3534

3635
/** the wikipedia api doesn't use json arrays, rather a series of nested objects. */
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
@@ -17,13 +17,13 @@
1717

1818
import static dagger.Provides.Type.SET;
1919

20+
import com.google.gson.TypeAdapter;
2021
import com.google.gson.stream.JsonReader;
2122
import dagger.Module;
2223
import dagger.Provides;
2324
import feign.Feign;
2425
import feign.Logger;
2526
import feign.RequestLine;
26-
import feign.codec.Decoder;
2727
import feign.gson.GsonModule;
2828
import java.io.IOException;
2929
import java.util.ArrayList;
@@ -100,13 +100,13 @@ public void remove() {
100100
};
101101
}
102102

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

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

111111
@Override
112112
protected String query() {

0 commit comments

Comments
 (0)