Repository: apache/streampipes
Pull request #1180 (2023-01-30 10:51:29)

[hotfix] Extend CustomRequestApi with new get request
<!--
  ~ 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.
  ~
  -->
  
  <!--
Thanks for contributing! Here are some tips you can follow to help us incorporate your contribution quickly and easily:
1. If this is your first time, please read our contributor guidelines:
    - https://streampipes.apache.org/getinvolved.html
    - https://cwiki.apache.org/confluence/display/STREAMPIPES/Getting+Started
2. Make sure the PR title is formatted like: `[#<GitHub issue id>] PR title ...`
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., `[WIP][#<GitHub issue id>] PR title ...`.
4. Please write your PR title to summarize what this PR proposes/fixes.
5. Link the PR to the corresponding GitHub issue (if present) in the `Development` section in the right menu bar. 
6. Be sure to keep the PR description updated to reflect all changes.
7. If possible, provide a concise example to reproduce the issue for a faster review.
8. Make sure tests pass via `mvn clean install`.
9. (Optional) If the contribution is large, please file an Apache ICLA
    - http://apache.org/licenses/icla.pdf
-->

### Purpose
<!--
Please clarify what changes you are proposing and describe how those changes will address the issue.
Furthermore, describe potential consequences the changes might have.
-->
I extended the class `CustomRequestApi` with a new method that takes additional query parameters for get requests to the API.

Here is a code sample:
```java
    Map<String, String> parameters = new HashMap<>();
    parameters.put("columns", "[id;COUNT;count_id]");

    var res = client
        .customRequest()
        .sendGet("api/v4/datalake/measurements/" + resourceId,
            parameters,
            SpQueryResult.class);

```

### Remarks
<!--
Is there anything left we need to pay attention on?
Are there some references that might be important? E.g. links to Confluence, or discussions
on the mailing list or GitHub.
-->
PR introduces (a) breaking change(s): no

PR introduces (a) deprecation(s): no



Patch:
diff --git a/streampipes-client/pom.xml b/streampipes-client/pom.xml
index 7db3fee9f2..ee91bc71c4 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 b4e679fef8..9b9a453bcb 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 f083217e11..72fff42a5b 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);
   }
 }


Test patch:
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 0000000000..96444594f3
--- /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