All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ecwid.consul.v1.status.StatusConsulClient Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
package com.ecwid.consul.v1.status;

import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.RawResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.Response;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * @author Vasily Vasilkov ([email protected])
 */
public final class StatusConsulClient implements StatusClient {

	private final ConsulRawClient rawClient;

	public StatusConsulClient(ConsulRawClient rawClient) {
		this.rawClient = rawClient;
	}

	public StatusConsulClient() {
		this(new ConsulRawClient());
	}

	public StatusConsulClient(TLSConfig tlsConfig) {
		this(new ConsulRawClient(tlsConfig));
	}

	public StatusConsulClient(String agentHost) {
		this(new ConsulRawClient(agentHost));
	}

	public StatusConsulClient(String agentHost, TLSConfig tlsConfig) {
		this(new ConsulRawClient(agentHost, tlsConfig));
	}

	public StatusConsulClient(String agentHost, int agentPort) {
		this(new ConsulRawClient(agentHost, agentPort));
	}

	public StatusConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {
		this(new ConsulRawClient(agentHost, agentPort, tlsConfig));
	}

	@Override
	public Response getStatusLeader() {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/status/leader");

		if (rawResponse.getStatusCode() == 200) {
			String value = GsonFactory.getGson().fromJson(rawResponse.getContent(), String.class);
			return new Response(value, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response> getStatusPeers() {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/status/peers");

		if (rawResponse.getStatusCode() == 200) {
			List value = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken>() {
			}.getType());
			return new Response>(value, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy