diff --git a/CHANGELOG.md b/CHANGELOG.md index 38f85d4..f681527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 0d741dd..ac896af 100644 --- a/README.md +++ b/README.md @@ -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/strato_python_api.git@v0.3.1-alpha +pip install git+https://github.com/iotile/strato_python_api.git@v0.3.2-alpha ``` Package is based on https://github.com/samgiles/slumber @@ -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. diff --git a/pystrato/api/connection.py b/pystrato/api/connection.py index 3799d5f..9acc451 100644 --- a/pystrato/api/connection.py +++ b/pystrato/api/connection.py @@ -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 diff --git a/setup.py b/setup.py index dbc95d9..08b778f 100644 --- a/setup.py +++ b/setup.py @@ -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',