當前位置: 首頁>>代碼示例>>Python>>正文


Python pubsub_v1.PublisherClient方法代碼示例

本文整理匯總了Python中google.cloud.pubsub_v1.PublisherClient方法的典型用法代碼示例。如果您正苦於以下問題:Python pubsub_v1.PublisherClient方法的具體用法?Python pubsub_v1.PublisherClient怎麽用?Python pubsub_v1.PublisherClient使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在google.cloud.pubsub_v1的用法示例。


在下文中一共展示了pubsub_v1.PublisherClient方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_grpc_request_with_regular_credentials

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_grpc_request_with_regular_credentials(http_request):
    credentials, project_id = google.auth.default()
    credentials = google.auth.credentials.with_scopes_if_required(
        credentials, ["https://www.googleapis.com/auth/pubsub"]
    )

    transport = publisher_grpc_transport.PublisherGrpcTransport(
        address=publisher_client.PublisherClient.SERVICE_ADDRESS,
        credentials=credentials,
    )

    # Create a pub/sub client.
    client = pubsub_v1.PublisherClient(transport=transport)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
    list(list_topics_iter) 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:20,代碼來源:test_grpc.py

示例2: test_grpc_request_with_jwt_credentials

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_grpc_request_with_jwt_credentials():
    credentials, project_id = google.auth.default()
    audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
    credentials = google.auth.jwt.Credentials.from_signing_credentials(
        credentials, audience=audience
    )

    transport = publisher_grpc_transport.PublisherGrpcTransport(
        address=publisher_client.PublisherClient.SERVICE_ADDRESS,
        credentials=credentials,
    )

    # Create a pub/sub client.
    client = pubsub_v1.PublisherClient(transport=transport)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
    list(list_topics_iter) 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:21,代碼來源:test_grpc.py

示例3: test_grpc_request_with_on_demand_jwt_credentials

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_grpc_request_with_on_demand_jwt_credentials():
    credentials, project_id = google.auth.default()
    credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
        credentials
    )

    transport = publisher_grpc_transport.PublisherGrpcTransport(
        address=publisher_client.PublisherClient.SERVICE_ADDRESS,
        credentials=credentials,
    )

    # Create a pub/sub client.
    client = pubsub_v1.PublisherClient(transport=transport)

    # list the topics and drain the iterator to test that an authorized API
    # call works.
    list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
    list(list_topics_iter) 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:20,代碼來源:test_grpc.py

示例4: pub

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def pub(project_id, topic_id):
    """Publishes a message to a Pub/Sub topic."""
    # [START pubsub_quickstart_pub_client]
    # Initialize a Publisher client.
    client = pubsub_v1.PublisherClient()
    # [END pubsub_quickstart_pub_client]
    # Create a fully qualified identifier in the form of
    # `projects/{project_id}/topics/{topic_id}`
    topic_path = client.topic_path(project_id, topic_id)

    # Data sent to Cloud Pub/Sub must be a bytestring.
    data = b"Hello, World!"

    # Keep track of the number of published messages.
    ref = dict({"num_messages": 0})

    # When you publish a message, the client returns a future.
    api_future = client.publish(topic_path, data=data)
    api_future.add_done_callback(get_callback(api_future, data, ref))

    # Keep the main thread from exiting while the message future
    # gets resolved in the background.
    while api_future.running():
        time.sleep(0.5)
        print("Published {} message(s).".format(ref["num_messages"])) 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:27,代碼來源:pub.py

示例5: get_topic_policy

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def get_topic_policy(project, topic_id):
    """Prints the IAM policy for the given topic."""
    # [START pubsub_get_topic_policy]
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"

    client = pubsub_v1.PublisherClient()
    topic_path = client.topic_path(project, topic_id)

    policy = client.get_iam_policy(topic_path)

    print("Policy for topic {}:".format(topic_path))
    for binding in policy.bindings:
        print("Role: {}, Members: {}".format(binding.role, binding.members))
    # [END pubsub_get_topic_policy] 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:20,代碼來源:iam.py

示例6: check_topic_permissions

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def check_topic_permissions(project, topic_id):
    """Checks to which permissions are available on the given topic."""
    # [START pubsub_test_topic_permissions]
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"

    client = pubsub_v1.PublisherClient()
    topic_path = client.topic_path(project, topic_id)

    permissions_to_check = ["pubsub.topics.publish", "pubsub.topics.update"]

    allowed_permissions = client.test_iam_permissions(topic_path, permissions_to_check)

    print(
        "Allowed permissions for topic {}: {}".format(topic_path, allowed_permissions)
    )
    # [END pubsub_test_topic_permissions] 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:22,代碼來源:iam.py

示例7: create_topic

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def create_topic(project_id, topic_id):
    """Create a new Pub/Sub topic."""
    # [START pubsub_quickstart_create_topic]
    # [START pubsub_create_topic]
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    topic = publisher.create_topic(topic_path)

    print("Topic created: {}".format(topic))
    # [END pubsub_quickstart_create_topic]
    # [END pubsub_create_topic] 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:20,代碼來源:publisher.py

示例8: delete_topic

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def delete_topic(project_id, topic_id):
    """Deletes an existing Pub/Sub topic."""
    # [START pubsub_delete_topic]
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    publisher.delete_topic(topic_path)

    print("Topic deleted: {}".format(topic_path))
    # [END pubsub_delete_topic] 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:18,代碼來源:publisher.py

示例9: publish_messages_with_custom_attributes

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def publish_messages_with_custom_attributes(project_id, topic_id):
    """Publishes multiple messages with custom attributes
    to a Pub/Sub topic."""
    # [START pubsub_publish_custom_attributes]
    from google.cloud import pubsub_v1

    # TODO(developer)
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    for n in range(1, 10):
        data = u"Message number {}".format(n)
        # Data must be a bytestring
        data = data.encode("utf-8")
        # Add two attributes, origin and username, to the message
        future = publisher.publish(
            topic_path, data, origin="python-sample", username="gcp"
        )
        print(future.result())

    print("Published messages with custom attributes.")
    # [END pubsub_publish_custom_attributes] 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:27,代碼來源:publisher.py

示例10: index

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def index():
    if request.method == 'GET':
        return render_template('index.html', messages=MESSAGES)

    data = request.form.get('payload', 'Example payload').encode('utf-8')

    # publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(
        current_app.config['PROJECT'],
        current_app.config['PUBSUB_TOPIC'])

    publisher.publish(topic_path, data=data)

    return 'OK', 200
# [END gae_flex_pubsub_index]


# [START gae_flex_pubsub_push] 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:20,代碼來源:main.py

示例11: index

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def index():
    if request.method == 'GET':
        return render_template('index.html', messages=MESSAGES, tokens=TOKENS,
                               claims=CLAIMS)

    data = request.form.get('payload', 'Example payload').encode('utf-8')

    # Consider initializing the publisher client outside this function
    # for better latency performance.
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(app.config['GOOGLE_CLOUD_PROJECT'],
                                      app.config['PUBSUB_TOPIC'])
    future = publisher.publish(topic_path, data)
    future.result()
    return 'OK', 200
# [END index]


# [START push] 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:21,代碼來源:main.py

示例12: test_create_feed

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_create_feed(capsys):
    client = resource_manager.Client()
    project_number = client.fetch_project(PROJECT).number
    full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(PROJECT, TOPIC)
    publisher.create_topic(topic_path)
    quickstart_createfeed.create_feed(
        PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
    out, _ = capsys.readouterr()
    assert "feed" in out

    # Clean up, delete the feed
    feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
    quickstart_deletefeed.delete_feed(feed_name)
    publisher.delete_topic(topic_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:18,代碼來源:quickstart_createfeed_test.py

示例13: test_update_feed

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_update_feed(capsys):
    client = resource_manager.Client()
    project_number = client.fetch_project(PROJECT).number
    # First create the feed, which will be updated later
    full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(PROJECT, TOPIC)
    publisher.create_topic(topic_path)
    quickstart_createfeed.create_feed(
        PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)

    feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
    new_full_topic_name = "projects/" + PROJECT + "/topics/" + NEW_TOPIC
    new_topic_path = publisher.topic_path(PROJECT, NEW_TOPIC)
    publisher.create_topic(new_topic_path)
    quickstart_updatefeed.update_feed(feed_name, new_full_topic_name)
    out, _ = capsys.readouterr()

    assert "updated_feed" in out
    # Clean up and delete the feed
    quickstart_deletefeed.delete_feed(feed_name)
    publisher.delete_topic(topic_path)
    publisher.delete_topic(new_topic_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:25,代碼來源:quickstart_updatefeed_test.py

示例14: test_delete_feed

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_delete_feed(capsys):
    client = resource_manager.Client()
    project_number = client.fetch_project(PROJECT).number
    # First create the feed, which will be deleted later
    full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(PROJECT, TOPIC)
    publisher.create_topic(topic_path)
    quickstart_createfeed.create_feed(
        PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)

    feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
    quickstart_deletefeed.delete_feed(feed_name)

    out, _ = capsys.readouterr()
    assert "deleted_feed" in out
    publisher.delete_topic(topic_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:19,代碼來源:quickstart_deletefeed_test.py

示例15: test_get_feed

# 需要導入模塊: from google.cloud import pubsub_v1 [as 別名]
# 或者: from google.cloud.pubsub_v1 import PublisherClient [as 別名]
def test_get_feed(capsys):
    client = resource_manager.Client()
    project_number = client.fetch_project(PROJECT).number
    # First create the feed, which will be gotten later
    full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(PROJECT, TOPIC)
    publisher.create_topic(topic_path)
    quickstart_createfeed.create_feed(
        PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)

    feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
    quickstart_getfeed.get_feed(feed_name)
    out, _ = capsys.readouterr()

    assert "gotten_feed" in out
    # Clean up and delete the feed
    quickstart_deletefeed.delete_feed(feed_name)
    publisher.delete_topic(topic_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:21,代碼來源:quickstart_getfeed_test.py


注:本文中的google.cloud.pubsub_v1.PublisherClient方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。