Skip to content

Implement version check #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2022
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
41 changes: 22 additions & 19 deletions influxdb_client/client/bucket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ def create_bucket(self, bucket=None, bucket_name=None, org_id=None, retention_ru
:param str, Organization org: specifies the organization for create the bucket;
Take the ``ID``, ``Name`` or ``Organization``.
If not specified the default value from ``InfluxDBClient.org`` is used.
:return: Bucket or the request thread when falling back.
:return: Bucket
If the method is called asynchronously,
returns also the request thread.
returns the request thread.
"""
if self._buckets_service._is_below_v2():
# Fall back to v1 API if buckets are not supported
warnings.warn("InfluxDB versions below v2.0 are deprecated. " + \
"Falling back to CREATE DATABASE statement", DeprecationWarning)
database_name = bucket_name if bucket_name is not None else bucket
return self._create_database(database=database_name)

if retention_rules is None:
retention_rules = []

Expand All @@ -55,19 +62,13 @@ def create_bucket(self, bucket=None, bucket_name=None, org_id=None, retention_ru
client=self._influxdb_client,
required_id=True))

try:
return self._buckets_service.post_buckets(post_bucket_request=bucket)
except ApiException:
# Fall back to v1 API if buckets are not supported
database_name = bucket_name if bucket_name is not None else bucket
return self.create_database(database=database_name, retention_rules=retention_rules)
return self._buckets_service.post_buckets(post_bucket_request=bucket)

def create_database(self, database=None, retention_rules=None):
def _create_database(self, database=None):
"""Create a database at the v1 api (legacy).

:param database_name: name of the new database
:param retention_rules: retention rules array or single BucketRetentionRules
:return: Tuple (response body, status code, header dict)
:return: tuple(response body, status code, header dict)
"""
if database is None:
raise ValueError("Invalid value for `database`, must be defined.")
Expand Down Expand Up @@ -114,24 +115,26 @@ def delete_bucket(self, bucket):
"""Delete a bucket. Delete a database via v1 API as fallback.

:param bucket: bucket id or Bucket
:return: Bucket or the request thread when falling back
:return: Bucket
"""
if isinstance(bucket, Bucket):
bucket_id = bucket.id
else:
bucket_id = bucket

try:
return self._buckets_service.delete_buckets_id(bucket_id=bucket_id)
except ApiException:
return self.delete_database(database=bucket_id)
if self._buckets_service._is_below_v2():
# Fall back to v1 API if buckets are not supported
warnings.warn("InfluxDB versions below v2.0 are deprecated. " + \
"Falling back to DROP DATABASE statement", DeprecationWarning)
return self._delete_database(database=bucket_id)

def delete_database(self, database=None):
return self._buckets_service.delete_buckets_id(bucket_id=bucket_id)

def _delete_database(self, database=None):
"""Delete a database at the v1 api (legacy).

:param database_name: name of the database to delete
:param retention_rules: retention rules array or single BucketRetentionRules
:return: Tuple (response body, status code, header dict)
:return: tuple(response body, status code, header dict)
"""
if database is None:
raise ValueError("Invalid value for `database`, must be defined.")
Expand Down
35 changes: 35 additions & 0 deletions influxdb_client/service/_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, api_client=None):
raise ValueError("Invalid value for `api_client`, must be defined.")
self.api_client = api_client
self._build_type = None
self._build_version = None

def _check_operation_params(self, operation_id, supported_params, local_params):
supported_params.append('async_req')
Expand All @@ -35,6 +36,16 @@ async def _is_cloud_instance_async(self) -> bool:
self._build_type = await self.build_type_async()
return 'cloud' in self._build_type.lower()

def _is_below_v2(self) -> bool:
if self._build_version is None:
self._build_version = self.build_version()
return self._build_version < '2'

async def _is_below_v2_async(self) -> bool:
if self._build_version is None:
self._build_version = await self.build_version()
return self._build_version < '2'

def build_type(self) -> str:
"""
Return the build type of the connected InfluxDB Server.
Expand All @@ -59,6 +70,30 @@ async def build_type_async(self) -> str:
response = await ping_service.get_ping_async(_return_http_data_only=False)
return self.response_header(response, header_name='X-Influxdb-Build')

def build_version(self) -> str:
"""
Return the version number of the connected InfluxDB Server.

:return: Version number of InfluxDB build.
"""
from influxdb_client import PingService
ping_service = PingService(self.api_client)

response = ping_service.get_ping_with_http_info(_return_http_data_only=False)
return self.response_header(response, header_name='X-Influxdb-Version')

async def build_version_async(self) -> str:
"""
Return the version number of the connected InfluxDB Server.

:return: Version number of InfluxDB build.
"""
from influxdb_client import PingService
ping_service = PingService(self.api_client)

response = await ping_service.get_ping_async(_return_http_data_only=False)
return self.response_header(response, header_name='X-Influxdb-Version')

def response_header(self, response, header_name='X-Influxdb-Version') -> str:
if response is not None and len(response) >= 3:
if header_name in response[2]:
Expand Down