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

com.gocardless.http.UrlFormatter Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package com.gocardless.http;

import static com.google.common.net.UrlEscapers.urlPathSegmentEscaper;

import java.util.Map;
import okhttp3.HttpUrl;

final class UrlFormatter {
    private final HttpUrl baseUrl;

    UrlFormatter(String baseUrl) {
        this.baseUrl = HttpUrl.parse(baseUrl);
    }

    HttpUrl formatUrl(String template, Map pathParams,
            Map queryParams) {
        String path = template;
        for (Map.Entry entry : pathParams.entrySet()) {
            path = path.replace(":" + entry.getKey(),
                    urlPathSegmentEscaper().escape(entry.getValue()));
        }
        HttpUrl.Builder builder = baseUrl.resolve(path).newBuilder();
        for (Map.Entry param : queryParams.entrySet()) {
            builder.addQueryParameter(param.getKey(), param.getValue().toString());
        }
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy