-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_sample.py
62 lines (46 loc) · 1.91 KB
/
http_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
from os import path
import google.auth
import google.auth.credentials
from google.auth.transport import mtls
import google.auth.transport.requests
import google.auth.transport.urllib3
from google.oauth2 import credentials
MTLS_ENDPOINT = "https://pubsub.mtls.googleapis.com/v1/projects/{}/topics"
REGULAR_ENDPOINT = "https://pubsub.googleapis.com/v1/projects/{}/topics"
project_id = "sijun-mtls-demo"
credentials = credentials.UserAccessTokenCredentials()
def use_requests():
print("\n\n====== Test http call with requests lib =====\n")
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
if mtls.has_default_client_cert_source():
print("= has default client cert source=")
callback = mtls.default_client_cert_source()
else:
callback = None
authed_session.configure_mtls_channel(client_cert_callback=callback)
if authed_session.is_mtls:
print("= using mtls channel=")
response = authed_session.get(MTLS_ENDPOINT.format(project_id))
else:
print("= using regular channel=")
response = authed_session.get(REGULAR_ENDPOINT.format(project_id))
print(response.content)
def use_urllib3():
print("\n\n====== Test http call with urllib3 lib =====\n")
authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
if mtls.has_default_client_cert_source():
print("= has default client cert source=")
callback = mtls.default_client_cert_source()
else:
callback = None
is_mtls = authed_http.configure_mtls_channel(client_cert_callback=callback)
if is_mtls:
print("= using mtls channel=")
response = authed_http.request("GET", MTLS_ENDPOINT.format(project_id))
else:
print("= using regular channel=")
response = authed_http.request("GET", REGULAR_ENDPOINT.format(project_id))
print(response.data)
use_requests()
use_urllib3()