Skip to content

Commit f82aa78

Browse files
author
adriancole
committed
added cli example
1 parent 7acb9a6 commit f82aa78

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Version 1.1.0
22
* adds Ribbon integration
3+
* adds cli example
34
* exponential backoff customizable via Retryer.Default ctor
45

56
### Version 1.0.0
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
compile 'com.netflix.feign:feign-core:1.0.0'
5+
compile 'com.google.code.gson:gson:2.2.4'
6+
provided 'com.squareup.dagger:dagger-compiler:1.0.1'
7+
}
8+
9+
// create a self-contained jar that is executable
10+
// the output is both a 'fat' project artifact and
11+
// a convenience file named "build/github"
12+
task fatJar(dependsOn: classes, type: Jar) {
13+
classifier 'fat'
14+
15+
doFirst {
16+
// Delay evaluation until the compile configuration is ready
17+
from {
18+
configurations.compile.collect { zipTree(it) }
19+
}
20+
}
21+
22+
from (sourceSets*.output.classesDir) {
23+
}
24+
25+
// really executable jar
26+
// http://skife.org/java/unix/2011/06/20/really_executable_jars.html
27+
28+
manifest {
29+
attributes 'Main-Class': 'feign.example.cli.GitHubExample'
30+
}
31+
32+
// for convenience, we make a file in the build dir named github with no extension
33+
doLast {
34+
def srcFile = new File("${buildDir}/libs/${archiveName}")
35+
def shortcutFile = new File("${buildDir}/github")
36+
shortcutFile.delete()
37+
shortcutFile << "#!/usr/bin/env sh\n"
38+
shortcutFile << 'exec java -jar $0 "$@"' + "\n"
39+
shortcutFile << srcFile.bytes
40+
shortcutFile.setExecutable(true, true)
41+
srcFile.delete()
42+
srcFile << shortcutFile.bytes
43+
srcFile.setExecutable(true, true)
44+
}
45+
}
46+
47+
artifacts {
48+
archives fatJar
49+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.example.cli;
17+
18+
import com.google.common.collect.ImmutableMap;
19+
import com.google.common.reflect.TypeToken;
20+
import com.google.gson.Gson;
21+
22+
import java.io.Reader;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import javax.inject.Singleton;
27+
import javax.ws.rs.GET;
28+
import javax.ws.rs.Path;
29+
import javax.ws.rs.PathParam;
30+
31+
import dagger.Module;
32+
import dagger.Provides;
33+
import feign.Feign;
34+
import feign.codec.Decoder;
35+
36+
/**
37+
* adapted from {@code com.example.retrofit.GitHubClient}
38+
*/
39+
public class GitHubExample {
40+
41+
interface GitHub {
42+
@GET @Path("/repos/{owner}/{repo}/contributors")
43+
List<Contributor> contributors(@PathParam("owner") String owner, @PathParam("repo") String repo);
44+
}
45+
46+
static class Contributor {
47+
String login;
48+
int contributions;
49+
}
50+
51+
public static void main(String... args) {
52+
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
53+
54+
// Fetch and print a list of the contributors to this library.
55+
List<Contributor> contributors = github.contributors("netflix", "feign");
56+
for (Contributor contributor : contributors) {
57+
System.out.println(contributor.login + " (" + contributor.contributions + ")");
58+
}
59+
}
60+
61+
/**
62+
* Here's how to wire gson deserialization.
63+
*/
64+
@Module(overrides = true, library = true)
65+
static class GsonModule {
66+
@Provides @Singleton Map<String, Decoder> decoders() {
67+
return ImmutableMap.of("GitHub", jsonDecoder);
68+
}
69+
70+
final Decoder jsonDecoder = new Decoder() {
71+
Gson gson = new Gson();
72+
73+
@Override public Object decode(String methodKey, Reader reader, TypeToken<?> type) {
74+
return gson.fromJson(reader, type.getType());
75+
}
76+
};
77+
}
78+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rootProject.name='feign'
2-
include 'feign-core', 'feign-ribbon'
2+
include 'feign-core', 'feign-ribbon', 'examples:feign-example-cli'

0 commit comments

Comments
 (0)