forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeign.java
More file actions
163 lines (140 loc) · 5.1 KB
/
Feign.java
File metadata and controls
163 lines (140 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Method;
import java.util.Map;
import javax.net.ssl.SSLSocketFactory;
import dagger.ObjectGraph;
import dagger.Provides;
import feign.Request.Options;
import feign.Target.HardCodedTarget;
import feign.Wire.NoOpWire;
import feign.codec.BodyEncoder;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.codec.FormEncoder;
/**
* Feign's purpose is to ease development against http apis that feign
* restfulness.
* <p/>
* In implementation, Feign is a {@link Feign#newInstance factory} for
* generating {@link Target targeted} http apis.
*/
public abstract class Feign {
/**
* Returns a new instance of an HTTP API, defined by annotations in the
* {@link Feign Contract}, for the specified {@code target}. You should
* cache this result.
*/
public abstract <T> T newInstance(Target<T> target);
public static <T> T create(Class<T> apiType, String url, Object... modules) {
return create(new HardCodedTarget<T>(apiType, url), modules);
}
/**
* Shortcut to {@link #newInstance(Target) create} a single {@code targeted}
* http api using {@link ReflectiveFeign reflection}.
*/
public static <T> T create(Target<T> target, Object... modules) {
return create(modules).newInstance(target);
}
/**
* Returns a {@link ReflectiveFeign reflective} factory for generating
* {@link Target targeted} http apis.
*/
public static Feign create(Object... modules) {
Object[] modulesForGraph = ImmutableList.b
8A73
uilder() //
.add(new Defaults()) //
.add(new ReflectiveFeign.Module()) //
.add(Optional.fromNullable(modules).or(new Object[]{})).build().toArray();
return ObjectGraph.create(modulesForGraph).get(Feign.class);
}
/**
* Returns an {@link ObjectGraph Dagger ObjectGraph} that can inject a
* {@link ReflectiveFeign reflective} Feign.
*/
public static ObjectGraph createObjectGraph(Object... modules) {
Object[] modulesForGraph = ImmutableList.builder() //
.add(new Defaults()) //
.add(new ReflectiveFeign.Module()) //
.add(Optional.fromNullable(modules).or(new Object[]{})).build().toArray();
return ObjectGraph.create(modulesForGraph);
}
@dagger.Module(complete = false, injects = Feign.class, library = true)
public static class Defaults {
@Provides SSLSocketFactory sslSocketFactory() {
return SSLSocketFactory.class.cast(SSLSocketFactory.getDefault());
}
@Provides Client httpClient(Client.Default client) {
return client;
}
@Provides Retryer retryer() {
return new Retryer.Default();
}
@Provides Wire noOp() {
return new NoOpWire();
}
@Provides Map<String, Options> noOptions() {
return ImmutableMap.of();
}
@Provides Map<String, BodyEncoder> noBodyEncoders() {
return ImmutableMap.of();
}
@Provides Map<String, FormEncoder> noFormEncoders() {
return ImmutableMap.of();
}
@Provides Map<String, Decoder> noDecoders() {
return ImmutableMap.of();
}
@Provides Map<String, ErrorDecoder> noErrorDecoders() {
return ImmutableMap.of();
}
}
/**
* <p/>
* Configuration keys are formatted as unresolved <a href=
* "http://docs.oracle.com/javase/6/docs/jdk/api/javadoc/doclet/com/sun/javadoc/SeeTag.html"
* >see tags</a>.
* <p/>
* For example.
* <ul>
* <li>{@code Route53}: would match a class such as
* {@code denominator.route53.Route53}
* <li>{@code Route53#list()}: would match a method such as
* {@code denominator.route53.Route53#list()}
* <li>{@code Route53#listAt(Marker)}: would match a method such as
* {@code denominator.route53.Route53#listAt(denominator.route53.Marker)}
* <li>{@code Route53#listByNameAndType(String, String)}: would match a
* method such as {@code denominator.route53.Route53#listAt(String, String)}
* </ul>
* <p/>
* Note that there is no whitespace expected in a key!
*/
public static String configKey(Method method) {
StringBuilder builder = new StringBuilder();
builder.append(method.getDeclaringClass().getSimpleName());
builder.append('#').append(method.getName()).append('(');
for (Class<?> param : method.getParameterTypes())
builder.append(param.getSimpleName()).append(',');
if (method.getParameterTypes().length > 0)
builder.deleteCharAt(builder.length() - 1);
return builder.append(')').toString();
}
Feign() {
}
}