Break out SubscribeKafkaTopic function.
authorFrederik Vanrenterghem <frederik@vanrenterghem.io>
Fri, 14 Sep 2018 12:04:58 +0000 (20:04 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.io>
Fri, 14 Sep 2018 12:04:58 +0000 (20:04 +0800)
R/SubscribeKafkaTopic.R [new file with mode: 0644]
kafkaConsumer.R

diff --git a/R/SubscribeKafkaTopic.R b/R/SubscribeKafkaTopic.R
new file mode 100644 (file)
index 0000000..5e6976b
--- /dev/null
@@ -0,0 +1,12 @@
+SubscribeKafkaTopic <- function(consumer.base.uri, topic) {
+  response <- POST(url=paste(consumer.base.uri,
+                             "subscription", sep="/"),
+                   content_type("application/vnd.kafka.v2+json"),
+                   body = paste0('{"topics":["',
+                                 topic,
+                                 '"]}')
+  )
+  if(response$status_code == 204) {
+    return(response)
+  } else stop(response)
+}
\ No newline at end of file
index d6b092b..969fabd 100644 (file)
@@ -19,14 +19,8 @@ source("R/CreateKafkaConsumer.R")
 consumerDetails <- CreateKafkaConsumer(kafka.rest.proxy = kafka_rest_proxy, consumer = consumer, consumer_instance = consumer_instance)
 
 # Subscribe it to topic
-response <- POST(url=paste(consumerDetails$base_uri,
-                           "subscription", sep="/"),
-               content_type("application/vnd.kafka.v2+json"),
-               body = paste0('{"topics":["',
-                             topic,
-                             '"]}')
-               )
-response
+source("R/SubscribeKafkaTopic.R")
+response <- SubscribeKafkaTopic(consumerDetails$base_uri,topic = topic)
 
 # Obtain all (or latest) messages on the topic
 source("R/ConsumeKafkaMessages.R")