|
1 | 1 | package com.sendgrid.util; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
4 | | -import com.fasterxml.jackson.databind.JsonMappingException; |
5 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
6 | | -import com.sendgrid.exception.ApiConnectionException; |
7 | | -import com.sendgrid.exception.ApiException; |
| 3 | +import com.sendgrid.constant.EnumConstants; |
| 4 | +import org.apache.http.client.utils.URIBuilder; |
8 | 5 |
|
9 | | -import java.io.IOException; |
10 | | -import java.net.URLEncoder; |
11 | | -import java.nio.charset.StandardCharsets; |
| 6 | +import java.util.Arrays; |
12 | 7 | import java.util.List; |
13 | 8 | import java.util.Map; |
14 | | -import java.util.StringJoiner; |
15 | 9 |
|
16 | 10 | public class Utility { |
17 | | - public static String buildWithPathParams(String path, Map<String, String> params) { |
| 11 | + |
| 12 | + public static String buildBaseUrl(final String domain, final String region, final String endPoint) { |
| 13 | + List<String> availableRegions = EnumConstants.Region.getValues(); |
| 14 | + StringBuilder defaultDomain = new StringBuilder(domain); |
| 15 | + if (availableRegions.contains(region)) { |
| 16 | + if (!EnumConstants.Region.GLOBAL.getValue().equals(region)) { |
| 17 | + defaultDomain.append("."); |
| 18 | + defaultDomain.append(region); |
| 19 | + } |
| 20 | + } |
| 21 | + return "https://" + defaultDomain.toString() + ".sendgrid.com" + endPoint; |
| 22 | + } |
| 23 | + public static String buildWithPathParams(String path, final Map<String, String> params) { |
18 | 24 | for (Map.Entry<String, String> entry : params.entrySet()) { |
19 | 25 | String placeholder = "\\{" + entry.getKey() + "\\}"; |
20 | 26 | path = path.replaceAll(placeholder, entry.getValue()); |
21 | 27 | } |
22 | 28 | return path; |
23 | 29 | } |
24 | 30 |
|
25 | | - public static String buildWithQueryParams(String path, Map<String, List<String>> queryParams) { |
26 | | - if (queryParams.isEmpty()) { |
27 | | - return path; |
| 31 | + public static String buildWithQueryParams(String path, final Map<String, String> queryParams) { |
| 32 | + URIBuilder builder = new URIBuilder(); |
| 33 | + if (queryParams != null) { |
| 34 | + String multiValueDelimiter = "&"; |
| 35 | + for (Map.Entry<String, String> entry : queryParams.entrySet()) { |
| 36 | + String key = entry.getKey(); |
| 37 | + String value = entry.getValue(); |
| 38 | + |
| 39 | + if (value.contains(multiValueDelimiter)) { |
| 40 | + List<String> values = Arrays.asList(value.split(multiValueDelimiter)); |
| 41 | + for (String val : values) { |
| 42 | + builder.addParameter(key, val); |
| 43 | + } |
| 44 | + } else { |
| 45 | + builder.setParameter(key, value); |
| 46 | + } |
| 47 | + } |
28 | 48 | } |
29 | | - StringJoiner joiner = new StringJoiner("&"); |
30 | | - queryParams.forEach((key, values) -> { |
31 | | - values.forEach(value -> { |
32 | | - joiner.add(key + "=" + value); // In case all query parameter needs to be URL Encoded, encode value here. |
33 | | - }); |
34 | | - }); |
35 | | - path = path + "?" + joiner.toString(); |
36 | | - return path; |
| 49 | + return path + builder.toString(); |
37 | 50 | } |
38 | 51 | } |
0 commit comments