1919# limitations under the License.
2020
2121import logging
22+ import pkg_resources
2223
2324import paho .mqtt .client as paho_mqtt
2425import 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 )
0 commit comments