<commit_before>package de.domisum.lib.auxilium.mattp;

import de.domisum.lib.auxilium.data.container.AbstractURL;
import de.domisum.lib.auxilium.mattp.authproviders.NoAuthProvider;
import de.domisum.lib.auxilium.mattp.request.MattpHeader;
import de.domisum.lib.auxilium.mattp.request.MattpRequest;
import de.domisum.lib.auxilium.mattp.response.MattpResponseBodyReader;
import de.domisum.lib.auxilium.mattp.response.RequestResponse;
import de.domisum.lib.auxilium.mattp.response.readers.MattpStringReader;
import de.domisum.lib.auxilium.mattp.response.responses.ConnectionError;
import de.domisum.lib.auxilium.mattp.response.responses.RequestFailure;
import de.domisum.lib.auxilium.mattp.response.responses.RequestSuccess;
import de.domisum.lib.auxilium.util.java.annotations.API;
import de.domisum.lib.auxilium.util.java.exceptions.ShouldNeverHappenError;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpMessage;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;

@API
@RequiredArgsConstructor
public class MattpRequestEnvoy<T>
{

	// REQUEST
	private final MattpRequest request;
	@Setter private MattpAuthProvider authProvider = new NoAuthProvider();

	// RESPONSE
	private final MattpResponseBodyReader<T> responseBodyReader;


	// SEND
	@API public RequestResponse<T> send()
	{
		HttpUriRequest apacheRequest = buildApacheRequest();

		try(CloseableHttpClient httpClient = buildHttpClient();
				CloseableHttpResponse response = httpClient.execute(apacheRequest))
		{
			return processResponse(response);
		}
		catch(IOException e)
		{
			return new ConnectionError<>(ExceptionUtils.getStackTrace(e));
		}
	}


	// RESPONSE
	private RequestResponse<T> processResponse(org.apache.http.HttpResponse response) throws IOException
	{
		de.domisum.lib.auxilium.mattp.response.StatusLine statusLine = convertApacheToDomainStatusLine(response.getStatusLine());

		if(didRequestFail(response))
			return new RequestFailure<>(statusLine, readResponseBodyOnFailure(response));

		return new RequestSuccess<>(statusLine, readResponseBodyOnSuccess(response));
	}

	private T readResponseBodyOnSuccess(org.apache.http.HttpResponse response) throws IOException
	{
		return readResponseBody(response, responseBodyReader);
	}

	private String readResponseBodyOnFailure(org.apache.http.HttpResponse response) throws IOException
	{
		return readResponseBody(response, new MattpStringReader());
	}

	private <BodyT> BodyT readResponseBody(org.apache.http.HttpResponse response, MattpResponseBodyReader<BodyT> reader)
			throws IOException
	{
		try(InputStream responseBodyStream = response.getEntity().getContent())
		{
			return reader.read(responseBodyStream);
		}
	}


	// BUILD CLIENT
	private CloseableHttpClient buildHttpClient()
	{
		HttpClientBuilder clientBuilder = HttpClients.custom();
		authProvider.provideAuthFor(clientBuilder);
		return clientBuilder.build();
	}


	// BUILD REQUEST
	private HttpUriRequest buildApacheRequest()
	{
		HttpUriRequest apacheRequest = getRawMethodRequest();

		addHeadersToRequest(apacheRequest);
		if(request.getBody() != null)
			addBodyToRequest(apacheRequest);

		return apacheRequest;
	}
<commit_msg>Added simple request stage timeouts<commit_after>package de.domisum.lib.auxilium.mattp;

import de.domisum.lib.auxilium.data.container.AbstractURL;
import de.domisum.lib.auxilium.mattp.authproviders.NoAuthProvider;
import de.domisum.lib.auxilium.mattp.request.MattpHeader;
import de.domisum.lib.auxilium.mattp.request.MattpRequest;
import de.domisum.lib.auxilium.mattp.response.MattpResponseBodyReader;
import de.domisum.lib.auxilium.mattp.response.RequestResponse;
import de.domisum.lib.auxilium.mattp.response.readers.MattpStringReader;
import de.domisum.lib.auxilium.mattp.response.responses.ConnectionError;
import de.domisum.lib.auxilium.mattp.response.responses.RequestFailure;
import de.domisum.lib.auxilium.mattp.response.responses.RequestSuccess;
import de.domisum.lib.auxilium.util.java.annotations.API;
import de.domisum.lib.auxilium.util.java.exceptions.ShouldNeverHappenError;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpMessage;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;

@API
@RequiredArgsConstructor
public class MattpRequestEnvoy<T>
{

	// CONSTANTS
	private static final Duration TIMEOUT = Duration.ofSeconds(15);

	// REQUEST
	private final MattpRequest request;
	@Setter private MattpAuthProvider authProvider = new NoAuthProvider();

	// RESPONSE
	private final MattpResponseBodyReader<T> responseBodyReader;


	// SEND
	@API public RequestResponse<T> send()
	{
		HttpUriRequest apacheRequest = buildApacheRequest();

		try(CloseableHttpClient httpClient = buildHttpClient();
				CloseableHttpResponse response = httpClient.execute(apacheRequest))
		{
			return processResponse(response);
		}
		catch(IOException e)
		{
			return new ConnectionError<>(ExceptionUtils.getStackTrace(e));
		}
	}


	// RESPONSE
	private RequestResponse<T> processResponse(org.apache.http.HttpResponse response) throws IOException
	{
		de.domisum.lib.auxilium.mattp.response.StatusLine statusLine = convertApacheToDomainStatusLine(response.getStatusLine());

		if(didRequestFail(response))
			return new RequestFailure<>(statusLine, readResponseBodyOnFailure(response));

		return new RequestSuccess<>(statusLine, readResponseBodyOnSuccess(response));
	}

	private T readResponseBodyOnSuccess(org.apache.http.HttpResponse response) throws IOException
	{
		return readResponseBody(response, responseBodyReader);
	}

	private String readResponseBodyOnFailure(org.apache.http.HttpResponse response) throws IOException
	{
		return readResponseBody(response, new MattpStringReader());
	}

	private <BodyT> BodyT readResponseBody(org.apache.http.HttpResponse response, MattpResponseBodyReader<BodyT> reader)
			throws IOException
	{
		try(InputStream responseBodyStream = response.getEntity().getContent())
		{
			return reader.read(responseBodyStream);
		}
	}


	// BUILD CLIENT
	private CloseableHttpClient buildHttpClient()
	{
		HttpClientBuilder clientBuilder = HttpClients.custom();
		authProvider.provideAuthFor(clientBuilder);

		RequestConfig config = RequestConfig
				.custom()
				.setConnectTimeout((int) TIMEOUT.toMillis())
				.setConnectionRequestTimeout((int) TIMEOUT.toMillis())
				.setSocketTimeout((int) TIMEOUT.toMillis())
				.build();
		clientBuilder.setDefaultRequestConfig(config);

		return clientBuilder.build();
	}


	// BUILD REQUEST
	private HttpUriRequest buildApacheRequest()
	{
		HttpUriRequest apacheRequest = getRawMethodRequest();

		addHeadersToRequest(apacheRequest);
		if(request.getBody() != null)
			addBodyToRequest(apacheRequest);

		return apacheRequest;
	}

{'fraction_non_alphanumeric': 0.10113809691423208, 'fraction_numerical': 0.0004693183151472486, 'mean_word_length': 6.19324894514768, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3835217', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2596, 'n_words': 405}