com.smartystreets.api.Request Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartystreets-java-sdk Show documentation
Show all versions of smartystreets-java-sdk Show documentation
A library to help Java developers easily access the SmartyStreets APIs.
package com.smartystreets.api;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Request {
private static final String CHARSET = "UTF-8";
private final Map headers;
private final Map parameters;
private String urlPrefix;
private String method;
private String contentType;
private byte[] payload;
public Request() {
this.urlPrefix = "";
this.method = "GET";
this.headers = new HashMap<>();
this.parameters = new LinkedHashMap<>();
this.contentType = "application/json";
}
void putHeader(String name, Object value) {
this.headers.put(name, value);
}
public void putParameter(String name, String value) {
if (name == null || value == null || name.length() == 0)
return;
parameters.put(name, value);
}
private static String urlEncode(String value) {
try {
return URLEncoder.encode(value, CHARSET);
} catch (Exception ex) {
return "";
}
}
//region [ Getters ]
public String getUrl() {
String url = this.urlPrefix;
this.headers.put("content-type", this.contentType);
if (!url.contains("?"))
url += "?";
for (String value : parameters.keySet()) {
if (!url.endsWith("?"))
url += "&";
String encodedName = urlEncode(value);
String encodedValue = urlEncode(parameters.get(value));
url += encodedName + "=" + encodedValue;
}
return url;
}
public String getMethod() {
return method;
}
public Map getHeaders() {
return this.headers;
}
public byte[] getPayload() {
return this.payload;
}
public String getUrlPrefix() {
return this.urlPrefix;
}
public String getContentType() {
return contentType;
}
//endregion
//region [ Setters ]
public void setPayload(byte[] payload) {
this.method = "POST";
this.payload = payload;
}
public void setUrlPrefix(String urlPrefix) {
this.urlPrefix = urlPrefix;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
//endregion
}