Skip to content

Commit 53f40d1

Browse files
author
Adrian Cole
committed
Adds EmptyTarget for interfaces who exclusively declare URI methods
Supports cases when the base url isn't known until runtime. Closes OpenFeign#98
1 parent e44229a commit 53f40d1

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### Version 7.2
22
* Adds `Feign.Builder.build()`
33
* Opens constructor for Gson and Jackson codecs which accepts type adapters
4+
* Adds EmptyTarget for interfaces who exclusively declare URI methods
45

56
### Version 7.1
67
* Introduces feign.@Param to annotate template parameters. Users must migrate from `javax.inject.@Named` to `feign.@Param` before updating to Feign 8.0.

‎core/src/main/java/feign/Target.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,64 @@ public HardCodedTarget(Class<T> type, String name, String url) {
118118
return "HardCodedTarget(type=" + type.getSimpleName() + ", name=" + name + ", url=" + url + ")";
119119
}
120120
}
121+
122+
public static final class EmptyTarget<T> implements Target<T> {
123+
private final Class<T> type;
124+
private final String name;
125+
126+
EmptyTarget(Class<T> type, String name) {
127+
this.type = checkNotNull(type, "type");
128+
this.name = checkNotNull(emptyToNull(name), "name");
129+
}
130+
131+
public static <T> EmptyTarget<T> create(Class<T> type) {
132+
return new EmptyTarget<T>(type, "empty:" + type.getSimpleName());
133+
}
134+
135+
public static <T> EmptyTarget<T> create(Class<T> type, String name) {
136+
return new EmptyTarget<T>(type, name);
137+
}
138+
139+
@Override public Class<T> type() {
140+
return type;
141+
}
142+
143+
@Override public String name() {
144+
return name;
145+
}
146+
147+
@Override public String url() {
148+
throw new UnsupportedOperationException("Empty targets don't have URLs");
149+
}
150+
151+
@Override public Request apply(RequestTemplate input) {
152+
if (input.url().indexOf("http") != 0) {
153+
throw new UnsupportedOperationException("Request with non-absolute URL not supported with empty target");
154+
}
155+
return input.request();
156+
}
157+
158+
@Override public boolean equals(Object obj) {
159+
if (obj instanceof EmptyTarget) {
160+
EmptyTarget<?> other = (EmptyTarget) obj;
161+
return type.equals(other.type)
162+
&& name.equals(other.name);
163+
}
164+
return false;
165+
}
166+
167+
@Override public int hashCode() {
168+
int result = 17;
169+
result = 31 * result + type.hashCode();
170+
result = 31 * result + name.hashCode();
171+
return result;
172+
}
173+
174+
@Override public String toString() {
175+
if (name.equals("empty:" + type.getSimpleName())) {
176+
return "EmptyTarget(type=" + type.getSimpleName() + ")";
177+
}
178+
return "EmptyTarget(type=" + type.getSimpleName() + ", name=" + name + ")";
179+
}
180+
}
121181
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign;
17+
18+
import feign.Target.EmptyTarget;
19+
import java.net.URI;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.rules.ExpectedException;
23+
24+
import static feign.assertj.FeignAssertions.assertThat;
25+
26+
public class EmptyTargetTest {
27+
@Rule public final ExpectedException thrown = ExpectedException.none();
28+
29+
interface UriInterface {
30+
@RequestLine("GET /") Response get(URI endpoint);
31+
}
32+
33+
@Test public void whenNameNotSupplied() {
34+
assertThat(EmptyTarget.create(UriInterface.class))
35+
.isEqualTo(EmptyTarget.create(UriInterface.class, "empty:UriInterface"));
36+
}
37+
38+
@Test public void toString_withoutName() {
39+
assertThat(EmptyTarget.create(UriInterface.class).toString())
40+
.isEqualTo("EmptyTarget(type=UriInterface)");
41+
}
42+
43+
@Test public void toString_withName() {
44+
assertThat(EmptyTarget.create(UriInterface.class, "manager-access").toString())
45+
.isEqualTo("EmptyTarget(type=UriInterface, name=manager-access)");
46+
}
47+
48+
@Test public void mustApplyToAbsoluteUrl() {
49+
thrown.expect(UnsupportedOperationException.class);
50+
thrown.expectMessage("Request with non-absolute URL not supported with empty target");
51+
52+
EmptyTarget.create(UriInterface.class).apply(new RequestTemplate().method("GET").append("/relative"));
53+
}
54+
}

0 commit comments

Comments
 (0)