Skip to content

Commit

Permalink
refresh token support
Browse files Browse the repository at this point in the history
  • Loading branch information
ebiiii committed Sep 25, 2016
1 parent 2de04e7 commit dc736ad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
CHANGES
=======

v0.2.0
-------

* adding refresh token support
* automatically tracking the expiration of access token and refreshing if necessary


v0.1.0
-------

Expand Down
21 changes: 19 additions & 2 deletions gsn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sanction import Client, transport_headers
from .sensor import Sensor
import time
try:
from urllib2 import HTTPError
except:
Expand Down Expand Up @@ -27,13 +28,28 @@ def __init__(self, service_url=None, client_id=None, client_secret=None, redirec
)
self.client.request_token(grant_type='client_credentials', redirect_uri=redirect_uri)

assert hasattr(self.client, 'expires_in')

self.expiration = time.time() + self.client.expires_in

def refresh_token(self):
""" Renew the access token by submitting a request with the previously
received refresh_token
"""
assert hasattr(self.client, 'refresh_token')

self.client.request_token(grant_type='refresh_token')

self.expiration = time.time() + self.client.expires_in

def get_latest_values(self, vs_name=None):
""" Query the API to get the latest values of a given virtual sensor.
:param vs_name: The name of the virtual sensor.
:returns: A Sensor object.
"""
assert vs_name is not None

if self.expiration <= time.time():
self.refresh_token()
data = self.client.request("/sensors/{}?latestValues=True".format(vs_name))
return Sensor(geojson_object=data)

Expand All @@ -45,7 +61,8 @@ def push_values(self, sensor_data=None):
"""

assert sensor_data is not None

if self.expiration <= time.time():
self.refresh_token()
try:
res = self.client.request("/sensors/{}/data".format(sensor_data.name),
data=sensor_data.to_geojson().encode('utf_8'),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
name='gsn',

# Versions should comply with PEP440.
version='0.1.0',
version='0.2.0',

description='A python wrapper for Global Sensor Networks API',
long_description=long_description,
Expand Down

0 comments on commit dc736ad

Please sign in to comment.