br.com.moip.util.QueryStringFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
Java SDK for Moip v2 APIs
package br.com.moip.util;
import br.com.moip.api.filter.Filters;
import br.com.moip.api.filter.Pagination;
import br.com.moip.exception.MoipException;
import org.apache.http.client.utils.URIBuilder;
import java.net.URISyntaxException;
import java.util.Map;
public class QueryStringFactory {
private URIBuilder uri;
private Pagination pagination;
private Filters filters;
private Map params;
public QueryStringFactory(String endpoint, Pagination pagination, Filters filters, Map params) {
try {
this.uri = new URIBuilder(endpoint);
this.pagination = pagination;
this.filters = filters;
this.params = params;
} catch (URISyntaxException e) {
throw new MoipException("Error trying to build URL: " + e.getMessage());
}
}
private void addPagination() {
if (pagination.getLimit() != 0) {
uri.addParameter("limit", String.valueOf(pagination.getLimit()));
}
if (pagination.getOffset() >= 0) {
uri.addParameter("offset", String.valueOf(pagination.getOffset()));
}
}
private void addParams() {
for (Map.Entry entry : params.entrySet()) {
uri.addParameter(entry.getKey(), entry.getValue());
}
}
public String generate() {
if (pagination != null) {
this.addPagination();
}
if (filters != null) {
uri.addParameter("filters", filters.toString());
}
if (params != null) {
this.addParams();
}
return uri.toString();
}
}