Browse Source

Makes examples standalone and built from standard Gradle or Maven

pull/153/head
Adrian Cole 10 years ago
parent
commit
f4342dce06
  1. 10
      example-github/README.md
  2. 22
      example-github/build.gradle
  3. 73
      example-github/pom.xml
  4. 27
      example-github/src/main/java/feign/example/github/GitHubExample.java
  5. 10
      example-wikipedia/README.md
  6. 22
      example-wikipedia/build.gradle
  7. 78
      example-wikipedia/pom.xml
  8. 13
      example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java
  9. 93
      example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java
  10. 2
      settings.gradle

10
example-github/README.md

@ -0,0 +1,10 @@
GitHub Example
===================
This is an example of a simple json client.
=== Building example with Gradle
Install and run `gradle` to produce `build/wikipedia`
=== Building example with Maven
Install and run `mvn` to produce `target/wikipedia`

22
example-github/build.gradle

@ -1,15 +1,19 @@
plugins { // NOTE: This module is intended to be a stand-alone example which does depend on nebula.
id 'nebula.provided-base' version '2.0.1' defaultTasks 'clean', 'fatJar'
}
apply plugin: 'java' apply plugin: 'java'
sourceCompatibility = 1.6 repositories {
mavenCentral()
}
configurations {
compile
}
dependencies { dependencies {
compile 'com.netflix.feign:feign-core:5.3.0' compile 'com.netflix.feign:feign-core:7.1.0'
compile 'com.netflix.feign:feign-gson:5.3.0' compile 'com.netflix.feign:feign-gson:7.1.0'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
} }
// create a self-contained jar that is executable // create a self-contained jar that is executable
@ -49,7 +53,3 @@ task fatJar(dependsOn: classes, type: Jar) {
srcFile.setExecutable(true, true) srcFile.setExecutable(true, true)
} }
} }
artifacts {
archives fatJar
}

73
example-github/pom.xml

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-example-github</artifactId>
<packaging>jar</packaging>
<version>7.1.0</version>
<name>GitHub Example</name>
<dependencies>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>feign.example.github.GitHubExample</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<programFile>github</programFile>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>really-executable-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

27
example-github/src/main/java/feign/example/github/GitHubExample.java

@ -15,14 +15,11 @@
*/ */
package feign.example.github; package feign.example.github;
import dagger.Module;
import dagger.Provides;
import feign.Feign; import feign.Feign;
import feign.Logger; import feign.Logger;
import feign.Param;
import feign.RequestLine; import feign.RequestLine;
import feign.gson.GsonModule; import feign.gson.GsonDecoder;
import javax.inject.Named;
import java.util.List; import java.util.List;
/** /**
@ -32,7 +29,7 @@ public class GitHubExample {
interface GitHub { interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors") @RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Named("owner") String owner, @Named("repo") String repo); List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
} }
static class Contributor { static class Contributor {
@ -41,7 +38,11 @@ public class GitHubExample {
} }
public static void main(String... args) throws InterruptedException { public static void main(String... args) throws InterruptedException {
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule(), new LogToStderr()); GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.logger(new Logger.ErrorLogger())
.logLevel(Logger.Level.BASIC)
.target(GitHub.class, "https://api.github.com");
System.out.println("Let's fetch and print a list of the contributors to this library."); System.out.println("Let's fetch and print a list of the contributors to this library.");
List<Contributor> contributors = github.contributors("netflix", "feign"); List<Contributor> contributors = github.contributors("netflix", "feign");
@ -49,16 +50,4 @@ public class GitHubExample {
System.out.println(contributor.login + " (" + contributor.contributions + ")"); System.out.println(contributor.login + " (" + contributor.contributions + ")");
} }
} }
@Module(overrides = true, library = true, includes = GsonModule.class)
static class LogToStderr {
@Provides Logger.Level loggingLevel() {
return Logger.Level.BASIC;
}
@Provides Logger logger() {
return new Logger.ErrorLogger();
}
}
} }

10
example-wikipedia/README.md

@ -0,0 +1,10 @@
Wikipedia Example
===================
This is an example of advanced json response parsing, including pagination.
=== Building example with Gradle
Install and run `gradle` to produce `build/wikipedia`
=== Building example with Maven
Install and run `mvn` to produce `target/wikipedia`

22
example-wikipedia/build.gradle

@ -1,15 +1,19 @@
plugins { // NOTE: This module is intended to be a stand-alone example which does depend on nebula.
id 'nebula.provided-base' version '2.0.1' defaultTasks 'clean', 'fatJar'
}
apply plugin: 'java' apply plugin: 'java'
sourceCompatibility = 1.6 repositories {
mavenCentral()
}
configurations {
compile
}
dependencies { dependencies {
compile 'com.netflix.feign:feign-core:5.3.0' compile 'com.netflix.feign:feign-core:7.1.0'
compile 'com.netflix.feign:feign-gson:5.3.0' compile 'com.netflix.feign:feign-gson:7.1.0'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
} }
// create a self-contained jar that is executable // create a self-contained jar that is executable
@ -49,7 +53,3 @@ task fatJar(dependsOn: classes, type: Jar) {
srcFile.setExecutable(true, true) srcFile.setExecutable(true, true)
} }
} }
artifacts {
archives fatJar
}

78
example-wikipedia/pom.xml

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-example-wikipedia</artifactId>
<packaging>jar</packaging>
<version>7.1.0</version>
<name>Wikipedia Example</name>
<dependencies>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>feign.example.wikipedia.WikipediaExample</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<programFile>wikipedia</programFile>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>really-executable-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

13
example-wikipedia/src/main/java/feign/example/wikipedia/ResponseAdapter.java

@ -58,17 +58,11 @@ abstract class ResponseAdapter<X> extends TypeAdapter<WikipediaExample.Response<
} }
} }
reader.endObject(); reader.endObject();
} else if ("query-continue".equals(nextName)) { } else if ("continue".equals(nextName)) {
reader.beginObject(); reader.beginObject();
while (reader.hasNext()) { while (reader.hasNext()) {
if ("search".equals(reader.nextName())) { if ("gsroffset".equals(reader.nextName())) {
reader.beginObject(); pages.nextOffset = reader.nextLong();
while (reader.hasNext()) {
if ("gsroffset".equals(reader.nextName())) {
pages.nextOffset = reader.nextLong();
}
}
reader.endObject();
} else { } else {
reader.skipValue(); reader.skipValue();
} }
@ -79,7 +73,6 @@ abstract class ResponseAdapter<X> extends TypeAdapter<WikipediaExample.Response<
} }
} }
reader.endObject(); reader.endObject();
reader.close();
return pages; return pages;
} }

93
example-wikipedia/src/main/java/feign/example/wikipedia/WikipediaExample.java

@ -15,30 +15,27 @@
*/ */
package feign.example.wikipedia; package feign.example.wikipedia;
import com.google.gson.TypeAdapter; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import dagger.Module;
import dagger.Provides;
import feign.Feign; import feign.Feign;
import feign.Logger; import feign.Logger;
import feign.Param;
import feign.RequestLine; import feign.RequestLine;
import feign.gson.GsonModule; import feign.gson.GsonDecoder;
import javax.inject.Named;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import static dagger.Provides.Type.SET;
public class WikipediaExample { public class WikipediaExample {
public static interface Wikipedia { public static interface Wikipedia {
@RequestLine("GET /w/api.php?action=query&generator=search&prop=info&format=json&gsrsearch={search}") @RequestLine("GET /w/api.php?action=query&continue=&generator=search&prop=info&format=json&gsrsearch={search}")
Response<Page> search(@Named("search") String search); Response<Page> search(@Param("search") String search);
@RequestLine("GET /w/api.php?action=query&generator=search&prop=info&format=json&gsrsearch={search}&gsroffset={offset}") @RequestLine("GET /w/api.php?action=query&continue=&generator=search&prop=info&format=json&gsrsearch={search}&gsroffset={offset}")
Response<Page> resumeSearch(@Named("search") String search, @Named("offset") long offset); Response<Page> resumeSearch(@Param("search") String search, @Param("offset") long offset);
} }
static class Page { static class Page {
@ -47,15 +44,20 @@ public class WikipediaExample {
} }
public static class Response<X> extends ArrayList<X> { public static class Response<X> extends ArrayList<X> {
/** /** when present, the position to resume the list. */
* when present, the position to resume the list.
*/
Long nextOffset; Long nextOffset;
} }
public static void main(String... args) throws InterruptedException { public static void main(String... args) throws InterruptedException {
Wikipedia wikipedia = Feign.create(Wikipedia.class, "http://en.wikipedia.org", Gson gson = new GsonBuilder()
new WikipediaDecoder(), new LogToStderr()); .registerTypeAdapter(new TypeToken<Response<Page>>(){}.getType(), pagesAdapter)
.create();
Wikipedia wikipedia = Feign.builder()
.decoder(new GsonDecoder(gson))
.logger(new Logger.ErrorLogger())
.logLevel(Logger.Level.BASIC)
.target(Wikipedia.class, "http://en.wikipedia.org");
System.out.println("Let's search for PTAL!"); System.out.println("Let's search for PTAL!");
Iterator<Page> pages = lazySearch(wikipedia, "PTAL"); Iterator<Page> pages = lazySearch(wikipedia, "PTAL");
@ -101,48 +103,25 @@ public class WikipediaExample {
}; };
} }
@Module(includes = GsonModule.class) static ResponseAdapter<Page> pagesAdapter = new ResponseAdapter<Page>() {
static class WikipediaDecoder {
/**
* registers a gson {@link TypeAdapter} for {@code Response<Page>}.
*/
@Provides(type = SET) TypeAdapter pagesAdapter() {
return new ResponseAdapter<Page>() {
@Override
protected String query() {
return "pages";
}
@Override
protected Page build(JsonReader reader) throws IOException {
Page page = new Page();
while (reader.hasNext()) {
String key = reader.nextName();
if (key.equals("pageid")) {
page.id = reader.nextLong();
} else if (key.equals("title")) {
page.title = reader.nextString();
} else {
reader.skipValue();
}
}
return page;
}
};
}
}
@Module(overrides = true, library = true)
static class LogToStderr {
@Provides Logger.Level loggingLevel() { @Override protected String query() {
return Logger.Level.BASIC; return "pages";
} }
@Provides Logger logger() { @Override protected Page build(JsonReader reader) throws IOException {
return new Logger.ErrorLogger(); Page page = new Page();
while (reader.hasNext()) {
String key = reader.nextName();
if (key.equals("pageid")) {
page.id = reader.nextLong();
} else if (key.equals("title")) {
page.title = reader.nextString();
} else {
reader.skipValue();
}
}
return page;
} }
} };
} }

2
settings.gradle

@ -1,5 +1,5 @@
rootProject.name='feign' rootProject.name='feign'
include 'core', 'sax', 'gson', 'jackson', 'jaxb', 'jaxrs', 'okhttp', 'ribbon', 'slf4j', 'example-github', 'example-wikipedia' include 'core', 'sax', 'gson', 'jackson', 'jaxb', 'jaxrs', 'okhttp', 'ribbon', 'slf4j'
rootProject.children.each { childProject -> rootProject.children.each { childProject ->
childProject.name = 'feign-' + childProject.name childProject.name = 'feign-' + childProject.name

Loading…
Cancel
Save