The xively-python library consists of two layers. The top layer uses the XivelyAPIClient and object wrappers. The lower layer via Client gives access directly to sending native python types and accessing the response.
An instance of an authenticated Xively API Client.
The root object from which the user can manage feeds, keys and triggers.
Parameters: |
|
---|
Usage:
>>> import xively
>>> xively.XivelyAPIClient("API_KEY")
<xively.XivelyAPIClient()>
>>> api = xively.XivelyAPIClient("API_KEY", use_ssl=True)
>>> api.feeds.base_url
'https://api.xively.com/v2/feeds'
>>> api.triggers.base_url
'https://api.xively.com/v2/triggers'
>>> api.keys.base_url
'https://api.xively.com/v2/keys'
Access Feed objects through a FeedsManager.
Access Key objects through a KeysManager.
Access Trigger objects through a TriggersManager.
Create, update and return Feed objects.
Note
This manager should live on a XivelyAPIClient instance and not instantiated directly.
Parameters: | client – Low level Client instance |
---|
Usage:
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> api.feeds.create(title="Xively Office environment")
<xively.Feed(7021)>
>>> api.feeds.get(7021)
<xively.Feed(7021)>
>>> api.feeds.update(7021, private=True)
>>> api.feeds.delete(7021)
Creates a new Feed.
Parameters: |
|
---|
Delete a feed by id or url.
Warning
This is final and cannot be undone.
Parameters: | id_or_url – The feed ID or its URL |
---|
Fetches and returns a feed by id or url.
By default the most recent datastreams are returned. It is also possible to filter the datastreams returned with the feed by using the “datastreams” parameter and a list of datastream IDs.
Parameters: |
|
---|
See history() for details.
Returns a paged list of feeds.
Only feeds that are viewable by the authenticated account will be returned. The following parameters can be applied to limit or refine the returned feeds:
Parameters: |
|
---|
The following additional parameters are available which allow location based searching of feeds:
Parameters: |
|
---|
Xively Feed, which can contain a number of Datastreams.
Parameters: |
|
---|
Usage:
>>> import xively
>>> xively.Feed(title="Xively Office environment")
<xively.Feed(None)>
Manager for datastreams of this feed.
When fetched from the API, datastreams behaves like a cache, populated with the most recently updated datastreams for this feed. The manager can also be used to create, update and delete datastreams for this feed.
Usage:
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> feed = api.feeds.get(7021)
>>> feed.datastreams[0]
<xively.Datastream('3')>
Delete this feed via the API.
Warning
This is final and cannot be undone.
Updates feed and datastreams via the API.
If successful, the current datastream values are stored and any changes in feed metadata overwrite previous values. Xively stores a server-side timestamp in the “updated” attribute and sets the feed to “live” if it wasn’t before.
Parameters: | fields (list of strings) – If given, only update these fields. |
---|
Create, update and return Datastream objects.
Instances of this class hang off of Feed objects to manage datastreams of that feed.
A list of datastreams can be retrieved along with the feed which can be accessed via this instance as a sequence.
Parameters: | feed – A Feed instance. |
---|
Usage:
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> feed = api.feeds.get(7021)
>>> list(feed.datastreams)
[<xively.Datastream('3')>, <xively.Datastream('4')>]
>>> feed.datastreams.create("1")
<xively.Datastream('1')>
Creates a new datastream on a feed.
Parameters: |
|
---|---|
Returns: | A Datastream object |
Delete a datastream by id or url.
Warning
This is final and cannot be undone.
Parameters: | id_or_url – The datastream ID or its URL |
---|
Fetches and returns a feed’s datastream by its id.
If start, end or duration are given, also returns Datapoints for that period.
Parameters: |
|
---|
See history() for details.
Returns a list of datastreams for the parent feed object.
Parameters: |
|
---|
Updates a feeds datastream by id.
Parameters: |
|
---|
Xively Datastream containing current and historical values.
Parameters: |
|
---|
Manager for datapoints of this datastream.
When a datastream is fetched with history from the API, datapoints is a sequence of timestamped values for the period requested. The manger can also be used tocreate, update and delte datapoints for this datastream.
Usage:
>>> import xively
>>> import datetime
>>> api = xively.XivelyAPIClient("API_KEY")
>>> feed = api.feeds.get(7021)
>>> datastream = feed.datastreams.get("random5",
... start=datetime.datetime(2013, 1, 1, 14, 0, 0),
... end=datetime.datetime(2013, 1, 1, 16, 0, 0))
>>> datastream.datapoints[:2]
...
[xively.Datapoint(datetime.datetime(...), '0.25741970'),
xively.Datapoint(datetime.datetime(...), '0.86826886')]
Delete this datastream from Xively.
Warning
This is final and cannot be undone.
Sends the current state of this datastream to Xively.
This method updates just the single datastream.
Parameters: | fields (list of strings) – If given, only update these fields. |
---|
Manage datapoints of a datastream.
A list of Datapoint objects can be retrieved along with the Datastream (or Feed) which can be accessed via this instance as a sequence.
Parameters: | datastream – A Datastream instance. |
---|
Create a single new datapoint for this datastream.
Parameters: |
|
---|
To create multiple datapoints at the same time do the following instead:
Note
You can use ISO8601 formatted strings instead of datetime objects when dealing with the API.
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> feed = api.feeds.get(7021)
>>> datastream = feed.datastreams[0]
>>> # First create the datapoints.
>>> datastream.datapoints = [
... xively.Datapoint(at="2010-05-20T11:01:43Z", value=294),
... xively.Datapoint(at="2010-05-20T11:01:44Z", value=295),
... xively.Datapoint(at="2010-05-20T11:01:45Z", value=296),
... xively.Datapoint(at="2010-05-20T11:01:46Z", value=297),
... ]
>>> # Then send them to the server.
>>> datastream.update(fields='datapoints')
Delete a datapoint or a range of datapoints.
Parameters: |
|
---|
By providing a start and end timestamp as query parameters, you may remove all datapoints that lie between those dates. If you send your request with only a start timestamp, all datapoints after the value will be removed. Providing an end timestamp will remove all datapoints prior to the supplied value.
Additionally, this method supports a duration parameter (e.g. duration="3hours"). Providing a start and a duration will delete all datapoints between the start and (start + duration). Providing end will delete all datapoints between (end - duration) and end. The formatting of the duration parameter is the same as is used in the history() method.
Fetch and return a Datapoint at the given timestamp.
Parameters: | at – The timestamp to return a datapoint for |
---|
Fetch and return a list of datapoints in a given timerange.
Parameters: |
|
---|
Note
find_previous is useful for any graphing because if you want to draw a graph of the date range you specified you would end up with a small gap until the first value.
Note
In order to paginate through the data use the last timestamp returned as the start of the next query.
Note
The maximum number of datapoints able to be returned from the API in one query is 1000. If you need more than 1000 datapoints for a specific period you should use the start and end times to split them up into smaller chunks.
The valid time units are:
* seconds
* minute(s)
* hour(s)
* day(s)
* week(s)
* month(s)
* year(s)
The acceptable intervals are currently:
Value | Description | Maximum range in one query |
---|---|---|
0 | Every datapoint stored | 6 hours |
30 | One datapoint every 30 seconds | 12 hours |
60 | One datapoint every minute | 24 hours |
300 | One datapoint every 5 minutes | 5 days |
900 | One datapoint every 15 minutes | 14 days |
1800 | One datapoint per 30 minutes | 31 days |
3600 | One datapoint per hour | 31 days |
10800 | One datapoint per three hours | 90 days |
21600 | One datapoint per six hours | 180 days |
43200 | One datapoint per twelve hours | 1 year |
86400 | One datapoint per day | 1 year |
Update the value of a datapiont at a given timestamp.
Parameters: |
|
---|
Note
A datapoint at the given time must already exist.
The location and location type of a feed.
Parameters: |
|
---|
A waypoint represents where a mobile feed was at a particular time.
Parameters: |
|
---|
Manage keys their permissions and restrict by resource.
This manager should live on a XivelyAPIClient instance and not instantiated directly.
Parameters: | client – Low level Client instance |
---|
Usage:
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> api.keys.create(
... label="sharing key",
... private_access=True,
... permissions=[
... xively.Permission(
... access_methods=["put"],
... source_ip="128.44.98.129",
... resources=[
... xively.Resource(feed_id=504),
... ]),
... xively.Permission(access_methods=["get"])
... ])
<xively.Key('sharing key')>
Create a new API key.
Parameters: |
|
---|
Delete the specified key.
Parameters: | key_id – The key ID |
---|
Fetch and return an API key by its id.
Parameters: | key_id – The ID of the key to get. |
---|
List all API keys for this account or for the given feed.
Parameters: | feed_id – Returns api keys limited to that feed and its datastreams. |
---|
Keys set which permissions are granted for certain resources.
Parameters: |
|
---|
Delete this key.
Permissions restrict what can be done by a key.
Parameters: |
|
---|
A Resource defines what an API key has access to.
Parameters: |
|
---|
Manage Trigger.
This manager should live on a XivelyAPIClient instance and not instantiated directly.
Parameters: | client – Low level Client instance |
---|
Usage:
>>> import xively
>>> api = xively.XivelyAPIClient("API_KEY")
>>> api.triggers.create(
... environment_id=8470, stream_id="0",
... url="http://www.postbin.org/1ijyltn",
... trigger_type='lt', threshold_value="15.0")
<xively.Trigger(3)>
Create a new Trigger.
Parameters: |
|
---|---|
Returns: | A new Trigger object. |
Possible values for trigger_type are:
gt | greater than |
gte | greater than or equal to |
lt | less than |
lte | less than or equal to |
eq | equal to |
change | any change |
frozen | no updates for 15 minutes |
live | updated again after being frozen |
Delete a trigger by id or url.
Warning
This is final and cannot be undone.
Parameters: | id_or_url – The datastream ID or its URL |
---|
Fetch and return an existing trigger.
Parameters: | id_or_url – The ID of the trigger or its URL |
---|
Return a list of triggers.
Parameters: | feed_id – Filter the returned triggers to only include those on datastreams of the specified feed. |
---|
Triggers provide ‘push’ capabilities (aka notifications).
To create a new trigger, use the TriggersManager on a XivelyAPIClient instance.
>>> from xively import XivelyAPIClient
>>> api = XivelyAPIClient("API_KEY")
>>> api.triggers.create(123, "temperature", "http://example.com", "frozen")
<xively.Trigger(3)>
Delete a trigger.
Warning
This is final and cannot be undone.
Update an existing trigger.
Parameters: | fields (list of strings) – If given, only update these fields |
---|
Bases: requests.sessions.Session
A Xively API Client object.
This is instantiated with an API key which is used for all requests to the Xively API. It also defines a BASE_URL so that we can specify relative urls when using the client (all requests via this client are going to Xively).
Parameters: |
|
---|
A Client instance can also be used when you want low level access to the API and can be used with CSV or XML instead of the default JSON.
Usage:
>>> client = xively.Client("YOUR_API_KEY")
>>> body = "1,123\r\n2,456\r\n"
>>> client.post('/v2/feeds/1977.csv', data=body)
<Response [200]>
Constructs and sends a Request to the Xively API.
Objects that implement __getstate__ will be serialised.