This module directs Feign's http requests to OkHttp , which enables SPDY and better network control. Ex. GitHub github = Feign.builder().client(new OkHttpClient()).target(GitHub.class, "https://api.github.com");
| 41 | * "https://api.github.com"); |
| 42 | */ |
| 43 | public final class OkHttpClient implements Client, AsyncClient<Object> { |
| 44 | |
| 45 | private final okhttp3.OkHttpClient delegate; |
| 46 | |
| 47 | public OkHttpClient() { |
| 48 | this(new okhttp3.OkHttpClient()); |
| 49 | } |
| 50 | |
| 51 | public OkHttpClient(okhttp3.OkHttpClient delegate) { |
| 52 | this.delegate = delegate; |
| 53 | } |
| 54 | |
| 55 | static Request toOkHttpRequest(feign.Request input) { |
| 56 | Request.Builder requestBuilder = new Request.Builder(); |
| 57 | requestBuilder.url(input.url()); |
| 58 | |
| 59 | MediaType mediaType = null; |
| 60 | boolean hasAcceptHeader = false; |
| 61 | for (String field : input.headers().keySet()) { |
| 62 | if (field.equalsIgnoreCase("Accept")) { |
| 63 | hasAcceptHeader = true; |
| 64 | } |
| 65 | |
| 66 | for (String value : input.headers().get(field)) { |
| 67 | requestBuilder.addHeader(field, value); |
| 68 | if (field.equalsIgnoreCase("Content-Type")) { |
| 69 | mediaType = MediaType.parse(value); |
| 70 | if (input.charset() != null) { |
| 71 | mediaType.charset(input.charset()); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | // Some servers choke on the default accept string. |
| 77 | if (!hasAcceptHeader) { |
| 78 | requestBuilder.addHeader("Accept", "*/*"); |
| 79 | } |
| 80 | |
| 81 | byte[] inputBody = input.body(); |
| 82 | if (input.httpMethod().isWithBody()) { |
| 83 | requestBuilder.removeHeader("Content-Type"); |
| 84 | if (inputBody == null) { |
| 85 | // write an empty BODY to conform with okhttp 2.4.0+ |
| 86 | // http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/ |
| 87 | inputBody = new byte[0]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | RequestBody body = inputBody != null ? RequestBody.create(mediaType, inputBody) : null; |
| 92 | requestBuilder.method(input.httpMethod().name(), body); |
| 93 | return requestBuilder.build(); |
| 94 | } |
| 95 | |
| 96 | private static feign.Response toFeignResponse(Response response, feign.Request request) |
| 97 | throws IOException { |
| 98 | return feign.Response.builder() |
| 99 | .protocolVersion(enumForName(ProtocolVersion.class, response.protocol().name())) |
| 100 | .status(response.code()) |
nothing calls this directly
no outgoing calls
no test coverage detected