Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame^] | 1 | # Copyright 2016 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import google.auth |
| 16 | import google.auth.credentials |
| 17 | import google.auth.transport.grpc |
| 18 | from google.cloud.gapic.pubsub.v1 import publisher_api |
| 19 | |
| 20 | |
| 21 | def test_grpc_request(http_request): |
| 22 | credentials, project_id = google.auth.default() |
| 23 | credentials = google.auth.credentials.with_scopes_if_required( |
| 24 | credentials, ['https://www.googleapis.com/auth/pubsub']) |
| 25 | |
| 26 | target = '{}:{}'.format( |
| 27 | publisher_api.PublisherApi.SERVICE_ADDRESS, |
| 28 | publisher_api.PublisherApi.DEFAULT_SERVICE_PORT) |
| 29 | |
| 30 | channel = google.auth.transport.grpc.secure_authorized_channel( |
| 31 | credentials, target, http_request) |
| 32 | |
| 33 | # Create a pub/sub client. |
| 34 | client = publisher_api.PublisherApi(channel=channel) |
| 35 | |
| 36 | # list the topics and drain the iterator to test that an authorized API |
| 37 | # call works. |
| 38 | list_topics_iter = client.list_topics( |
| 39 | project='projects/{}'.format(project_id)) |
| 40 | list(list_topics_iter) |