Skip to content

Commit 9deb903

Browse files
author
Torben
committed
Updated client to use a version property (defaults to API v1) so it works with legacy API
1 parent 4d092e7 commit 9deb903

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

Adafruit_IO/client.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,23 @@ class Client(object):
4141
REST API. Use this client class to send, receive, and enumerate feed data.
4242
"""
4343

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'):
4545
"""Create an instance of the Adafruit IO REST API client. Key must be
4646
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
4848
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.
5051
"""
5152
self.key = key
5253
self.proxies = proxies
54+
self.api_version = api_version
5355
# Save URL without trailing slash as it will be added later when
5456
# constructing the path.
5557
self.base_url = base_url.rstrip('/')
5658

5759
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)
5961

6062
def _handle_error(self, response):
6163
# Handle explicit errors.
@@ -101,7 +103,7 @@ def send(self, feed_name, value):
101103
will append the provided value to the feed. Returns a Data instance
102104
with details about the newly appended row of data.
103105
"""
104-
path = "api/feeds/{0}/data/send".format(feed_name)
106+
path = "feeds/{0}/data/send".format(feed_name)
105107
return Data.from_dict(self._post(path, {'value': value}))
106108

107109
def append(self, feed, value):
@@ -117,23 +119,23 @@ def receive(self, feed):
117119
feed ID, feed key, or feed name. Returns a Data instance whose value
118120
property holds the retrieved value.
119121
"""
120-
path = "api/feeds/{0}/data/last".format(feed)
122+
path = "feeds/{0}/data/last".format(feed)
121123
return Data.from_dict(self._get(path))
122124

123125
def receive_next(self, feed):
124126
"""Retrieve the next unread value from the specified feed. Feed can be
125127
a feed ID, feed key, or feed name. Returns a Data instance whose value
126128
property holds the retrieved value.
127129
"""
128-
path = "api/feeds/{0}/data/next".format(feed)
130+
path = "feeds/{0}/data/next".format(feed)
129131
return Data.from_dict(self._get(path))
130132

131133
def receive_previous(self, feed):
132134
"""Retrieve the previous unread value from the specified feed. Feed can
133135
be a feed ID, feed key, or feed name. Returns a Data instance whose
134136
value property holds the retrieved value.
135137
"""
136-
path = "api/feeds/{0}/data/previous".format(feed)
138+
path = "feeds/{0}/data/previous".format(feed)
137139
return Data.from_dict(self._get(path))
138140

139141
def data(self, feed, data_id=None):
@@ -143,10 +145,10 @@ def data(self, feed, data_id=None):
143145
returned in an array.
144146
"""
145147
if data_id is None:
146-
path = "api/feeds/{0}/data".format(feed)
148+
path = "feeds/{0}/data".format(feed)
147149
return list(map(Data.from_dict, self._get(path)))
148150
else:
149-
path = "api/feeds/{0}/data/{1}".format(feed, data_id)
151+
path = "feeds/{0}/data/{1}".format(feed, data_id)
150152
return Data.from_dict(self._get(path))
151153

152154
def create_data(self, feed, data):
@@ -155,14 +157,14 @@ def create_data(self, feed, data):
155157
with at least a value property set on it. Returns a Data instance with
156158
details about the newly appended row of data.
157159
"""
158-
path = "api/feeds/{0}/data".format(feed)
160+
path = "feeds/{0}/data".format(feed)
159161
return Data.from_dict(self._post(path, data._asdict()))
160162

161163
def delete(self, feed, data_id):
162164
"""Delete data from a feed. Feed can be a feed ID, feed key, or feed
163165
name. Data_id must be the ID of the piece of data to delete.
164166
"""
165-
path = "api/feeds/{0}/data/{1}".format(feed, data_id)
167+
path = "feeds/{0}/data/{1}".format(feed, data_id)
166168
self._delete(path)
167169

168170
# Feed functionality.
@@ -172,24 +174,24 @@ def feeds(self, feed=None):
172174
can be a feed name, key, or ID and the requested feed will be returned.
173175
"""
174176
if feed is None:
175-
path = "api/feeds"
177+
path = "feeds"
176178
return list(map(Feed.from_dict, self._get(path)))
177179
else:
178-
path = "api/feeds/{0}".format(feed)
180+
path = "feeds/{0}".format(feed)
179181
return Feed.from_dict(self._get(path))
180182

181183
def create_feed(self, feed):
182184
"""Create the specified feed. Feed should be an instance of the Feed
183185
type with at least the name property set.
184186
"""
185-
path = "api/feeds/"
187+
path = "feeds/"
186188
return Feed.from_dict(self._post(path, feed._asdict()))
187189

188190
def delete_feed(self, feed):
189191
"""Delete the specified feed. Feed can be a feed ID, feed key, or feed
190192
name.
191193
"""
192-
path = "api/feeds/{0}".format(feed)
194+
path = "feeds/{0}".format(feed)
193195
self._delete(path)
194196

195197
# Group functionality.
@@ -208,15 +210,15 @@ def send_group(self, group_name, data):
208210
After a successful update an instance of Group will be returned with
209211
metadata about the updated group.
210212
"""
211-
path = "api/groups/{0}/send".format(group_name)
213+
path = "groups/{0}/send".format(group_name)
212214
return Group.from_dict(self._post(path, {'value': data}))
213215

214216
def receive_group(self, group):
215217
"""Retrieve the most recent value for the specified group. Group can be
216218
a group ID, group key, or group name. Returns a Group instance whose
217219
feeds property holds an array of Feed instances associated with the group.
218220
"""
219-
path = "api/groups/{0}/last".format(group)
221+
path = "groups/{0}/last".format(group)
220222
return Group.from_dict(self._get(path))
221223

222224
def receive_next_group(self, group):
@@ -225,7 +227,7 @@ def receive_next_group(self, group):
225227
feeds property holds an array of Feed instances associated with the
226228
group.
227229
"""
228-
path = "api/groups/{0}/next".format(group)
230+
path = "groups/{0}/next".format(group)
229231
return Group.from_dict(self._get(path))
230232

231233
def receive_previous_group(self, group):
@@ -234,7 +236,7 @@ def receive_previous_group(self, group):
234236
whose feeds property holds an array of Feed instances associated with
235237
the group.
236238
"""
237-
path = "api/groups/{0}/previous".format(group)
239+
path = "groups/{0}/previous".format(group)
238240
return Group.from_dict(self._get(path))
239241

240242
def groups(self, group=None):
@@ -244,22 +246,22 @@ def groups(self, group=None):
244246
will be returned.
245247
"""
246248
if group is None:
247-
path = "api/groups/"
249+
path = "groups/"
248250
return list(map(Group.from_dict, self._get(path)))
249251
else:
250-
path = "api/groups/{0}".format(group)
252+
path = "groups/{0}".format(group)
251253
return Group.from_dict(self._get(path))
252254

253255
def create_group(self, group):
254256
"""Create the specified group. Group should be an instance of the Group
255257
type with at least the name and feeds property set.
256258
"""
257-
path = "api/groups/"
259+
path = "groups/"
258260
return Group.from_dict(self._post(path, group._asdict()))
259261

260262
def delete_group(self, group):
261263
"""Delete the specified group. Group can be a group ID, group key, or
262264
group name.
263265
"""
264-
path = "api/groups/{0}".format(group)
266+
path = "groups/{0}".format(group)
265267
self._delete(path)

0 commit comments

Comments
 (0)