com.freenow.apis.phrase.api.GenericPhraseAPI Maven / Gradle / Ivy
package com.freenow.apis.phrase.api;
import com.freenow.apis.phrase.exception.PhraseAppApiException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
public class GenericPhraseAPI
{
private static final Logger LOG = LoggerFactory.getLogger(GenericPhraseAPI.class);
private String PHRASE_SCHEME = "https";
private String PHRASE_HOST = "api.phraseapp.com";
// --- internal services ---
private final RestTemplate restTemplate;
protected final String authToken;
private final Map pathToETagCache = new HashMap<>();
private final Map pathToResponseCache = new HashMap<>();
public GenericPhraseAPI(
final RestTemplate restTemplate,
final String authToken
)
{
this.restTemplate = restTemplate;
this.authToken = authToken;
}
public GenericPhraseAPI(
final RestTemplate restTemplate,
final String scheme,
final String host,
final String authToken
)
{
if (scheme != null)
{
PHRASE_SCHEME = scheme;
}
if (host != null)
{
PHRASE_HOST = host;
}
this.restTemplate = restTemplate;
this.authToken = authToken;
}
protected static RestTemplate createRestTemplateWithConverter()
{
final RestTemplate restTemplate = new RestTemplate();
final List> httpMessageConverters = new ArrayList<>();
httpMessageConverters.add(new MappingJackson2HttpMessageConverter());
httpMessageConverters.add(new ByteArrayHttpMessageConverter());
restTemplate.setMessageConverters(httpMessageConverters);
return restTemplate;
}
protected String createPath(final String path, final Map placeholders)
{
// TODO with URIBuilder or directly in createUriBuilder()
String requestPath = path;
for (final Map.Entry entity : placeholders.entrySet())
{
final String value = entity.getValue();
final String key = entity.getKey();
requestPath = requestPath.replace(key, value);
}
return requestPath;
}
protected URIBuilder createUriBuilder(final String path)
{
return createUriBuilder(path, null);
}
protected URIBuilder createUriBuilder(final String path, final List parameters)
{
final URIBuilder builder = new URIBuilder();
builder.setScheme(PHRASE_SCHEME)
.setHost(PHRASE_HOST)
.setPath(path);
if (parameters != null)
{
builder.setParameters(parameters);
}
return builder;
}
protected HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy