@@ -41,21 +41,23 @@ class Client(object):
41
41
REST API. Use this client class to send, receive, and enumerate feed data.
42
42
"""
43
43
44
- def __init__ (self , key , proxies = None , base_url = 'https://io.adafruit.com' ):
44
+ def __init__ (self , key , proxies = None , base_url = 'https://io.adafruit.com' , api_version = 'v1' ):
45
45
"""Create an instance of the Adafruit IO REST API client. Key must be
46
46
provided and set to your Adafruit IO access key value. Optionaly
47
- provide a proxies dict in the format used by the requests library, and
47
+ provide a proxies dict in the format used by the requests library, a
48
48
base_url to point at a different Adafruit IO service (the default is
49
- the production Adafruit IO service over SSL).
49
+ the production Adafruit IO service over SSL), and a api_version to
50
+ add support for future API versions.
50
51
"""
51
52
self .key = key
52
53
self .proxies = proxies
54
+ self .api_version = api_version
53
55
# Save URL without trailing slash as it will be added later when
54
56
# constructing the path.
55
57
self .base_url = base_url .rstrip ('/' )
56
58
57
59
def _compose_url (self , path ):
58
- return '{0}/{1}' .format (self .base_url , path )
60
+ return '{0}/api/ {1}/{2} ' .format (self .base_url , self . api_version , path )
59
61
60
62
def _handle_error (self , response ):
61
63
# Handle explicit errors.
@@ -101,7 +103,7 @@ def send(self, feed_name, value):
101
103
will append the provided value to the feed. Returns a Data instance
102
104
with details about the newly appended row of data.
103
105
"""
104
- path = "api/ feeds/{0}/data/send" .format (feed_name )
106
+ path = "feeds/{0}/data/send" .format (feed_name )
105
107
return Data .from_dict (self ._post (path , {'value' : value }))
106
108
107
109
def append (self , feed , value ):
@@ -117,23 +119,23 @@ def receive(self, feed):
117
119
feed ID, feed key, or feed name. Returns a Data instance whose value
118
120
property holds the retrieved value.
119
121
"""
120
- path = "api/ feeds/{0}/data/last" .format (feed )
122
+ path = "feeds/{0}/data/last" .format (feed )
121
123
return Data .from_dict (self ._get (path ))
122
124
123
125
def receive_next (self , feed ):
124
126
"""Retrieve the next unread value from the specified feed. Feed can be
125
127
a feed ID, feed key, or feed name. Returns a Data instance whose value
126
128
property holds the retrieved value.
127
129
"""
128
- path = "api/ feeds/{0}/data/next" .format (feed )
130
+ path = "feeds/{0}/data/next" .format (feed )
129
131
return Data .from_dict (self ._get (path ))
130
132
131
133
def receive_previous (self , feed ):
132
134
"""Retrieve the previous unread value from the specified feed. Feed can
133
135
be a feed ID, feed key, or feed name. Returns a Data instance whose
134
136
value property holds the retrieved value.
135
137
"""
136
- path = "api/ feeds/{0}/data/previous" .format (feed )
138
+ path = "feeds/{0}/data/previous" .format (feed )
137
139
return Data .from_dict (self ._get (path ))
138
140
139
141
def data (self , feed , data_id = None ):
@@ -143,10 +145,10 @@ def data(self, feed, data_id=None):
143
145
returned in an array.
144
146
"""
145
147
if data_id is None :
146
- path = "api/ feeds/{0}/data" .format (feed )
148
+ path = "feeds/{0}/data" .format (feed )
147
149
return list (map (Data .from_dict , self ._get (path )))
148
150
else :
149
- path = "api/ feeds/{0}/data/{1}" .format (feed , data_id )
151
+ path = "feeds/{0}/data/{1}" .format (feed , data_id )
150
152
return Data .from_dict (self ._get (path ))
151
153
152
154
def create_data (self , feed , data ):
@@ -155,14 +157,14 @@ def create_data(self, feed, data):
155
157
with at least a value property set on it. Returns a Data instance with
156
158
details about the newly appended row of data.
157
159
"""
158
- path = "api/ feeds/{0}/data" .format (feed )
160
+ path = "feeds/{0}/data" .format (feed )
159
161
return Data .from_dict (self ._post (path , data ._asdict ()))
160
162
161
163
def delete (self , feed , data_id ):
162
164
"""Delete data from a feed. Feed can be a feed ID, feed key, or feed
163
165
name. Data_id must be the ID of the piece of data to delete.
164
166
"""
165
- path = "api/ feeds/{0}/data/{1}" .format (feed , data_id )
167
+ path = "feeds/{0}/data/{1}" .format (feed , data_id )
166
168
self ._delete (path )
167
169
168
170
# Feed functionality.
@@ -172,24 +174,24 @@ def feeds(self, feed=None):
172
174
can be a feed name, key, or ID and the requested feed will be returned.
173
175
"""
174
176
if feed is None :
175
- path = "api/ feeds"
177
+ path = "feeds"
176
178
return list (map (Feed .from_dict , self ._get (path )))
177
179
else :
178
- path = "api/ feeds/{0}" .format (feed )
180
+ path = "feeds/{0}" .format (feed )
179
181
return Feed .from_dict (self ._get (path ))
180
182
181
183
def create_feed (self , feed ):
182
184
"""Create the specified feed. Feed should be an instance of the Feed
183
185
type with at least the name property set.
184
186
"""
185
- path = "api/ feeds/"
187
+ path = "feeds/"
186
188
return Feed .from_dict (self ._post (path , feed ._asdict ()))
187
189
188
190
def delete_feed (self , feed ):
189
191
"""Delete the specified feed. Feed can be a feed ID, feed key, or feed
190
192
name.
191
193
"""
192
- path = "api/ feeds/{0}" .format (feed )
194
+ path = "feeds/{0}" .format (feed )
193
195
self ._delete (path )
194
196
195
197
# Group functionality.
@@ -208,15 +210,15 @@ def send_group(self, group_name, data):
208
210
After a successful update an instance of Group will be returned with
209
211
metadata about the updated group.
210
212
"""
211
- path = "api/ groups/{0}/send" .format (group_name )
213
+ path = "groups/{0}/send" .format (group_name )
212
214
return Group .from_dict (self ._post (path , {'value' : data }))
213
215
214
216
def receive_group (self , group ):
215
217
"""Retrieve the most recent value for the specified group. Group can be
216
218
a group ID, group key, or group name. Returns a Group instance whose
217
219
feeds property holds an array of Feed instances associated with the group.
218
220
"""
219
- path = "api/ groups/{0}/last" .format (group )
221
+ path = "groups/{0}/last" .format (group )
220
222
return Group .from_dict (self ._get (path ))
221
223
222
224
def receive_next_group (self , group ):
@@ -225,7 +227,7 @@ def receive_next_group(self, group):
225
227
feeds property holds an array of Feed instances associated with the
226
228
group.
227
229
"""
228
- path = "api/ groups/{0}/next" .format (group )
230
+ path = "groups/{0}/next" .format (group )
229
231
return Group .from_dict (self ._get (path ))
230
232
231
233
def receive_previous_group (self , group ):
@@ -234,7 +236,7 @@ def receive_previous_group(self, group):
234
236
whose feeds property holds an array of Feed instances associated with
235
237
the group.
236
238
"""
237
- path = "api/ groups/{0}/previous" .format (group )
239
+ path = "groups/{0}/previous" .format (group )
238
240
return Group .from_dict (self ._get (path ))
239
241
240
242
def groups (self , group = None ):
@@ -244,22 +246,22 @@ def groups(self, group=None):
244
246
will be returned.
245
247
"""
246
248
if group is None :
247
- path = "api/ groups/"
249
+ path = "groups/"
248
250
return list (map (Group .from_dict , self ._get (path )))
249
251
else :
250
- path = "api/ groups/{0}" .format (group )
252
+ path = "groups/{0}" .format (group )
251
253
return Group .from_dict (self ._get (path ))
252
254
253
255
def create_group (self , group ):
254
256
"""Create the specified group. Group should be an instance of the Group
255
257
type with at least the name and feeds property set.
256
258
"""
257
- path = "api/ groups/"
259
+ path = "groups/"
258
260
return Group .from_dict (self ._post (path , group ._asdict ()))
259
261
260
262
def delete_group (self , group ):
261
263
"""Delete the specified group. Group can be a group ID, group key, or
262
264
group name.
263
265
"""
264
- path = "api/ groups/{0}" .format (group )
266
+ path = "groups/{0}" .format (group )
265
267
self ._delete (path )
0 commit comments