Browse Source

Merge pull request #6 from Netflix/cli-example

added cli example
pull/4/merge
Adrian Cole 12 years ago
parent
commit
4315ff7abc
  1. 1
      CHANGES.md
  2. 49
      examples/feign-example-cli/build.gradle
  3. 78
      examples/feign-example-cli/src/main/java/feign/example/cli/GitHubExample.java
  4. 2
      settings.gradle

1
CHANGES.md

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
### Version 1.1.0
* adds Ribbon integration
* adds cli example
* exponential backoff customizable via Retryer.Default ctor
### Version 1.0.0

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

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
apply plugin: 'java'
dependencies {
compile 'com.netflix.feign:feign-core:1.0.0'
compile 'com.google.code.gson:gson:2.2.4'
provided 'com.squareup.dagger:dagger-compiler:1.0.1'
}
// create a self-contained jar that is executable
// the output is both a 'fat' project artifact and
// a convenience file named "build/github"
task fatJar(dependsOn: classes, type: Jar) {
classifier 'fat'
doFirst {
// Delay evaluation until the compile configuration is ready
from {
configurations.compile.collect { zipTree(it) }
}
}
from (sourceSets*.output.classesDir) {
}
// really executable jar
// http://skife.org/java/unix/2011/06/20/really_executable_jars.html
manifest {
attributes 'Main-Class': 'feign.example.cli.GitHubExample'
}
// for convenience, we make a file in the build dir named github with no extension
doLast {
def srcFile = new File("${buildDir}/libs/${archiveName}")
def shortcutFile = new File("${buildDir}/github")
shortcutFile.delete()
shortcutFile << "#!/usr/bin/env sh\n"
shortcutFile << 'exec java -jar $0 "$@"' + "\n"
shortcutFile << srcFile.bytes
shortcutFile.setExecutable(true, true)
srcFile.delete()
srcFile << shortcutFile.bytes
srcFile.setExecutable(true, true)
}
}
artifacts {
archives fatJar
}

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

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign.example.cli;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.io.Reader;
import java.util.List;
import java.util.Map;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import dagger.Module;
import dagger.Provides;
import feign.Feign;
import feign.codec.Decoder;
/**
* adapted from {@code com.example.retrofit.GitHubClient}
*/
public class GitHubExample {
interface GitHub {
@GET @Path("/repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@PathParam("owner") String owner, @PathParam("repo") String repo);
}
static class Contributor {
String login;
int contributions;
}
public static void main(String... args) {
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
// Fetch and print a list of the contributors to this library.
List<Contributor> contributors = github.contributors("netflix", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
/**
* Here's how to wire gson deserialization.
*/
@Module(overrides = true, library = true)
static class GsonModule {
@Provides @Singleton Map<String, Decoder> decoders() {
return ImmutableMap.of("GitHub", jsonDecoder);
}
final Decoder jsonDecoder = new Decoder() {
Gson gson = new Gson();
@Override public Object decode(String methodKey, Reader reader, TypeToken<?> type) {
return gson.fromJson(reader, type.getType());
}
};
}
}

2
settings.gradle

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
rootProject.name='feign'
include 'feign-core', 'feign-ribbon'
include 'feign-core', 'feign-ribbon', 'examples:feign-example-cli'

Loading…
Cancel
Save