Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2020 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | #!/usr/bin/env python |
| 6 | """ |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 7 | This is an example of how a script might make calls to monorail's v3 pRPC API. |
| 8 | |
| 9 | Usage example: |
| 10 | ``` |
| 11 | python v3_test_call.py \ |
| 12 | monorail.v3.Issues GetIssue '{"name": "projects/monorail/issues/404"}' |
| 13 | ``` |
| 14 | |
| 15 | The email of your service account should be allow-listed with Monorail. |
| 16 | """ |
| 17 | |
| 18 | import argparse |
| 19 | import json |
| 20 | import logging |
| 21 | import os |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | import requests |
| 23 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 24 | |
| 25 | # Older versions of https://github.com/googleapis/google-auth-library-python |
| 26 | # do not have the fetch_id_token() method called below. |
| 27 | # v1.15.0 or later should be fine. |
| 28 | from google.oauth2 import id_token |
| 29 | from google.auth.transport import requests as google_requests |
| 30 | |
| 31 | # Download and save your service account credentials file in |
| 32 | # api/service-account-key.json. |
| 33 | # id_token.fetch_id_token looks inside GOOGLE_APPLICATION_CREDENTIALS to fetch |
| 34 | # service account credentials. |
| 35 | os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'service-account-key.json' |
| 36 | |
| 37 | # BASE_URL can point to any monorail-dev api service version. |
| 38 | # However, you MAY get ssl cert errors when BASE_URL is not the |
| 39 | # default below. If this happens you will have to test your version |
| 40 | # by using the existing BASE_URL and migrating all traffic to your api |
| 41 | # version via pantheon. |
| 42 | BASE_URL = 'https://api-dot-monorail-dev.appspot.com/prpc' |
| 43 | |
| 44 | # TARGET_AUDIENCE should not change as long as BASE_URL is pointing to |
| 45 | # some monorail-dev version. If BASE_URL is updated to point to |
| 46 | # monorail-{staging|prod}, update TARGET_AUDIENCE accordingly. |
| 47 | TARGET_AUDIENCE = 'https://monorail-dev.appspot.com' |
| 48 | |
| 49 | # XSSI_PREFIX found at the beginning of every prpc response. |
| 50 | XSSI_PREFIX = ")]}'\n" |
| 51 | |
| 52 | import httplib2 |
| 53 | from oauth2client.client import GoogleCredentials |
| 54 | |
| 55 | |
| 56 | def make_call(service, method, json_body): |
| 57 | # Fetch ID token |
| 58 | request = google_requests.Request() |
| 59 | token = id_token.fetch_id_token(request, TARGET_AUDIENCE) |
| 60 | # Note: ID tokens for service accounts can also be fetched with with the |
| 61 | # Cloud IAM API projects.serviceAccounts.generateIdToken |
| 62 | # generateIdToken only needs the service account email or ID and the |
| 63 | # target_audience. |
| 64 | |
| 65 | # Call monorail's API. |
| 66 | headers = { |
| 67 | 'Authorization': 'Bearer %s' % token, |
| 68 | 'Content-Type': 'application/json', |
| 69 | 'Accept': 'application/json', |
| 70 | } |
| 71 | |
| 72 | url = "%s/%s/%s" % (BASE_URL, service, method) |
| 73 | |
| 74 | body = json.loads(json_body) |
| 75 | resp = requests.post(url, data=json.dumps(body), headers=headers) |
| 76 | logging.info(resp) |
| 77 | logging.info(resp.text) |
| 78 | logging.info(resp.content) |
| 79 | logging.info(json.dumps(json.loads(resp.content[len(XSSI_PREFIX):]))) |
| 80 | |
| 81 | # Verify and decode ID token to take a look at what's inside. |
| 82 | # API users should not have to do this. This is just for learning about |
| 83 | # how ID tokens work. |
| 84 | request = google_requests.Request() |
| 85 | id_info = id_token.verify_oauth2_token(token, request) |
| 86 | logging.info('id_info %s' % id_info) |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | parser = argparse.ArgumentParser(description='Process some integers.') |
| 91 | parser.add_argument('service', help='pRPC service name.') |
| 92 | parser.add_argument('method', help='pRPC method name.') |
| 93 | parser.add_argument('json_body', help='pRPC HTTP body in valid JSON.') |
| 94 | args = parser.parse_args() |
| 95 | log_level = logging.INFO |
| 96 | logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level) |
| 97 | make_call(args.service, args.method, args.json_body) |