1919# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020# SOFTWARE.
2121import json
22+ import pkg_resources
23+ import platform
2224
2325import requests
2426
2527from .errors import RequestError , ThrottlingError
2628from .model import Data , Feed , Group
2729
30+ # set outgoing version, pulled from setup.py
31+ version = pkg_resources .require ("Adafruit_IO" )[0 ].version
32+ default_headers = {
33+ 'User-Agent' : 'AdafruitIO-Python/{0} ({1}, {2} {3})' .format (version ,
34+ platform .platform (),
35+ platform .python_implementation (),
36+ platform .python_version ())
37+ }
2838
2939class Client (object ):
3040 """Client instance for interacting with the Adafruit IO service using its
@@ -56,34 +66,39 @@ def _handle_error(self, response):
5666 raise RequestError (response )
5767 # Else do nothing if there was no error.
5868
69+ def _headers (self , given ):
70+ headers = default_headers .copy ()
71+ headers .update (given )
72+ return headers
73+
5974 def _get (self , path ):
6075 response = requests .get (self ._compose_url (path ),
61- headers = {'X-AIO-Key' : self .key },
76+ headers = self . _headers ( {'X-AIO-Key' : self .key }) ,
6277 proxies = self .proxies )
6378 self ._handle_error (response )
6479 return response .json ()
6580
6681 def _post (self , path , data ):
6782 response = requests .post (self ._compose_url (path ),
68- headers = {'X-AIO-Key' : self .key ,
69- 'Content-Type' : 'application/json' },
83+ headers = self . _headers ( {'X-AIO-Key' : self .key ,
84+ 'Content-Type' : 'application/json' }) ,
7085 proxies = self .proxies ,
7186 data = json .dumps (data ))
7287 self ._handle_error (response )
7388 return response .json ()
7489
7590 def _delete (self , path ):
7691 response = requests .delete (self ._compose_url (path ),
77- headers = {'X-AIO-Key' : self .key ,
78- 'Content-Type' : 'application/json' },
92+ headers = self . _headers ( {'X-AIO-Key' : self .key ,
93+ 'Content-Type' : 'application/json' }) ,
7994 proxies = self .proxies )
8095 self ._handle_error (response )
8196
8297 # Data functionality.
8398 def send (self , feed_name , value ):
84- """Helper function to simplify adding a value to a feed. Will find the
85- specified feed by name or create a new feed if it doesn't exist, then
86- will append the provided value to the feed. Returns a Data instance
99+ """Helper function to simplify adding a value to a feed. Will find the
100+ specified feed by name or create a new feed if it doesn't exist, then
101+ will append the provided value to the feed. Returns a Data instance
87102 with details about the newly appended row of data.
88103 """
89104 path = "api/feeds/{0}/data/send" .format (feed_name )
@@ -106,7 +121,7 @@ def receive(self, feed):
106121 return Data .from_dict (self ._get (path ))
107122
108123 def receive_next (self , feed ):
109- """Retrieve the next unread value from the specified feed. Feed can be
124+ """Retrieve the next unread value from the specified feed. Feed can be
110125 a feed ID, feed key, or feed name. Returns a Data instance whose value
111126 property holds the retrieved value.
112127 """
@@ -115,16 +130,16 @@ def receive_next(self, feed):
115130
116131 def receive_previous (self , feed ):
117132 """Retrieve the previous unread value from the specified feed. Feed can
118- be a feed ID, feed key, or feed name. Returns a Data instance whose
133+ be a feed ID, feed key, or feed name. Returns a Data instance whose
119134 value property holds the retrieved value.
120135 """
121136 path = "api/feeds/{0}/data/previous" .format (feed )
122137 return Data .from_dict (self ._get (path ))
123138
124139 def data (self , feed , data_id = None ):
125140 """Retrieve data from a feed. Feed can be a feed ID, feed key, or feed
126- name. Data_id is an optional id for a single data value to retrieve.
127- If data_id is not specified then all the data for the feed will be
141+ name. Data_id is an optional id for a single data value to retrieve.
142+ If data_id is not specified then all the data for the feed will be
128143 returned in an array.
129144 """
130145 if data_id is None :
@@ -184,9 +199,9 @@ def send_group(self, group_name, data):
184199 feed in the group, where the key is the feed name and value is the new
185200 data row value. For example a group 'TestGroup' with feeds 'FeedOne'
186201 and 'FeedTwo' could be updated by calling:
187-
202+
188203 send_group('TestGroup', {'FeedOne': 'value1', 'FeedTwo': 10})
189-
204+
190205 This would add the value 'value1' to the feed 'FeedOne' and add the
191206 value 10 to the feed 'FeedTwo'.
192207
@@ -207,7 +222,7 @@ def receive_group(self, group):
207222 def receive_next_group (self , group ):
208223 """Retrieve the next unread value from the specified group. Group can
209224 be a group ID, group key, or group name. Returns a Group instance whose
210- feeds property holds an array of Feed instances associated with the
225+ feeds property holds an array of Feed instances associated with the
211226 group.
212227 """
213228 path = "api/groups/{0}/next" .format (group )
@@ -223,9 +238,9 @@ def receive_previous_group(self, group):
223238 return Group .from_dict (self ._get (path ))
224239
225240 def groups (self , group = None ):
226- """Retrieve a list of all groups, or the specified group. If group is
227- not specified a list of all groups will be returned. If group is
228- specified it can be a group name, key, or ID and the requested group
241+ """Retrieve a list of all groups, or the specified group. If group is
242+ not specified a list of all groups will be returned. If group is
243+ specified it can be a group name, key, or ID and the requested group
229244 will be returned.
230245 """
231246 if group is None :
0 commit comments