Skip to content

Commit

Permalink
Add upload_file() function
Browse files Browse the repository at this point in the history
with example for how to use to upload streamer reports
  • Loading branch information
dkarchmer committed Feb 4, 2017
1 parent 1aa6167 commit 4861a29
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v0.3.2 (2017-02-04)

* Add upload_file() functionality

### v0.3.0 (2016-11-17)

* Refactor connection into an api sub-package
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A python library for interacting with [IOTile Cloud](https://iotile.cloud) Rest

```
# pip install pystrato
pip install git+https://github.com/iotile/[email protected].1-alpha
pip install git+https://github.com/iotile/[email protected].2-alpha
```

Package is based on https://github.com/samgiles/slumber
Expand Down Expand Up @@ -135,6 +135,23 @@ if ok:
```

### Uploading a Streamer Report

Example:

```
from pystrato.api.connection import Api
c = Api()
ok = c.login(email=args.email, password=password)
if ok:
url_args = 'timestamp={}'.format(datetime.datetime.utcnow().isoformat())
resp = c.streamer(action='report').upload_file(filename='path/to/my/file', extra=url_args)
```


## Requirements

pystrato requires the following modules.
Expand Down
15 changes: 15 additions & 0 deletions pystrato/api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ def delete(self, **kwargs):
else:
return False

def upload_file(self, filename, mode='rb', extra=None, **kwargs):
try:
payload = {
'file': open(filename, mode)
}
except Exception as e:
raise RestBaseException(str(e))

headers = {}
authorization_str = '{0} {1}'.format(self._store['token_type'], self._store["token"])
headers['Authorization'] = authorization_str
logger.debug('Uploading file to {}'.format(self.url(extra)))
resp = requests.post(self.url(extra), files=payload, headers=headers)
return self._process_response(resp)


class Api(object):
token = None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='pystrato',
version='0.3.1',
version='0.3.2',
description='Python client for https://iotile.cloud',
url='https://github.com/iotile/strato_python_api',
author='David Karchmer',
Expand Down

0 comments on commit 4861a29

Please sign in to comment.