Adrian Cole
12 years ago
4 changed files with 129 additions and 1 deletions
@ -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 |
||||
} |
@ -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()); |
||||
} |
||||
}; |
||||
} |
||||
} |
Loading…
Reference in new issue