com.youcruit.billogram.client.http.AbstractHttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of billogram-v2-api-java-lib Show documentation
Show all versions of billogram-v2-api-java-lib Show documentation
Library for connecting to the Billogram v2 API
package com.youcruit.billogram.client.http;
import static java.util.Collections.EMPTY_MAP;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.youcruit.billogram.objects.request.Search;
public abstract class AbstractHttpClient implements HttpClient {
public static final Charset UTF8 = Charset.forName("UTF-8");
protected final Gson gson;
protected final String baseUrl;
public AbstractHttpClient(Gson gson, String baseUrl) {
this.baseUrl = baseUrl == null ? API_BASE_URL : baseUrl;
this.gson = gson == null ? createGson() : gson;
}
protected static Gson createGson() {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}
protected static String getApiUrl(boolean sandbox) {
return sandbox ? API_SANDBOX_BASE_URL : API_BASE_URL;
}
@SuppressWarnings("unchecked")
public URI pathToUri(String... pathSegments) {
return pathToUri(EMPTY_MAP, pathSegments);
}
@SuppressWarnings("unchecked")
public URI pathToUri(Search search, String... pathSegments) {
if (search == null) {
return pathToUri(EMPTY_MAP, pathSegments);
} else {
return pathToUri(search.toQueryParameters(), pathSegments);
}
}
public URI pathToUri(Map queryParameters, String... pathSegments) {
StringBuilder sb = new StringBuilder(baseUrl);
if (baseUrl.endsWith("/")) {
sb.setLength(sb.length() - 1);
}
try {
for (String pathSegment : pathSegments) {
sb.append('/').append(URLEncoder.encode(pathSegment, "UTF-8"));
}
if (queryParameters.size() > 0) {
boolean first = true;
for (Map.Entry queryParameter : queryParameters.entrySet()) {
sb.append(first ? '?' : '&');
first = false;
sb.append(queryParameter.getKey()).append('=').append(URLEncoder.encode(queryParameter.getValue(), "UTF-8"));
}
}
return URI.create(sb.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public Gson getMapper() {
return gson;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy