Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

feat(client): add support for context managers #816

Merged
merged 2 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add gzip compression for post and response data (#732 thx @KEClaytor)
- Add support for chunked responses in ResultSet (#753 and #538 thx @hrbonz && @psy0rz)
- Add support for empty string fields (#766 thx @gregschrock)
- Add support for context managers to InfluxDBClient (#721 thx @JustusAdam)

### Changed
- Clean up stale CI config (#755)
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update client to type-set UDP port to int (#651 thx @yifeikong)
- Update batched writing support for all iterables (#746 thx @JayH5)
- Update SeriesHelper to enable class instantiation when not initialized (#772 thx @ocworld)
- Update UDP test case to add proper timestamp to datapoints (#808 thx @shantanoo-desai)

### Removed

Expand Down
12 changes: 12 additions & 0 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class InfluxDBClient(object):
connect to InfluxDB. Requests can be made to InfluxDB directly through
the client.

The client supports the use as a `context manager
<https://docs.python.org/3/reference/datamodel.html#context-managers>`_.

:param host: hostname to connect to InfluxDB, defaults to 'localhost'
:type host: str
:param port: port to connect to InfluxDB, defaults to 8086
Expand Down Expand Up @@ -78,6 +81,7 @@ class InfluxDBClient(object):
requests Session, defaults to None
:type session: requests.Session
:raises ValueError: if cert is provided but ssl is disabled (set to False)

"""

def __init__(self,
Expand Down Expand Up @@ -165,6 +169,14 @@ def __init__(self,

self._gzip = gzip

def __enter__(self):
"""Enter function as used by context manager."""
pass

def __exit__(self, _exc_type, _exc_value, _traceback):
"""Exit function as used by context manager."""
self.close()

@property
def _baseurl(self):
return self.__baseurl
Expand Down