forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriBuilder.java
More file actions
106 lines (88 loc) · 3.03 KB
/
Copy pathUriBuilder.java
File metadata and controls
106 lines (88 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package io.intercom.api;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
class UriBuilder {
public static UriBuilder newBuilder() {
return new UriBuilder(Intercom.getApiBaseURI());
}
public static UriBuilder newBuilder(String uri) {
return new UriBuilder(uri);
}
public static UriBuilder newBuilder(URI uri) {
return new UriBuilder(uri);
}
private StringBuilder uri;
private TreeMap<String, String> paramsMap; // treemap to simplify testing
private UriBuilder(String uri) {
try {
this.uri = new StringBuilder(new URI(uri).toASCIIString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private UriBuilder(URI uri) {
this.uri = new StringBuilder(uri.toASCIIString());
}
public String buildString() {
return build().toASCIIString();
}
public URI build() {
if (paramsMap != null) {
uri.append("?");
final StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
if (sb.lastIndexOf("&") == sb.length() - 1) {
sb.deleteCharAt(sb.length() - 1);
}
uri.append(sb);
}
try {
return new URI(uri.toString());
} catch (URISyntaxException e) {
throw new IntercomException("could not create api url", e);
}
}
public UriBuilder path(String path) {
uri.append("/").append(urlEncode(path));
return this;
}
public UriBuilder path(List<String> path) {
List<String> urlEncoded = Lists.newArrayList();
for (String s : path) {
urlEncoded.add(urlEncode(s));
}
uri.append("/").append(Joiner.on("/").join(urlEncoded));
return this;
}
public UriBuilder query(String param, String value) {
if (paramsMap == null) {
paramsMap = Maps.newTreeMap();
}
paramsMap.put(urlEncode(param), urlEncode(value));
return this;
}
public UriBuilder query(Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
query(entry.getKey(), entry.getValue());
}
return this;
}
private String urlEncode(String param) {
try {
// URLEncoder is a html forms encoder not a percent encoder
return java.net.URLEncoder.encode(param, Charsets.UTF_8.name()).replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
throw new IntercomException("could not encode url param " + param, e);
}
}
}