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

fi.evolver.ai.spring.config.ApiEndpointParameters Maven / Gradle / Ivy

package fi.evolver.ai.spring.config;

import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

public record ApiEndpointParameters(Map headers, String baseUri, Optional port, Duration timeout) {

	private URI prepareUri(List pathParts, MultiValueMap query) {
		String[] uriParts = baseUri.split("\\?");

		UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriParts[0]);
		for (String part : pathParts)
			builder.path("/").path(part);
		port.map(builder::port);

		if (uriParts.length == 2)
			builder.query(uriParts[1]);

		if (query != null && !query.isEmpty()) {
			builder.queryParams(query);
		}

		return builder.build().toUri();
	}

	/**
	 * Prepare the URI for an API request.
	 *
	 * @param pathParts Parts of the path added to the base URI.
	 * @param query Query parameters.
	 * @return The prepared URI.
	 */
	public URI prepareUri(List pathParts, Map query) {
		return prepareUri(pathParts, toMultiValueMap(query));
	}

	/**
	 * Prepare the URI for an API request.
	 *
	 * @param pathParts Parts of the path added to the base URI.
	 * @return The prepared URI.
	 */
	public URI prepareUri(String... pathParts) {
		return prepareUri(Arrays.asList(pathParts), new LinkedMultiValueMap<>());
	}


	private static MultiValueMap toMultiValueMap(Map map) {
		MultiValueMap multiValueMap = new LinkedMultiValueMap<>();
		if (map != null)
			map.forEach((key, value) -> multiValueMap.add(key, value));
		return multiValueMap;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy