Skip to content

Commit 5fb1076

Browse files
author
adriancole
committed
ported example to use latest and greatest
1 parent 13e23e5 commit 5fb1076

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

examples/feign-example-cli/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:2.0.0'
5-
compile 'com.google.code.gson:gson:2.2.4'
4+
compile 'com.netflix.feign:feign-core:3.0.0'
5+
compile 'com.netflix.feign:feign-gson:3.0.0'
66
provided 'com.squareup.dagger:dagger-compiler:1.0.1'
77
}
88

examples/feign-example-cli/src/main/java/feign/example/cli/GitHubExample.java

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,14 @@
1515
*/
1616
package feign.example.cli;
1717

18-
import com.google.gson.Gson;
19-
20-
import java.io.Reader;
21-
import java.lang.reflect.Type;
22-
import java.util.LinkedHashMap;
23-
import java.util.List;
24-
import java.util.Map;
25-
26-
import javax.inject.Named;
27-
import javax.inject.Singleton;
28-
29-
import dagger.Module;
30-
import dagger.Provides;
3118
import feign.Feign;
19+
import feign.IncrementalCallback;
3220
import feign.RequestLine;
33-
import feign.codec.Decoder;
21+
import feign.gson.GsonModule;
22+
23+
import javax.inject.Named;
24+
import java.util.List;
25+
import java.util.concurrent.CountDownLatch;
3426

3527
/**
3628
* adapted from {@code com.example.retrofit.GitHubClient}
@@ -40,40 +32,56 @@ public class GitHubExample {
4032
interface GitHub {
4133
@RequestLine("GET /repos/{owner}/{repo}/contributors")
4234
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
35+
36+
@RequestLine("GET /repos/{owner}/{repo}/contributors")
37+
void contributors(@Named("owner") String owner, @Named("repo") String repo,
38+
IncrementalCallback<Contributor> contributors);
4339
}
4440

4541
static class Contributor {
4642
String login;
4743
int contributions;
4844
}
4945

50-
public static void main(String... args) {
46+
public static void main(String... args) throws InterruptedException {
5147
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
5248

53-
// Fetch and print a list of the contributors to this library.
49+
System.out.println("Let's fetch and print a list of the contributors to this library.");
5450
List<Contributor> contributors = github.contributors("netflix", "feign");
5551
for (Contributor contributor : contributors) {
5652
System.out.println(contributor.login + " (" + contributor.contributions + ")");
5753
}
58-
}
5954

60-
/**
61-
* Here's how to wire gson deserialization.
62-
*/
63-
@Module(overrides = true, library = true)
64-
static class GsonModule {
65-
@Provides @Singleton Map<String, Decoder> decoders() {
66-
Map<String, Decoder> decoders = new LinkedHashMap<String, Decoder>();
67-
decoders.put("GitHub", jsonDecoder);
68-
return decoders;
69-
}
55+
final CountDownLatch latch = new CountDownLatch(1);
56+
57+
System.out.println("Now, let's do it as an incremental async task.");
58+
IncrementalCallback<Contributor> task = new IncrementalCallback<Contributor>() {
59+
60+
public int count;
61+
62+
// parsed directly from the text stream without an intermediate collection.
63+
@Override public void onNext(Contributor contributor) {
64+
System.out.println(contributor.login + " (" + contributor.contributions + ")");
65+
count++;
66+
}
7067

71-
final Decoder jsonDecoder = new Decoder() {
72-
Gson gson = new Gson();
68+
@Override public void onSuccess() {
69+
System.out.println("found " + count + " contributors");
70+
latch.countDown();
71+
}
7372

74-
@Override public Object decode(String methodKey, Reader reader, Type type) {
75-
return gson.fromJson(reader, type);
73+
@Override public void onFailure(Throwable cause) {
74+
cause.printStackTrace();
75+
latch.countDown();
7676
}
7777
};
78+
79+
// fire a task in the background.
80+
github.contributors("netflix", "feign", task);
81+
82+
// wait for the task to complete.
83+
latch.await();
84+
85+
System.exit(0);
7886
}
7987
}

0 commit comments

Comments
 (0)