Kinae commented on a change in pull request #5121:
URL: https://github.com/apache/camel/pull/5121#discussion_r585649917



##########
File path: 
components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackConsumer.java
##########
@@ -141,68 +129,40 @@ public int processBatch(Queue<Object> exchanges) throws 
Exception {
         return total;
     }
 
-    private String getChannelId(String channel) throws IOException, 
DeserializationException {
-        HttpPost httpPost = new HttpPost(slackEndpoint.getServerUrl() + 
"/api/conversations.list");
-
-        List<BasicNameValuePair> params = new ArrayList<>();
-        params.add(new BasicNameValuePair("token", slackEndpoint.getToken()));
-        httpPost.setEntity(new UrlEncodedFormEntity(params));
-
-        HttpResponse response = client.execute(httpPost);
-
-        String jsonString = readResponse(response);
-        JsonObject c = (JsonObject) Jsoner.deserialize(jsonString);
-
-        checkSlackReply(c);
-
-        Collection<JsonObject> channels = c.getCollection("channels");
-        if (channels == null) {
-            throw new RuntimeCamelException("The response was successful but 
no channel list was provided");
-        }
-
-        for (JsonObject singleChannel : channels) {
-            if (singleChannel.get("name") != null) {
-                if (singleChannel.get("name").equals(channel)) {
-                    if (singleChannel.get("id") != null) {
-                        return (String) singleChannel.get("id");
-                    }
-                }
+    private String getChannelId(final String channel, final String cursor) {
+        try {
+            // Maximum limit is 1000. Slack recommends no more than 200 
results at a time.
+            // https://api.slack.com/methods/conversations.list
+            ConversationsListResponse response = 
slack.methods(slackEndpoint.getToken()).conversationsList(req -> req
+                    
.types(Collections.singletonList(slackEndpoint.getConversationType()))
+                    .cursor(cursor)
+                    .limit(200));
+
+            if (!response.isOk()) {
+                throw new RuntimeCamelException("API request 
conversations.list to Slack failed: " + response);
             }
-        }
 
-        return jsonString;
+            return response.getChannels().stream()
+                    .filter(it -> it.getName().equals(channel))
+                    .map(Conversation::getId)
+                    .findFirst().orElseGet(() -> {
+                        if 
(isNullOrEmpty(response.getResponseMetadata().getNextCursor())) {
+                            throw new 
RuntimeCamelException(String.format("Channel %s not found", channel));
+                        }
+                        return getChannelId(channel, 
response.getResponseMetadata().getNextCursor());
+                    });
+        } catch (IOException | SlackApiException e) {
+            throw new RuntimeCamelException("API request conversations.list to 
Slack failed", e);
+        }
     }
 
-    private void checkSlackReply(JsonObject c) {
-        boolean okStatus = c.getBoolean("ok");
-
-        if (!okStatus) {
-            String errorMessage = c.getString("error");
-
-            if (errorMessage == null || errorMessage.isEmpty()) {
-                errorMessage = "the slack server did not provide error 
details";
-            }
-
-            throw new RuntimeCamelException(String.format("API request to 
Slack failed: %s", errorMessage));
-        }
+    private static boolean isNullOrEmpty(String str) {
+        return str == null || str.isEmpty();

Review comment:
       Yes ! thank you




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to