-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-limits.py
executable file
·78 lines (59 loc) · 2.58 KB
/
test-limits.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import requests, json, time, sys
from config import *
#Suppress InsecureRequestWarning message in console when verify certificate option is set to false in API request
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Get key values required to access Enterprise API from config file; build complete header to accompany API request
if SESSION_ID:
myHeader = {'Authorization': 'Bearer ' + SESSION_ID,
'User-Agent': 'KBH Python Script to test API Limits',
'Host': HOST,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Accept-Charset': 'UTF-8'
}
else:
print("Session ID or Auth token not found - cannot proceed")
sys.exit(2)
############################################ FUNCTION DEFINITIONS #############################################
# Function that makes GET request
def apiGetRequest(url, certCheck):
try:
apiResponse = requests.get(url, headers=myHeader, timeout=120, verify=certCheck)
except:
print(apiResponse.status_code)
print(apiResponse.content)
apiResponse.close()
if apiResponse:
return apiResponse
else:
print(apiResponse.status_code)
print(apiResponse.content)
sys.exit(2)
######################################### END OF FUNCTION DEFINITIONS #########################################
print("Start: ", time.asctime( time.localtime(time.time()) )) #Log script start time to console
remainingLimit = 0
getObjectsUrl = "https://{0}/services/data/v{1}/sobjects/".format(HOST, VERSION)
getLimitUrl = "https://{0}/services/data/v{1}/limits/".format(HOST, VERSION)
limitsResponse = apiGetRequest(getLimitUrl, False)
requestsMade = 1
remainingLimit = limitsResponse.json()['DailyApiRequests']['Remaining']
print("Per Limits request: {0}".format(limitsResponse.headers['Sforce-Limit-Info']))
print(remainingLimit)
while remainingLimit > 0:
requestsMade += 1
print("Requests attempted: {0}".format(requestsMade))
objectsResponse = apiGetRequest(getObjectsUrl, False)
print(objectsResponse.headers['Sforce-Limit-Info'])
requestsMade += 1
print("Requests attempted: {0}".format(requestsMade))
limitsResponse = apiGetRequest(getLimitUrl, False)
remainingLimit = limitsResponse.json()['DailyApiRequests']['Remaining']
print(time.asctime( time.localtime(time.time()) ))
print(limitsResponse.headers['Sforce-Limit-Info'])
print("Remaining: ", remainingLimit)
print("End: ", time.asctime( time.localtime(time.time()) )) # Log script completion ending time to console
print(objectsResponse.headers['Sforce-Limit-Info'])
print(remainingLimit)
print("Requests submitted: {0}".format(requestsMade))