Skip to content

Commit 1483a1a

Browse files
committed
added refresh token, added getting num nodes, cleanup numpy in sliderule.py
1 parent 18795ac commit 1483a1a

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

sliderule/icesat2.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,21 @@ def __get_values(data, dtype, size):
303303
"""
304304

305305
raw = bytes(data)
306-
datatype = sliderule.basictypes[sliderule.codedtype2str[dtype]]["nptype"]
306+
datatype = {
307+
"INT8": numpy.int8,
308+
"INT16": numpy.int16,
309+
"INT32": numpy.int32,
310+
"INT64": numpy.int64,
311+
"UINT8": numpy.uint8,
312+
"UINT16": numpy.uint16,
313+
"UINT32": numpy.uint32,
314+
"UINT64": numpy.uint64,
315+
"BITFIELD": numpy.byte, # unsupported
316+
"FLOAT": numpy.single,
317+
"DOUBLE": numpy.double,
318+
"TIME8": numpy.byte,
319+
"STRING": numpy.byte
320+
}[sliderule.codedtype2str[dtype]]
307321
num_elements = int(size / numpy.dtype(datatype).itemsize)
308322
slicesize = num_elements * numpy.dtype(datatype).itemsize # truncates partial bytes
309323
values = numpy.frombuffer(raw[:slicesize], dtype=datatype, count=num_elements)

sliderule/sliderule.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import os
3131
import netrc
3232
import requests
33-
import numpy
3433
import json
3534
import struct
3635
import ctypes
36+
import time
3737
import logging
3838
from datetime import datetime, timedelta
3939

@@ -49,6 +49,7 @@
4949

5050
ps_refresh_token = None
5151
ps_access_token = None
52+
ps_token_exp = None
5253

5354
verbose = False
5455

@@ -82,19 +83,19 @@
8283
}
8384

8485
basictypes = {
85-
"INT8": { "fmt": 'b', "size": 1, "nptype": numpy.int8 },
86-
"INT16": { "fmt": 'h', "size": 2, "nptype": numpy.int16 },
87-
"INT32": { "fmt": 'i', "size": 4, "nptype": numpy.int32 },
88-
"INT64": { "fmt": 'q', "size": 8, "nptype": numpy.int64 },
89-
"UINT8": { "fmt": 'B', "size": 1, "nptype": numpy.uint8 },
90-
"UINT16": { "fmt": 'H', "size": 2, "nptype": numpy.uint16 },
91-
"UINT32": { "fmt": 'I', "size": 4, "nptype": numpy.uint32 },
92-
"UINT64": { "fmt": 'Q', "size": 8, "nptype": numpy.uint64 },
93-
"BITFIELD": { "fmt": 'x', "size": 0, "nptype": numpy.byte }, # unsupported
94-
"FLOAT": { "fmt": 'f', "size": 4, "nptype": numpy.single },
95-
"DOUBLE": { "fmt": 'd', "size": 8, "nptype": numpy.double },
96-
"TIME8": { "fmt": 'Q', "size": 8, "nptype": numpy.byte },
97-
"STRING": { "fmt": 's', "size": 1, "nptype": numpy.byte }
86+
"INT8": { "fmt": 'b', "size": 1 },
87+
"INT16": { "fmt": 'h', "size": 2 },
88+
"INT32": { "fmt": 'i', "size": 4 },
89+
"INT64": { "fmt": 'q', "size": 8 },
90+
"UINT8": { "fmt": 'B', "size": 1 },
91+
"UINT16": { "fmt": 'H', "size": 2 },
92+
"UINT32": { "fmt": 'I', "size": 4 },
93+
"UINT64": { "fmt": 'Q', "size": 8 },
94+
"BITFIELD": { "fmt": 'x', "size": 0 }, # unsupported
95+
"FLOAT": { "fmt": 'f', "size": 4 },
96+
"DOUBLE": { "fmt": 'd', "size": 8 },
97+
"TIME8": { "fmt": 'Q', "size": 8 },
98+
"STRING": { "fmt": 's', "size": 1 }
9899
}
99100

100101
codedtype2str = {
@@ -342,7 +343,7 @@ def __exceptrec(rec):
342343
#
343344
# SOURCE
344345
#
345-
def source (api, parm={}, stream=False, callbacks={}):
346+
def source (api, parm={}, stream=False, callbacks={}, path="/source"):
346347
'''
347348
Perform API call to SlideRule service
348349
@@ -354,10 +355,10 @@ def source (api, parm={}, stream=False, callbacks={}):
354355
dictionary of request parameters
355356
stream: bool
356357
whether the request is a **normal** service or a **stream** service (see `De-serialization <./SlideRule.html#de-serialization>`_ for more details)
357-
format: str
358-
format of the data being returned in the response: native, json
359358
callbacks: dict
360359
record type callbacks (advanced use)
360+
path: str
361+
path to api being requested
361362
362363
Returns
363364
-------
@@ -377,22 +378,32 @@ def source (api, parm={}, stream=False, callbacks={}):
377378
>>> print(rsps)
378379
{'time': 1300556199523.0, 'format': 'GPS'}
379380
'''
380-
global service_url, service_org
381+
global service_url, service_org, ps_access_token, ps_refresh_token, ps_token_exp
381382
rqst = json.dumps(parm)
382383
rsps = {}
383384
headers = None
384-
# Build callbacks
385+
# Build Callbacks
385386
for c in __callbacks:
386387
if c not in callbacks:
387388
callbacks[c] = __callbacks[c]
388-
# Construct Request URL and Authorization #
389+
# Construct Request URL and Authorization
389390
if service_org:
390-
url = 'https://%s.%s/source/%s' % (service_org, service_url, api)
391+
url = 'https://%s.%s%s/%s' % (service_org, service_url, path, api)
391392
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
392403
headers = {'Authorization': 'Bearer ' + ps_access_token}
393404
else:
394-
url = 'http://%s/source/%s' % (service_url, api)
395-
# Attempt Request #
405+
url = 'http://%s%s/%s' % (service_url, path, api)
406+
# Attempt Request
396407
try:
397408
# Perform Request
398409
if not stream:
@@ -404,6 +415,8 @@ def source (api, parm={}, stream=False, callbacks={}):
404415
format = data.headers['Content-Type']
405416
if format == 'text/plain':
406417
rsps = __parse_json(data)
418+
elif format == 'application/json':
419+
rsps = __parse_json(data)
407420
elif format == 'application/octet-stream':
408421
rsps = __parse_native(data, callbacks)
409422
else:
@@ -534,8 +547,10 @@ def update_available_servers (desired_nodes=None):
534547
>>> import sliderule
535548
>>> num_servers, max_workers = sliderule.update_available_servers(10)
536549
'''
537-
# placeholder until functionality implemented
538-
return 7,7
550+
551+
rsps = source("status", parm={"service":"sliderule"}, path="/discovery")
552+
available_servers = rsps["nodes"]
553+
return available_servers, available_servers
539554

540555
#
541556
# AUTHENTICATE
@@ -569,7 +584,7 @@ def authenticate (ps_organization, ps_username=None, ps_password=None):
569584
>>> sliderule.authenticate("myorg")
570585
True
571586
'''
572-
global service_org, ps_refresh_token, ps_access_token
587+
global service_org, ps_refresh_token, ps_access_token, ps_token_exp
573588
login_status = False
574589
ps_url = "ps." + service_url
575590

@@ -604,6 +619,7 @@ def authenticate (ps_organization, ps_username=None, ps_password=None):
604619
rsps = requests.post(api, data=json.dumps(rqst), headers=headers, timeout=request_timeout).json()
605620
ps_refresh_token = rsps["refresh"]
606621
ps_access_token = rsps["access"]
622+
ps_token_exp = time.time() + (rsps["access_lifetime"] / 2)
607623
login_status = True
608624
except:
609625
logger.error("Unable to authenticate user %s to %s" % (ps_username, api))

0 commit comments

Comments
 (0)