I am confused about the GCP Pub/Sub REST API.
Background: I am trying to write an application using GCP Pub/Sub where the language doesn't exit as a client library (I'm trying to use R).
Therefore, I will need to rely upon the REST API provided: https://cloud.google.com/pubsub/docs/reference/rest
Based on my understanding of the REST API, we would have to use the pull subscription: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/pull
Question: My confusion is that this is a POST request
POST https://pubsub.googleapis.com/v1/{subscription}:pull
This POST request has a response:
{
"receivedMessages": [
{
object (ReceivedMessage)
}
]
}
How could I receive a response from a POST request? This doesn't make sense to me.
My goal would be to subscribe to a Pub/Sub subscription for messages, similar to the Python library here:
To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that, passing a callback function.
import os
from google.cloud import pubsub_v1
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
)
def callback(message):
print(message.data)
message.ack()
with pubsub_v1.SubscriberClient() as subscriber:
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
future = subscriber.subscribe(subscription_name, callback)
try:
future.result()
except KeyboardInterrupt:
future.cancel()