diff --git a/src/main/java/mesosphere/marathon/client/MarathonClient.java b/src/main/java/mesosphere/marathon/client/MarathonClient.java index 6447f92..520523d 100644 --- a/src/main/java/mesosphere/marathon/client/MarathonClient.java +++ b/src/main/java/mesosphere/marathon/client/MarathonClient.java @@ -1,5 +1,7 @@ package mesosphere.marathon.client; +import feign.Feign.Builder; +import feign.auth.BasicAuthRequestInterceptor; import mesosphere.marathon.client.utils.MarathonException; import mesosphere.marathon.client.utils.ModelUtils; import feign.Feign; @@ -10,6 +12,10 @@ import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; +import java.util.Arrays; + +import static java.util.Arrays.asList; + public class MarathonClient { static class MarathonHeadersInterceptor implements RequestInterceptor { @Override @@ -27,11 +33,33 @@ public Exception decode(String methodKey, Response response) { } public static Marathon getInstance(String endpoint) { - GsonDecoder decoder = new GsonDecoder(ModelUtils.GSON); - GsonEncoder encoder = new GsonEncoder(ModelUtils.GSON); - return Feign.builder().encoder(encoder).decoder(decoder) + return getInstance(endpoint, null); + } + + /** + * The generalized version of the method that allows more in-depth customizations via + * {@link RequestInterceptor}s. + * + * @param endpoint + * URL of Marathon + */ + public static Marathon getInstance(String endpoint, RequestInterceptor... interceptors) { + Builder b = Feign.builder() + .encoder(new GsonEncoder(ModelUtils.GSON)) + .decoder(new GsonDecoder(ModelUtils.GSON)) .errorDecoder(new MarathonErrorDecoder()) - .requestInterceptor(new MarathonHeadersInterceptor()) - .target(Marathon.class, endpoint); + .requestInterceptor(new MarathonHeadersInterceptor()); + + if (interceptors!=null) + b.requestInterceptors(asList(interceptors)); + + return b.target(Marathon.class, endpoint); + } + + /** + * Creates a Marathon client proxy that performs HTTP basic authentication. + */ + public static Marathon getInstanceWithBasicAuth(String endpoint, String username, String password) { + return getInstance(endpoint,new BasicAuthRequestInterceptor(username,password)); } }