Browse Source

Merge pull request #38 from Netflix/example-observable

update github example to use feign 4.0 observable
pull/40/merge
Adrian Cole 11 years ago
parent
commit
8793ae3d3c
  1. 4
      examples/feign-example-cli/build.gradle
  2. 76
      examples/feign-example-cli/src/main/java/feign/example/cli/GitHubExample.java

4
examples/feign-example-cli/build.gradle

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
apply plugin: 'java'
dependencies {
compile 'com.netflix.feign:feign-core:3.0.0'
compile 'com.netflix.feign:feign-gson:3.0.0'
compile 'com.netflix.feign:feign-core:4.0.0'
compile 'com.netflix.feign:feign-gson:4.0.0'
provided 'com.squareup.dagger:dagger-compiler:1.0.1'
}

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

@ -15,8 +15,12 @@ @@ -15,8 +15,12 @@
*/
package feign.example.cli;
import dagger.Module;
import dagger.Provides;
import feign.Feign;
import feign.IncrementalCallback;
import feign.Logger;
import feign.Observable;
import feign.Observer;
import feign.RequestLine;
import feign.gson.GsonModule;
@ -34,8 +38,7 @@ public class GitHubExample { @@ -34,8 +38,7 @@ public class GitHubExample {
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo);
@RequestLine("GET /repos/{owner}/{repo}/contributors")
void contributors(@Named("owner") String owner, @Named("repo") String repo,
IncrementalCallback<Contributor> contributors);
Observable<Contributor> observable(@Named("owner") String owner, @Named("repo") String repo);
}
static class Contributor {
@ -44,7 +47,7 @@ public class GitHubExample { @@ -44,7 +47,7 @@ public class GitHubExample {
}
public static void main(String... args) throws InterruptedException {
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GitHubModule());
System.out.println("Let's fetch and print a list of the contributors to this library.");
List<Contributor> contributors = github.contributors("netflix", "feign");
@ -52,36 +55,55 @@ public class GitHubExample { @@ -52,36 +55,55 @@ public class GitHubExample {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
final CountDownLatch latch = new CountDownLatch(1);
System.out.println("Let's treat our contributors as an observable.");
Observable<Contributor> observable = github.observable("netflix", "feign");
System.out.println("Now, let's do it as an incremental async task.");
IncrementalCallback<Contributor> task = new IncrementalCallback<Contributor>() {
CountDownLatch latch = new CountDownLatch(2);
public int count;
System.out.println("Let's add 2 subscribers.");
observable.subscribe(new ContributorObserver(latch));
observable.subscribe(new ContributorObserver(latch));
// parsed directly from the text stream without an intermediate collection.
@Override public void onNext(Contributor contributor) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
count++;
}
// wait for the task to complete.
latch.await();
@Override public void onSuccess() {
System.out.println("found " + count + " contributors");
latch.countDown();
}
System.exit(0);
}
@Override public void onFailure(Throwable cause) {
cause.printStackTrace();
latch.countDown();
}
};
static class ContributorObserver implements Observer<Contributor> {
// fire a task in the background.
github.contributors("netflix", "feign", task);
private final CountDownLatch latch;
public int count;
// wait for the task to complete.
latch.await();
public ContributorObserver(CountDownLatch latch) {
this.latch = latch;
}
System.exit(0);
// 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, includes = GsonModule.class)
static class GitHubModule {
@Provides Logger.Level loggingLevel() {
return Logger.Level.BASIC;
}
@Provides Logger logger() {
return new Logger.ErrorLogger();
}
}
}

Loading…
Cancel
Save