|
5 | 5 | import sys
|
6 | 6 | import json
|
7 | 7 | import requests
|
| 8 | +import logging |
8 | 9 |
|
9 | 10 | ###############################################################################
|
10 | 11 | # MAIN
|
|
16 | 17 | password = sys.argv[2]
|
17 | 18 | organization = sys.argv[3]
|
18 | 19 | url = "slideruleearth.io"
|
| 20 | + verbose = False |
19 | 21 |
|
20 | 22 | # Set URL
|
21 | 23 | if len(sys.argv) > 4:
|
22 | 24 | url = sys.argv[4]
|
23 | 25 |
|
| 26 | + # Set Verbose |
| 27 | + if len(sys.argv) > 5: |
| 28 | + verbose = sys.argv[5] == "verbose" |
| 29 | + |
| 30 | + # Configure Debug Logging |
| 31 | + if verbose: |
| 32 | + import http.client as http_client |
| 33 | + http_client.HTTPConnection.debuglevel = 1 |
| 34 | + |
| 35 | + # You must initialize logging, otherwise you'll not see debug output. |
| 36 | + logging.basicConfig() |
| 37 | + logging.getLogger().setLevel(logging.DEBUG) |
| 38 | + requests_log = logging.getLogger("requests.packages.urllib3") |
| 39 | + requests_log.setLevel(logging.DEBUG) |
| 40 | + requests_log.propagate = True |
| 41 | + |
| 42 | + # Create Session |
| 43 | + session = requests.Session() |
| 44 | + session.trust_env = False |
| 45 | + |
24 | 46 | # Authentication Request
|
25 | 47 | host = "https://ps." + url + "/api/org_token/"
|
26 | 48 | rqst = {"username": username, "password": password, "org_name": organization}
|
27 | 49 | headers = {'Content-Type': 'application/json'}
|
28 |
| - rsps = requests.post(host, data=json.dumps(rqst), headers=headers, timeout=(60,10)).json() |
| 50 | + rsps = session.post(host, data=json.dumps(rqst), headers=headers, timeout=(60,10)).json() |
29 | 51 | refresh = rsps["refresh"]
|
30 | 52 | access = rsps["access"]
|
31 | 53 | print("Login Response: ", rsps)
|
32 | 54 |
|
33 | 55 | # Organization Access Request
|
34 | 56 | host = "https://ps." + url + "/api/membership_status/" + organization + "/"
|
35 | 57 | headers = {'Authorization': 'Bearer ' + access}
|
36 |
| - rsps = requests.get(host, headers=headers, timeout=(60,10)).json() |
| 58 | + rsps = session.get(host, headers=headers, timeout=(60,10)).json() |
37 | 59 | print("Validation Response: ", rsps)
|
38 | 60 |
|
39 |
| - # Refresh Request |
| 61 | + # Refresh Request 1 |
| 62 | + host = "https://ps." + url + "/api/org_token/refresh/" |
| 63 | + rqst = {"refresh": refresh} |
| 64 | + headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + access} |
| 65 | + rsps = session.post(host, data=json.dumps(rqst), headers=headers, timeout=(60,10)).json() |
| 66 | + refresh = rsps["refresh"] |
| 67 | + access = rsps["access"] |
| 68 | + print("Refresh 1 Response: ", rsps) |
| 69 | + |
| 70 | + # Refresh Request 2 |
40 | 71 | host = "https://ps." + url + "/api/org_token/refresh/"
|
41 | 72 | rqst = {"refresh": refresh}
|
42 | 73 | headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + access}
|
43 |
| - rsps = requests.post(host, data=json.dumps(rqst), headers=headers, timeout=(60,10)).json() |
44 |
| - print("Refresh Response: ", rsps) |
| 74 | + rsps = session.post(host, data=json.dumps(rqst), headers=headers, timeout=(60,10)).json() |
| 75 | + refresh = rsps["refresh"] |
| 76 | + access = rsps["access"] |
| 77 | + print("Refresh 2 Response: ", rsps) |
0 commit comments