2525from .errors import RequestError , ThrottlingError
2626from .model import Data , Feed , Group
2727
28+ # set outgoing version, pulled from setup.py
29+ import pkg_resources
30+ version = pkg_resources .require ("Adafruit_IO" )[0 ].version
31+ default_headers = {
32+ 'User-Agent' : 'AdafruitIO-Python/{0}' .format (version )
33+ }
2834
2935class Client (object ):
3036 """Client instance for interacting with the Adafruit IO service using its
@@ -56,34 +62,39 @@ def _handle_error(self, response):
5662 raise RequestError (response )
5763 # Else do nothing if there was no error.
5864
65+ def _headers (self , given ):
66+ headers = default_headers .copy ()
67+ headers .update (given )
68+ return headers
69+
5970 def _get (self , path ):
6071 response = requests .get (self ._compose_url (path ),
61- headers = {'X-AIO-Key' : self .key },
72+ headers = self . _headers ( {'X-AIO-Key' : self .key }) ,
6273 proxies = self .proxies )
6374 self ._handle_error (response )
6475 return response .json ()
6576
6677 def _post (self , path , data ):
6778 response = requests .post (self ._compose_url (path ),
68- headers = {'X-AIO-Key' : self .key ,
69- 'Content-Type' : 'application/json' },
79+ headers = self . _headers ( {'X-AIO-Key' : self .key ,
80+ 'Content-Type' : 'application/json' }) ,
7081 proxies = self .proxies ,
7182 data = json .dumps (data ))
7283 self ._handle_error (response )
7384 return response .json ()
7485
7586 def _delete (self , path ):
7687 response = requests .delete (self ._compose_url (path ),
77- headers = {'X-AIO-Key' : self .key ,
78- 'Content-Type' : 'application/json' },
88+ headers = self . _headers ( {'X-AIO-Key' : self .key ,
89+ 'Content-Type' : 'application/json' }) ,
7990 proxies = self .proxies )
8091 self ._handle_error (response )
8192
8293 # Data functionality.
8394 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
95+ """Helper function to simplify adding a value to a feed. Will find the
96+ specified feed by name or create a new feed if it doesn't exist, then
97+ will append the provided value to the feed. Returns a Data instance
8798 with details about the newly appended row of data.
8899 """
89100 path = "api/feeds/{0}/data/send" .format (feed_name )
@@ -106,7 +117,7 @@ def receive(self, feed):
106117 return Data .from_dict (self ._get (path ))
107118
108119 def receive_next (self , feed ):
109- """Retrieve the next unread value from the specified feed. Feed can be
120+ """Retrieve the next unread value from the specified feed. Feed can be
110121 a feed ID, feed key, or feed name. Returns a Data instance whose value
111122 property holds the retrieved value.
112123 """
@@ -115,16 +126,16 @@ def receive_next(self, feed):
115126
116127 def receive_previous (self , feed ):
117128 """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
129+ be a feed ID, feed key, or feed name. Returns a Data instance whose
119130 value property holds the retrieved value.
120131 """
121132 path = "api/feeds/{0}/data/previous" .format (feed )
122133 return Data .from_dict (self ._get (path ))
123134
124135 def data (self , feed , data_id = None ):
125136 """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
137+ name. Data_id is an optional id for a single data value to retrieve.
138+ If data_id is not specified then all the data for the feed will be
128139 returned in an array.
129140 """
130141 if data_id is None :
@@ -184,9 +195,9 @@ def send_group(self, group_name, data):
184195 feed in the group, where the key is the feed name and value is the new
185196 data row value. For example a group 'TestGroup' with feeds 'FeedOne'
186197 and 'FeedTwo' could be updated by calling:
187-
198+
188199 send_group('TestGroup', {'FeedOne': 'value1', 'FeedTwo': 10})
189-
200+
190201 This would add the value 'value1' to the feed 'FeedOne' and add the
191202 value 10 to the feed 'FeedTwo'.
192203
@@ -207,7 +218,7 @@ def receive_group(self, group):
207218 def receive_next_group (self , group ):
208219 """Retrieve the next unread value from the specified group. Group can
209220 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
221+ feeds property holds an array of Feed instances associated with the
211222 group.
212223 """
213224 path = "api/groups/{0}/next" .format (group )
@@ -223,9 +234,9 @@ def receive_previous_group(self, group):
223234 return Group .from_dict (self ._get (path ))
224235
225236 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
237+ """Retrieve a list of all groups, or the specified group. If group is
238+ not specified a list of all groups will be returned. If group is
239+ specified it can be a group name, key, or ID and the requested group
229240 will be returned.
230241 """
231242 if group is None :
0 commit comments