25
25
from .errors import RequestError , ThrottlingError
26
26
from .model import Data , Feed , Group
27
27
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
+ }
28
34
29
35
class Client (object ):
30
36
"""Client instance for interacting with the Adafruit IO service using its
@@ -56,34 +62,39 @@ def _handle_error(self, response):
56
62
raise RequestError (response )
57
63
# Else do nothing if there was no error.
58
64
65
+ def _headers (self , given ):
66
+ headers = default_headers .copy ()
67
+ headers .update (given )
68
+ return headers
69
+
59
70
def _get (self , path ):
60
71
response = requests .get (self ._compose_url (path ),
61
- headers = {'X-AIO-Key' : self .key },
72
+ headers = self . _headers ( {'X-AIO-Key' : self .key }) ,
62
73
proxies = self .proxies )
63
74
self ._handle_error (response )
64
75
return response .json ()
65
76
66
77
def _post (self , path , data ):
67
78
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' }) ,
70
81
proxies = self .proxies ,
71
82
data = json .dumps (data ))
72
83
self ._handle_error (response )
73
84
return response .json ()
74
85
75
86
def _delete (self , path ):
76
87
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' }) ,
79
90
proxies = self .proxies )
80
91
self ._handle_error (response )
81
92
82
93
# Data functionality.
83
94
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
87
98
with details about the newly appended row of data.
88
99
"""
89
100
path = "api/feeds/{0}/data/send" .format (feed_name )
@@ -106,7 +117,7 @@ def receive(self, feed):
106
117
return Data .from_dict (self ._get (path ))
107
118
108
119
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
110
121
a feed ID, feed key, or feed name. Returns a Data instance whose value
111
122
property holds the retrieved value.
112
123
"""
@@ -115,16 +126,16 @@ def receive_next(self, feed):
115
126
116
127
def receive_previous (self , feed ):
117
128
"""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
119
130
value property holds the retrieved value.
120
131
"""
121
132
path = "api/feeds/{0}/data/previous" .format (feed )
122
133
return Data .from_dict (self ._get (path ))
123
134
124
135
def data (self , feed , data_id = None ):
125
136
"""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
128
139
returned in an array.
129
140
"""
130
141
if data_id is None :
@@ -184,9 +195,9 @@ def send_group(self, group_name, data):
184
195
feed in the group, where the key is the feed name and value is the new
185
196
data row value. For example a group 'TestGroup' with feeds 'FeedOne'
186
197
and 'FeedTwo' could be updated by calling:
187
-
198
+
188
199
send_group('TestGroup', {'FeedOne': 'value1', 'FeedTwo': 10})
189
-
200
+
190
201
This would add the value 'value1' to the feed 'FeedOne' and add the
191
202
value 10 to the feed 'FeedTwo'.
192
203
@@ -207,7 +218,7 @@ def receive_group(self, group):
207
218
def receive_next_group (self , group ):
208
219
"""Retrieve the next unread value from the specified group. Group can
209
220
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
211
222
group.
212
223
"""
213
224
path = "api/groups/{0}/next" .format (group )
@@ -223,9 +234,9 @@ def receive_previous_group(self, group):
223
234
return Group .from_dict (self ._get (path ))
224
235
225
236
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
229
240
will be returned.
230
241
"""
231
242
if group is None :
0 commit comments