You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2023/01/30 12:27:19 UTC
[streampipes] branch dev updated: [hotfix] Extend CustomRequestApi with new get request (#1180)
This is an automated email from the ASF dual-hosted git repository.
zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git
The following commit(s) were added to refs/heads/dev by this push:
new 9450fcc97 [hotfix] Extend CustomRequestApi with new get request (#1180)
9450fcc97 is described below
commit 9450fcc97fd1732f8db2a82ec3124dca3739327d
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Mon Jan 30 13:27:13 2023 +0100
[hotfix] Extend CustomRequestApi with new get request (#1180)
* [hotfix] Extend CustomRequestApi with new get request
* [hotfix] Add encoding to the parameters
* [hotfix] Remove StreamPipesResourceConfig.java
* [hotfix] Extracted URL encoding
---
streampipes-client/pom.xml | 7 ++
.../streampipes/client/api/CustomRequestApi.java | 9 +++
.../client/util/StreamPipesApiPath.java | 33 ++++++++-
.../client/util/StreamPipesApiPathTest.java | 80 ++++++++++++++++++++++
4 files changed, 127 insertions(+), 2 deletions(-)
diff --git a/streampipes-client/pom.xml b/streampipes-client/pom.xml
index 7db3fee9f..ee91bc71c 100644
--- a/streampipes-client/pom.xml
+++ b/streampipes-client/pom.xml
@@ -71,6 +71,13 @@
<version>0.91.0-SNAPSHOT</version>
</dependency>
+ <!-- Test dependencies -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
<!-- 3rd party Dependencies -->
<dependency>
<groupId>commons-collections</groupId>
diff --git a/streampipes-client/src/main/java/org/apache/streampipes/client/api/CustomRequestApi.java b/streampipes-client/src/main/java/org/apache/streampipes/client/api/CustomRequestApi.java
index b4e679fef..9b9a453bc 100644
--- a/streampipes-client/src/main/java/org/apache/streampipes/client/api/CustomRequestApi.java
+++ b/streampipes-client/src/main/java/org/apache/streampipes/client/api/CustomRequestApi.java
@@ -20,6 +20,8 @@ package org.apache.streampipes.client.api;
import org.apache.streampipes.client.model.StreamPipesClientConfig;
import org.apache.streampipes.client.util.StreamPipesApiPath;
+import java.util.Map;
+
public class CustomRequestApi extends AbstractClientApi {
public CustomRequestApi(StreamPipesClientConfig clientConfig) {
@@ -34,4 +36,11 @@ public class CustomRequestApi extends AbstractClientApi {
return getSingle(StreamPipesApiPath.fromStreamPipesBasePath(apiPath), responseClass);
}
+ public <T> T sendGet(String apiPath, Map<String, String> queryParameters, Class<T> responseClass) {
+ return getSingle(
+ StreamPipesApiPath.fromStreamPipesBasePath(apiPath)
+ .withQueryParameters(queryParameters),
+ responseClass);
+ }
+
}
diff --git a/streampipes-client/src/main/java/org/apache/streampipes/client/util/StreamPipesApiPath.java b/streampipes-client/src/main/java/org/apache/streampipes/client/util/StreamPipesApiPath.java
index f083217e1..72fff42a5 100644
--- a/streampipes-client/src/main/java/org/apache/streampipes/client/util/StreamPipesApiPath.java
+++ b/streampipes-client/src/main/java/org/apache/streampipes/client/util/StreamPipesApiPath.java
@@ -17,19 +17,25 @@
*/
package org.apache.streampipes.client.util;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.StringJoiner;
public class StreamPipesApiPath {
private static final List<String> BaseApiPathV2 = Arrays.asList("streampipes-backend", "api", "v2");
- private List<String> pathItems;
+ private final List<String> pathItems;
+ private final Map<String, String> queryParameters;
private StreamPipesApiPath(List<String> initialPathItems) {
this.pathItems = initialPathItems;
+ this.queryParameters = new HashMap<>();
}
public static StreamPipesApiPath fromStreamPipesBasePath() {
@@ -51,10 +57,33 @@ public class StreamPipesApiPath {
return this;
}
+ public StreamPipesApiPath withQueryParameters(Map<String, String> queryParameters) {
+ this.queryParameters.putAll(queryParameters);
+ return this;
+ }
+
@Override
public String toString() {
StringJoiner joiner = new StringJoiner("/");
pathItems.forEach(joiner::add);
- return joiner.toString();
+ return appendQueryParameters(joiner.toString());
+ }
+
+ private String appendQueryParameters(String input) {
+
+ StringJoiner joiner = new StringJoiner("&");
+ for (Map.Entry<String, String> parameter : queryParameters.entrySet()) {
+ joiner.add(applyEncoding(parameter.getKey()) + "=" + applyEncoding(parameter.getValue()));
+ }
+
+ if (joiner.length() > 0) {
+ return input + "?" + joiner;
+ } else {
+ return input;
+ }
+ }
+
+ private String applyEncoding(String value) {
+ return URLEncoder.encode(value, StandardCharsets.UTF_8);
}
}
diff --git a/streampipes-client/src/test/java/org/apache/streampipes/client/util/StreamPipesApiPathTest.java b/streampipes-client/src/test/java/org/apache/streampipes/client/util/StreamPipesApiPathTest.java
new file mode 100644
index 000000000..96444594f
--- /dev/null
+++ b/streampipes-client/src/test/java/org/apache/streampipes/client/util/StreamPipesApiPathTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.client.util;
+
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class StreamPipesApiPathTest {
+ Map<String, String> queryParameters = new HashMap();
+
+ StreamPipesApiPath streamPipesApiPath = StreamPipesApiPath
+ .fromBaseApiPath();
+
+ String baseRoute = streamPipesApiPath.toString();
+
+ @Test
+ public void testWithEmptyQueryParameters() {
+
+ var result = streamPipesApiPath
+ .withQueryParameters(queryParameters)
+ .toString();
+
+ assertEquals(baseRoute, result);
+ }
+
+ @Test
+ public void testWithOneQueryParameter() {
+ queryParameters.put("one", "v1");
+
+ var result = streamPipesApiPath
+ .withQueryParameters(queryParameters)
+ .toString();
+
+ assertEquals(baseRoute + "?one=v1", result);
+ }
+
+ @Test
+ public void testWithMultipleQueryParameters() {
+ queryParameters.put("one", "v1");
+ queryParameters.put("two", "v2");
+
+ var result = streamPipesApiPath
+ .withQueryParameters(queryParameters)
+ .toString();
+
+ assertEquals(baseRoute + "?two=v2&one=v1", result);
+ }
+
+ @Test
+ public void testEncodeParameters() {
+ queryParameters.put("one", "[v1]");
+
+ var result = streamPipesApiPath
+ .withQueryParameters(queryParameters)
+ .toString();
+
+ assertEquals(baseRoute + "?one=%5Bv1%5D", result);
+ }
+
+}
\ No newline at end of file