|
|
|
@ -17,18 +17,13 @@ package feign.examples;
@@ -17,18 +17,13 @@ package feign.examples;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
import com.google.gson.JsonIOException; |
|
|
|
|
import com.google.gson.stream.JsonReader; |
|
|
|
|
import dagger.Module; |
|
|
|
|
import dagger.Provides; |
|
|
|
|
import feign.Feign; |
|
|
|
|
import feign.Logger; |
|
|
|
|
import feign.RequestLine; |
|
|
|
|
import feign.Response; |
|
|
|
|
import feign.codec.Decoder; |
|
|
|
|
|
|
|
|
|
import javax.inject.Inject; |
|
|
|
|
import javax.inject.Named; |
|
|
|
|
import javax.inject.Singleton; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.Reader; |
|
|
|
|
import java.lang.reflect.Type; |
|
|
|
@ -51,8 +46,12 @@ public class GitHubExample {
@@ -51,8 +46,12 @@ public class GitHubExample {
|
|
|
|
|
int contributions; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void main(String... args) throws InterruptedException { |
|
|
|
|
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GitHubModule()); |
|
|
|
|
public static void main(String... args) { |
|
|
|
|
GitHub github = Feign.builder() |
|
|
|
|
.logger(new Logger.ErrorLogger()) |
|
|
|
|
.logLevel(Logger.Level.BASIC) |
|
|
|
|
.decoder(new GsonDecoder()) |
|
|
|
|
.target(GitHub.class, "https://api.github.com"); |
|
|
|
|
|
|
|
|
|
System.out.println("Let's fetch and print a list of the contributors to this library."); |
|
|
|
|
List<Contributor> contributors = github.contributors("netflix", "feign"); |
|
|
|
@ -61,60 +60,26 @@ public class GitHubExample {
@@ -61,60 +60,26 @@ public class GitHubExample {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Module(overrides = true, library = true, includes = GsonModule.class) |
|
|
|
|
static class GitHubModule { |
|
|
|
|
|
|
|
|
|
@Provides Logger.Level loggingLevel() { |
|
|
|
|
return Logger.Level.BASIC; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Provides Logger logger() { |
|
|
|
|
return new Logger.ErrorLogger(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Here's how it looks to wire json codecs. Note, that you can always instead use {@code feign-gson}! |
|
|
|
|
* Here's how it looks to write a decoder. Note: you can instead use {@code feign-gson}! |
|
|
|
|
*/ |
|
|
|
|
@Module(library = true) |
|
|
|
|
static class GsonModule { |
|
|
|
|
|
|
|
|
|
@Provides @Singleton Gson gson() { |
|
|
|
|
return new Gson(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Provides Decoder decoder(GsonDecoder gsonDecoder) { |
|
|
|
|
return gsonDecoder; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static class GsonDecoder implements Decoder { |
|
|
|
|
private final Gson gson; |
|
|
|
|
|
|
|
|
|
@Inject GsonDecoder(Gson gson) { |
|
|
|
|
this.gson = gson; |
|
|
|
|
} |
|
|
|
|
private final Gson gson = new Gson(); |
|
|
|
|
|
|
|
|
|
@Override public Object decode(Response response, Type type) throws IOException { |
|
|
|
|
if (response.body() == null) { |
|
|
|
|
if (void.class == type || response.body() == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
Reader reader = response.body().asReader(); |
|
|
|
|
try { |
|
|
|
|
return fromJson(new JsonReader(reader), type); |
|
|
|
|
} finally { |
|
|
|
|
ensureClosed(reader); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Object fromJson(JsonReader jsonReader, Type type) throws IOException { |
|
|
|
|
try { |
|
|
|
|
return gson.fromJson(jsonReader, type); |
|
|
|
|
return gson.fromJson(reader, type); |
|
|
|
|
} catch (JsonIOException e) { |
|
|
|
|
if (e.getCause() != null && e.getCause() instanceof IOException) { |
|
|
|
|
throw IOException.class.cast(e.getCause()); |
|
|
|
|
} |
|
|
|
|
throw e; |
|
|
|
|
} finally { |
|
|
|
|
ensureClosed(reader); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|