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

com.ecwid.consul.v1.event.EventConsulClient Maven / Gradle / Ivy

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

import java.util.List;

import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
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.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.event.model.Event;
import com.ecwid.consul.v1.event.model.EventParams;
import com.google.gson.reflect.TypeToken;

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

	private final ConsulRawClient rawClient;

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

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

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

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

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

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

	@Override
	public Response eventFire(String event, String payload, EventParams eventParams, QueryParams queryParams) {
		RawResponse rawResponse = rawClient.makePutRequest("/v1/event/fire/" + event, payload, eventParams, queryParams);

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

	@Override
	public Response> eventList(QueryParams queryParams) {
		return eventList(null, queryParams);
	}

	@Override
	public Response> eventList(String event, QueryParams queryParams) {
		UrlParameters eventParams = event != null ? new SingleUrlParameters("name", event) : null;
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/event/list", eventParams, queryParams);

		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