Skip to content

Commit

Permalink
Merge pull request #6 from allthingstalk/feature/user-agent
Browse files Browse the repository at this point in the history
Adds support for User-Agent string
  • Loading branch information
rayalex authored May 17, 2018
2 parents e5b43c3 + 1a4887d commit e0463db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ celerybeat-schedule

# virtualenv
venv/
.venv/
ENV/

# Spyder project settings
Expand All @@ -90,3 +91,6 @@ ENV/

scratchpad.py
*~

# Tools
.vscode/
22 changes: 16 additions & 6 deletions allthingstalk/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# limitations under the License.

import logging
import pkg_resources

import paho.mqtt.client as paho_mqtt
import requests
Expand Down Expand Up @@ -95,6 +96,7 @@ def prefix_http(url):
self.mqtt = None

self._devices = {}
self._version = self._get_version()

def _make_mqtt_client(self, host, port, token):
def on_mqtt_connect(client, userdata, rc):
Expand Down Expand Up @@ -139,8 +141,7 @@ def get_assets(self, device_id):
:rtype: list of Asset
"""

r = requests.get('%s/device/%s/assets' % (self.http, device_id),
headers={'Authorization': 'Bearer %s' % self.token})
r = requests.get('%s/device/%s/assets' % (self.http, device_id), headers=self._get_headers())
if r.status_code == 403:
raise AccessForbiddenException('Could not use token "%s" to access device "%s" on "%s".'
% (self.token, device_id, self.http))
Expand All @@ -163,7 +164,7 @@ def create_asset(self, device_id, asset):
'Profile': asset.profile
}
asset_dict = requests.post('%s/device/%s/assets' % (self.http, device_id),
headers={'Authorization': 'Bearer %s' % self.token},
headers=self._get_headers(),
json=attalk_asset).json()
return Asset.from_dict(asset_dict)

Expand All @@ -177,8 +178,8 @@ def get_asset_state(self, device_id, asset_name):
:rtype: AssetState
"""

r = requests.get('%s/device/%s/asset/%s/state' % (self.http, device_id, asset_name),
headers={'Authorization': 'Bearer %s' % self.token})
r = requests.get('%s/device/%s/asset/%s/state' %
(self.http, device_id, asset_name), headers=self._get_headers())
if r.status_code != 200:
raise AssetStateRetrievalException()
response_json = r.json()
Expand All @@ -200,9 +201,18 @@ def publish_asset_state(self, device_id, asset_name, state):
else:
json_state = {'value': state}
requests.put('%s/device/%s/asset/%s/state' % (self.http, device_id, asset_name),
headers={'Authorization': 'Bearer %s' % self.token},
headers=self._get_headers(),
json=json_state)

def _get_headers(self):
return {
'Authorization': 'Bearer %s' % self.token,
'User-Agent': 'ATTalk-PythonSDK/%s' % self._get_version()
}

def _get_version(self):
return pkg_resources.require('allthingstalk')[0].version

def __del__(self):
try:
self.mqtt.loop_stop(force=True)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

setup(
name='allthingstalk',
version='0.2.4',
version='0.2.5',
description='AllThingsTalk Python SDK',
url='https://github.com/allthingstalk/python-sdk',
download_url='https://github.com/allthingstalk/python-sdk/archive/0.2.4.tar.gz',
download_url='https://github.com/allthingstalk/python-sdk/archive/0.2.5.tar.gz',
author='Danilo Vidovic',
author_email='[email protected]',
license='Apache 2.0',
Expand Down

0 comments on commit e0463db

Please sign in to comment.