![JAR search and dependency download from the Maven repository](/logo.png)
com.ecwid.consul.v1.catalog.CatalogConsulClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of consul-api Show documentation
Show all versions of consul-api Show documentation
Java client for Consul HTTP API (http://consul.io)
The newest version!
package com.ecwid.consul.v1.catalog;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.*;
import com.ecwid.consul.v1.catalog.model.*;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.Map;
/**
* @author Vasily Vasilkov ([email protected])
*/
public final class CatalogConsulClient implements CatalogClient {
private final ConsulRawClient rawClient;
public CatalogConsulClient(ConsulRawClient rawClient) {
this.rawClient = rawClient;
}
public CatalogConsulClient() {
this(new ConsulRawClient());
}
public CatalogConsulClient(TLSConfig tlsConfig) {
this(new ConsulRawClient(tlsConfig));
}
public CatalogConsulClient(String agentHost) {
this(new ConsulRawClient(agentHost));
}
public CatalogConsulClient(String agentHost, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, tlsConfig));
}
public CatalogConsulClient(String agentHost, int agentPort) {
this(new ConsulRawClient(agentHost, agentPort));
}
public CatalogConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {
this(new ConsulRawClient(agentHost, agentPort, tlsConfig));
}
@Override
public Response catalogRegister(CatalogRegistration catalogRegistration) {
return catalogRegister(catalogRegistration, null);
}
@Override
public Response catalogRegister(CatalogRegistration catalogRegistration, String token) {
String json = GsonFactory.getGson().toJson(catalogRegistration);
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/catalog/register", json, tokenParam);
if (httpResponse.getStatusCode() == 200) {
return new Response(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response catalogDeregister(CatalogDeregistration catalogDeregistration) {
return catalogDeregister(catalogDeregistration, null);
}
@Override
public Response catalogDeregister(CatalogDeregistration catalogDeregistration, String token) {
String json = GsonFactory.getGson().toJson(catalogDeregistration);
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
HttpResponse httpResponse = rawClient.makePutRequest("/v1/catalog/deregister", json, tokenParam);
if (httpResponse.getStatusCode() == 200) {
return new Response(null, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response> getCatalogDatacenters() {
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/catalog/datacenters");
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
return new Response>(value, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response> getCatalogNodes(QueryParams queryParams) {
CatalogNodesRequest request = CatalogNodesRequest.newBuilder()
.setQueryParams(queryParams)
.build();
return getCatalogNodes(request);
}
@Override
public Response> getCatalogNodes(CatalogNodesRequest catalogNodesRequest) {
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/catalog/nodes")
.addUrlParameters(catalogNodesRequest.asUrlParameters())
.build();
HttpResponse httpResponse = rawClient.makeGetRequest(request);
if (httpResponse.getStatusCode() == 200) {
List value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken>() {
}.getType());
return new Response<>(value, httpResponse);
} else {
throw new OperationException(httpResponse);
}
}
@Override
public Response
© 2015 - 2025 Weber Informatics LLC | Privacy Policy