Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/main/java/mesosphere/marathon/client/MarathonClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requestInterceptors overwrites existing requestInterceptors, so the MarathonHeadersInterceptor gets removed in the process.


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));
}
}