From 02be68f389d5a54f2bdf40932f1b8ef8f929f5bd Mon Sep 17 00:00:00 2001 From: mjjs Date: Wed, 1 Dec 2021 00:00:36 +0200 Subject: [PATCH] Support multiple duplicate queryparams The SendGrid API documentation states that the user can supply up to ten categories, but there is no way to supply them using the Java library. This commit fixes the problem by interpreting any value that has an ampersand in it as a list of values, which will be inserted as multiple query params. For example, the call request.addQueryParam("categories", "cat1&cat2") will output "?categories=cat1&categories=cat2" into the URL. --- README.md | 2 ++ src/main/java/com/sendgrid/Client.java | 15 +++++++++++- src/test/java/com/sendgrid/ClientTest.java | 28 ++++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1d0198d..0c15ca9 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,8 @@ try { request.addHeader("Authorization", "Bearer YOUR_API_KEY"); request.addQueryParam("limit", "100"); request.addQueryParam("offset", "0"); +// Will be parsed to categories=cake&categories=pie&categories=baking +request.addQueryParam("categories", "cake&pie&baking"); request.setBody("{\"name\": \"My Request Body\"}"); request.setMethod(Method.POST); String param = "param"; diff --git a/src/main/java/com/sendgrid/Client.java b/src/main/java/com/sendgrid/Client.java index 340f12c..d0865fd 100644 --- a/src/main/java/com/sendgrid/Client.java +++ b/src/main/java/com/sendgrid/Client.java @@ -9,6 +9,8 @@ import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; +import java.util.Arrays; +import java.util.List; import org.apache.http.Header; import org.apache.http.HttpMessage; @@ -129,8 +131,19 @@ public URI buildUri(String baseUri, String endpoint, Map queryPa builder.setPath(endpoint); if (queryParams != null) { + String multiValueDelimiter = "&"; + for (Map.Entry entry : queryParams.entrySet()) { - builder.setParameter(entry.getKey(), entry.getValue()); + String value = entry.getValue(); + + if (value.indexOf(multiValueDelimiter) != -1) { + List values = Arrays.asList(value.split(multiValueDelimiter)); + for (String val : values) { + builder.addParameter(entry.getKey(), val); + } + } else { + builder.setParameter(entry.getKey(), entry.getValue()); + } } } diff --git a/src/test/java/com/sendgrid/ClientTest.java b/src/test/java/com/sendgrid/ClientTest.java index 9367379..0425199 100644 --- a/src/test/java/com/sendgrid/ClientTest.java +++ b/src/test/java/com/sendgrid/ClientTest.java @@ -33,8 +33,10 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -62,6 +64,7 @@ public void testbuildUri() { Map queryParams = new HashMap(); queryParams.put("test1", "1"); queryParams.put("test2", "2"); + queryParams.put("test3", "3&4&5"); try { uri = client.buildUri(baseUri, endpoint, queryParams); } catch (URISyntaxException ex) { @@ -70,10 +73,23 @@ public void testbuildUri() { Assert.assertTrue(errors.toString(), false); } - String url = uri.toString(); - System.out.println(url); - Assert.assertTrue(url.equals("https://api.test.com/endpoint?test2=2&test1=1") || - url.equals("https://api.test.com/endpoint?test1=1&test2=2")); + URL url = null; + try { + url = uri.toURL(); + } catch (MalformedURLException ex) { + StringWriter errors = new StringWriter(); + ex.printStackTrace(new PrintWriter(errors)); + Assert.assertTrue(errors.toString(), false); + } + + Assert.assertTrue(url.getProtocol().equals("https")); + Assert.assertTrue(url.getHost().equals("api.test.com")); + Assert.assertTrue(url.getPath().equals("/endpoint")); + Assert.assertTrue(this.queryParamHasCorrectValue(url, "test1", "1")); + Assert.assertTrue(this.queryParamHasCorrectValue(url, "test2", "2")); + Assert.assertTrue(this.queryParamHasCorrectValue(url, "test3", "3")); + Assert.assertTrue(this.queryParamHasCorrectValue(url, "test3", "4")); + Assert.assertTrue(this.queryParamHasCorrectValue(url, "test3", "5")); } @Test @@ -174,4 +190,8 @@ public void testPut() { public void testDelete() { testMethod(Method.DELETE, 204); } + + private boolean queryParamHasCorrectValue(URL url, String key, String value) { + return url.getQuery().indexOf(key + "=" + value) != -1; + } }