com.teamscale.commons.service.client.HttpRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
The newest version!
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.teamscale.commons.service.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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy