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

br.com.moip.util.QueryStringFactory Maven / Gradle / Ivy

There is a newer version: 4.7.6
Show newest version
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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy