Skip to content

Commit e0463db

Browse files
authored
Merge pull request #6 from allthingstalk/feature/user-agent
Adds support for User-Agent string
2 parents e5b43c3 + 1a4887d commit e0463db

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ celerybeat-schedule
8080

8181
# virtualenv
8282
venv/
83+
.venv/
8384
ENV/
8485

8586
# Spyder project settings
@@ -90,3 +91,6 @@ ENV/
9091

9192
scratchpad.py
9293
*~
94+
95+
# Tools
96+
.vscode/

allthingstalk/clients.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# limitations under the License.
2020

2121
import logging
22+
import pkg_resources
2223

2324
import paho.mqtt.client as paho_mqtt
2425
import requests
@@ -95,6 +96,7 @@ def prefix_http(url):
9596
self.mqtt = None
9697

9798
self._devices = {}
99+
self._version = self._get_version()
98100

99101
def _make_mqtt_client(self, host, port, token):
100102
def on_mqtt_connect(client, userdata, rc):
@@ -139,8 +141,7 @@ def get_assets(self, device_id):
139141
:rtype: list of Asset
140142
"""
141143

142-
r = requests.get('%s/device/%s/assets' % (self.http, device_id),
143-
headers={'Authorization': 'Bearer %s' % self.token})
144+
r = requests.get('%s/device/%s/assets' % (self.http, device_id), headers=self._get_headers())
144145
if r.status_code == 403:
145146
raise AccessForbiddenException('Could not use token "%s" to access device "%s" on "%s".'
146147
% (self.token, device_id, self.http))
@@ -163,7 +164,7 @@ def create_asset(self, device_id, asset):
163164
'Profile': asset.profile
164165
}
165166
asset_dict = requests.post('%s/device/%s/assets' % (self.http, device_id),
166-
headers={'Authorization': 'Bearer %s' % self.token},
167+
headers=self._get_headers(),
167168
json=attalk_asset).json()
168169
return Asset.from_dict(asset_dict)
169170

@@ -177,8 +178,8 @@ def get_asset_state(self, device_id, asset_name):
177178
:rtype: AssetState
178179
"""
179180

180-
r = requests.get('%s/device/%s/asset/%s/state' % (self.http, device_id, asset_name),
181-
headers={'Authorization': 'Bearer %s' % self.token})
181+
r = requests.get('%s/device/%s/asset/%s/state' %
182+
(self.http, device_id, asset_name), headers=self._get_headers())
182183
if r.status_code != 200:
183184
raise AssetStateRetrievalException()
184185
response_json = r.json()
@@ -200,9 +201,18 @@ def publish_asset_state(self, device_id, asset_name, state):
200201
else:
201202
json_state = {'value': state}
202203
requests.put('%s/device/%s/asset/%s/state' % (self.http, device_id, asset_name),
203-
headers={'Authorization': 'Bearer %s' % self.token},
204+
headers=self._get_headers(),
204205
json=json_state)
205206

207+
def _get_headers(self):
208+
return {
209+
'Authorization': 'Bearer %s' % self.token,
210+
'User-Agent': 'ATTalk-PythonSDK/%s' % self._get_version()
211+
}
212+
213+
def _get_version(self):
214+
return pkg_resources.require('allthingstalk')[0].version
215+
206216
def __del__(self):
207217
try:
208218
self.mqtt.loop_stop(force=True)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
setup(
44
name='allthingstalk',
5-
version='0.2.4',
5+
version='0.2.5',
66
description='AllThingsTalk Python SDK',
77
url='https://github.com/allthingstalk/python-sdk',
8-
download_url='https://github.com/allthingstalk/python-sdk/archive/0.2.4.tar.gz',
8+
download_url='https://github.com/allthingstalk/python-sdk/archive/0.2.5.tar.gz',
99
author='Danilo Vidovic',
1010
author_email='dv@allthingstalk.com',
1111
license='Apache 2.0',

0 commit comments

Comments
 (0)