blob: c7eaa2cf483f52f0973efe813476bdb34bf9d909 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4
5#!/usr/bin/env python
6"""
Copybara854996b2021-09-07 19:36:02 +00007This is an example of how a script might make calls to monorail's v3 pRPC API.
8
9Usage example:
10```
11python v3_test_call.py \
12monorail.v3.Issues GetIssue '{"name": "projects/monorail/issues/404"}'
13```
14
15The email of your service account should be allow-listed with Monorail.
16"""
17
18import argparse
19import json
20import logging
21import os
Copybara854996b2021-09-07 19:36:02 +000022import requests
23
Copybara854996b2021-09-07 19:36:02 +000024
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.
28from google.oauth2 import id_token
29from 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.
35os.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.
42BASE_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.
47TARGET_AUDIENCE = 'https://monorail-dev.appspot.com'
48
49# XSSI_PREFIX found at the beginning of every prpc response.
50XSSI_PREFIX = ")]}'\n"
51
52import httplib2
53from oauth2client.client import GoogleCredentials
54
55
56def 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
89if __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)