-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathokta_requests.py
46 lines (38 loc) · 1.63 KB
/
okta_requests.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
import time
import requests
import datetime
import os
# If you're making multiple API calls, using Session is much faster.
class Session(requests.Session):
def __init__(self, org_url=os.getenv('OKTA_CLIENT_ORGURL'), token=os.getenv('OKTA_CLIENT_TOKEN')):
super().__init__()
self.org_url = org_url
self.headers['authorization'] = 'SSWS ' + token
def request(self, method, url, **kwargs):
if not url.startswith('https:'):
url = self.org_url + url
response = super().request(method, url, **kwargs)
limit = int(response.headers['X-Rate-Limit-Limit'])
remaining = int(response.headers['X-Rate-Limit-Remaining'])
reset = datetime.datetime.fromtimestamp(int(response.headers['X-Rate-Limit-Reset']), datetime.UTC)
now = datetime.datetime.now(datetime.UTC)
print(limit, remaining, reset)
if remaining < 10:
print('sleeping...')
while reset > now:
time.sleep(1) # TODO: calculate sleep time
now = datetime.datetime.now(datetime.UTC)
return response
def post(self, url, json=None):
return super().post(url, json=json)
def put(self, url, json=None):
return super().put(url, json=json)
def patch(self, url, json=None):
return super().patch(url, json=json)
def get_objects(self, url, **params):
while url:
response = self.get(url, params=params)
params = None
for o in response.json():
yield o
url = response.links.get('next', {}).get('url')