Skip to content

added cli example #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
49 changes: 49 additions & 0 deletions examples/feign-example-cli/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name='feign'
include 'feign-core', 'feign-ribbon'
include 'feign-core', 'feign-ribbon', 'examples:feign-example-cli'