Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide easier support for empty targets #98

Closed
davidmc24 opened this issue Dec 27, 2013 · 4 comments · Fixed by #157
Closed

Provide easier support for empty targets #98

davidmc24 opened this issue Dec 27, 2013 · 4 comments · Fixed by #157
Milestone

Comments

@davidmc24
Copy link
Contributor

In some cases, it may be useful to have Feign instances that don't have any base URL at all. Instead, all methods would include a URI parameter.

Currently, this can be accomplished by using a custom implementation of Target that requires a URL to be provided. I've included an example below. This target can then be passed to Feign.builder().target(Target) Feign.create(Target, Object...).

If such an EmptyTarget were included in feign-core, we could add signatures like Feign.builder().target(Class) that allow creating Feign instances without a base URL and without explicitly specifying a Target type. A signature for Feign.create will require a bit more thought, since Feign.create(Class, Object...) would conflict with the pre-existing Feign.create(Class, String, Object...).

public class EmptyTarget<T> implements Target<T> {
    private final Class<T> type;
    private final String name;

    public EmptyTarget(Class<T> type) {
      this.type = checkNotNull(type, "type");
      this.name = "empty:" + type.getSimpleName();
    }

    @Override
    public Class<T> type() {
        return type;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String url() {
        throw new UnsupportedOperationException("Empty targets don't have URLs");
    }

    @Override
    public Request apply(RequestTemplate input) {
        if (input.url().indexOf("http") != 0) {
            throw new UnsupportedOperationException("Request with non-absolute URL not supported with empty target");
        }
        return input.request();
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(new Object[]{type, name});
    }

    @Override
    public boolean equals(Object obj) {
      if (obj == null) {
          return false;
      }
      if (this == obj) {
          return true;
      }
      if (EmptyTarget.class != obj.getClass()) {
          return false;
      }
      EmptyTarget<?> that = EmptyTarget.class.cast(obj);
      return this.type.equals(that.type) && this.name.equals(that.name);
    }
}
@davidmc24
Copy link
Contributor Author

If there's interest in this sort of change, I'd be happy to put together a pull request.

@matthurne
Copy link
Contributor

I recently found a case where this type of usage would aid unit testing.

@codefromthecrypt
Copy link
Contributor

Looks sensible

codefromthecrypt pushed a commit that referenced this issue Feb 1, 2015
Supports cases when the base url isn't known until runtime.

Closes #98
@codefromthecrypt
Copy link
Contributor

on the way.. #157

@codefromthecrypt codefromthecrypt added this to the 7.2.0 milestone Feb 1, 2015
codefromthecrypt pushed a commit that referenced this issue Feb 1, 2015
Supports cases when the base url isn't known until runtime.

Closes #98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants