Skip to content

Commit f3ab4bd

Browse files
committed
added desired_or_num_nodes api
1 parent 1483a1a commit f3ab4bd

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

sliderule/sliderule.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,31 @@ def __parse_native(data, callbacks):
310310

311311
return recs
312312

313+
#
314+
# __build_auth_header
315+
#
316+
def __build_auth_header():
317+
"""
318+
Build authentication header for use with provisioning system
319+
"""
320+
321+
global service_url, ps_access_token, ps_refresh_token, ps_token_exp
322+
headers = None
323+
if ps_access_token:
324+
# Check if Refresh Needed
325+
if time.time() > ps_token_exp:
326+
host = "https://ps." + service_url + "/api/org_token/refresh/"
327+
rqst = {"refresh": ps_refresh_token}
328+
hdrs = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + ps_access_token}
329+
rsps = requests.post(host, data=json.dumps(rqst), headers=hdrs, timeout=request_timeout).json()
330+
ps_refresh_token = rsps["refresh"]
331+
ps_access_token = rsps["access"]
332+
ps_token_exp = time.time() + (rsps["access_lifetime"] / 2)
333+
# Build Authentication Header
334+
headers = {'Authorization': 'Bearer ' + ps_access_token}
335+
return headers
336+
337+
313338
###############################################################################
314339
# Default Record Processing
315340
###############################################################################
@@ -378,7 +403,7 @@ def source (api, parm={}, stream=False, callbacks={}, path="/source"):
378403
>>> print(rsps)
379404
{'time': 1300556199523.0, 'format': 'GPS'}
380405
'''
381-
global service_url, service_org, ps_access_token, ps_refresh_token, ps_token_exp
406+
global service_url, service_org
382407
rqst = json.dumps(parm)
383408
rsps = {}
384409
headers = None
@@ -389,18 +414,7 @@ def source (api, parm={}, stream=False, callbacks={}, path="/source"):
389414
# Construct Request URL and Authorization
390415
if service_org:
391416
url = 'https://%s.%s%s/%s' % (service_org, service_url, path, api)
392-
if ps_access_token:
393-
# Check if Refresh Needed
394-
if time.time() > ps_token_exp:
395-
refresh_host = "https://ps." + url + "/api/org_token/refresh/"
396-
refresh_rqst = {"refresh": ps_refresh_token}
397-
refresh_headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + ps_access_token}
398-
refresh_rsps = requests.post(refresh_host, data=json.dumps(refresh_rqst), headers=refresh_headers, timeout=(60,10)).json()
399-
ps_refresh_token = refresh_rsps["refresh"]
400-
ps_access_token = refresh_rsps["access"]
401-
ps_token_exp = time.time() + (refresh_rsps["access_lifetime"] / 2)
402-
# Build Authentication Header
403-
headers = {'Authorization': 'Bearer ' + ps_access_token}
417+
headers = __build_auth_header()
404418
else:
405419
url = 'http://%s%s/%s' % (service_url, path, api)
406420
# Attempt Request
@@ -548,6 +562,16 @@ def update_available_servers (desired_nodes=None):
548562
>>> num_servers, max_workers = sliderule.update_available_servers(10)
549563
'''
550564

565+
global service_url, service_org, request_timeout
566+
567+
# Update number of nodes
568+
if type(desired_nodes) == int:
569+
host = "https://ps." + service_url + "/api/desired_org_num_nodes/" + service_org + "/" + str(desired_nodes)
570+
headers = __build_auth_header()
571+
rsps = requests.get(host, headers=headers, timeout=request_timeout).json()
572+
rsps.raise_for_status()
573+
574+
# Get number of nodes currently registered
551575
rsps = source("status", parm={"service":"sliderule"}, path="/discovery")
552576
available_servers = rsps["nodes"]
553577
return available_servers, available_servers

0 commit comments

Comments
 (0)