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

org.conqat.engine.service.shared.client.HttpRequest Maven / Gradle / Ivy

There is a newer version: 2025.1.0
Show newest version
package org.conqat.engine.service.shared.client;

import java.io.IOException;

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;

/**
 * Allows to formulate requests against Teamscale with an exchangeable http
 * client.
 */
public class HttpRequest {

	/**
	 * Helper method for starting a service call using a given
	 * {@link CloseableHttpClient}.
	 */
	public static  T withHttpClient(CloseableHttpClient client, ServiceClientCallable callable)
			throws ServiceCallException {
		try (CloseableHttpClient c = client) {
			return callable.call(c);
		} catch (IOException e) {
			throw new ServiceCallException("Problem closing HTTP client: " + e.getMessage(), e);
		}
	}

	/**
	 * Helper method for starting a service call with the given
	 * {@link ServerDetails}.
	 */
	public static  T with(ServerDetails serverDetails, ServiceClientCallable callable)
			throws ServiceCallException {
		return withHttpClient(ServiceClientUtils.getHttpClient(serverDetails), callable);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP GET on the given URI.
	 */
	public static  ServiceClientCallable get(IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpGet(uri), deserializationFormat);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP POST on the given URI.
	 */
	public static  ServiceClientCallable post(ISerializationFormat serializationFormat, T data,
			IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpPost(uri), serializationFormat, data, deserializationFormat);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP POST on the given URI.
	 */
	public static  ServiceClientCallable post(IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpPost(uri), deserializationFormat);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP PUT on the given URI.
	 */
	public static  ServiceClientCallable put(ISerializationFormat serializationFormat, T data,
			IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpPut(uri), serializationFormat, data, deserializationFormat);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP PUT on the given URI
	 *         without any payload.
	 * @deprecated PUT requests should always have a body
	 */
	@Deprecated
	public static  ServiceClientCallable put(IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpPut(uri), deserializationFormat);
	}

	/**
	 * @return a {@link ServiceClientCallable} for a HTTP DELETE on the given URI.
	 */
	public static  ServiceClientCallable delete(IDeserializationFormat deserializationFormat, String uri) {
		return execute(new HttpDelete(uri), deserializationFormat);
	}

	/**
	 * Executes the request and deserializes the response using the given
	 * deserialization format.
	 */
	private static  ServiceClientCallable execute(HttpRequestBase httpRequest,
			IDeserializationFormat deserializationFormat) {
		return client -> ServiceClientUtils.executeRequest(client, httpRequest,
				deserializationFormat.getMimeType().orElse(null), deserializationFormat::deserialize);
	}

	/**
	 * Executes the request and deserializes the response using the given
	 * deserialization format.
	 */
	private static  ServiceClientCallable execute(HttpEntityEnclosingRequestBase httpRequest,
			ISerializationFormat serializationFormat, T data, IDeserializationFormat deserializationFormat) {
		serializationFormat.getMimeType()
				.ifPresent(mimeType -> httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, mimeType.getType()));
		httpRequest.setEntity(serializationFormat.toHttpEntity(data));
		return execute(httpRequest, deserializationFormat);
	}
}