Skip to content

Commit 5834332

Browse files
authored
feat: add PingService to check status of OSS and Cloud instance (#352)
1 parent 503de13 commit 5834332

12 files changed

+289
-16
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 1.23.0 [unreleased]
22

3+
### Deprecates
4+
- `InfluxDBClient.health()`: instead use `InfluxDBClient.ping()`
5+
6+
### Features
7+
1. [#352](https://github.com/influxdata/influxdb-client-python/pull/352): Add `PingService` to check status of OSS and Cloud instance
8+
39
## 1.22.0 [2021-10-22]
410

511
### Features

README.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -1146,9 +1146,9 @@ The following forward compatible APIs are available:
11461146
======================================================= ==================================================================================================== =======
11471147
API Endpoint Description
11481148
======================================================= ==================================================================================================== =======
1149-
`query_api.py <influxdb_client/client/query_api.py>`_ `/api/v2/query <https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-query-http-endpoint>`_ Query data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and `Flux <https://docs.influxdata.com/flux/latest/>`_ (endpoint should be enabled by `flux-enabled option <https://docs.influxdata.com/influxdb/v1.8/administration/config/#flux-enabled-false>`_)
1150-
`write_api.py <influxdb_client/client/write_api.py>`_ `/api/v2/write <https://docs.influxdata.com/influxdb/latest/tools/api/#api-v2-write-http-endpoint>`_ Write data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API
1151-
`health() <influxdb_client/client/influxdb_client.py>`_ `/health <https://docs.influxdata.com/influxdb/latest/tools/api/#health-http-endpointt>`_ Check the health of your InfluxDB instance
1149+
`query_api.py <influxdb_client/client/query_api.py>`_ `/api/v2/query <https://docs.influxdata.com/influxdb/v1.8/tools/api/#apiv2query-http-endpoint>`_ Query data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and `Flux <https://docs.influxdata.com/flux/latest/>`_ (endpoint should be enabled by `flux-enabled option <https://docs.influxdata.com/influxdb/v1.8/administration/config/#flux-enabled-false>`_)
1150+
`write_api.py <influxdb_client/client/write_api.py>`_ `/api/v2/write <https://docs.influxdata.com/influxdb/v1.8/tools/api/#apiv2write-http-endpoint>`_ Write data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API
1151+
`ping() <influxdb_client/client/influxdb_client.py>`_ `/ping <https://docs.influxdata.com/influxdb/v1.8/tools/api/#ping-http-endpoint>`_ Check the status of your InfluxDB instance
11521152
======================================================= ==================================================================================================== =======
11531153

11541154
For detail info see `InfluxDB 1.8 example <examples/influxdb_18_example.py>`_.

influxdb_client/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
2828
from influxdb_client.service.notification_rules_service import NotificationRulesService
2929
from influxdb_client.service.organizations_service import OrganizationsService
30+
from influxdb_client.service.ping_service import PingService
3031
from influxdb_client.service.query_service import QueryService
3132
from influxdb_client.service.ready_service import ReadyService
3233
from influxdb_client.service.rules_service import RulesService

influxdb_client/client/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
2626
from influxdb_client.service.notification_rules_service import NotificationRulesService
2727
from influxdb_client.service.organizations_service import OrganizationsService
28+
from influxdb_client.service.ping_service import PingService
2829
from influxdb_client.service.query_service import QueryService
2930
from influxdb_client.service.ready_service import ReadyService
3031
from influxdb_client.service.rules_service import RulesService

influxdb_client/client/influxdb_client.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from __future__ import absolute_import
44

55
import configparser
6+
import logging
67
import os
78
import base64
9+
import warnings
810

9-
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
11+
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService, PingService
1012
from influxdb_client.client.authorizations_api import AuthorizationsApi
1113
from influxdb_client.client.bucket_api import BucketsApi
1214
from influxdb_client.client.delete_api import DeleteApi
@@ -18,6 +20,9 @@
1820
from influxdb_client.client.write_api import WriteApi, WriteOptions, PointSettings
1921

2022

23+
logger = logging.getLogger(__name__)
24+
25+
2126
class InfluxDBClient(object):
2227
"""InfluxDBClient is client for InfluxDB v2."""
2328

@@ -403,6 +408,7 @@ def health(self) -> HealthCheck:
403408
404409
:return: HealthCheck
405410
"""
411+
warnings.warn("This method is deprecated. Call 'ping()' instead.", DeprecationWarning)
406412
health_service = HealthService(self.api_client)
407413

408414
try:
@@ -411,6 +417,36 @@ def health(self) -> HealthCheck:
411417
except Exception as e:
412418
return HealthCheck(name="influxdb", message=str(e), status="fail")
413419

420+
def ping(self) -> bool:
421+
"""
422+
Return the the status of InfluxDB instance.
423+
424+
:return: The status of InfluxDB.
425+
"""
426+
ping_service = PingService(self.api_client)
427+
428+
try:
429+
ping_service.get_ping()
430+
return True
431+
except Exception as ex:
432+
logger.error("Unexpected error during /ping: %s", ex)
433+
return False
434+
435+
def version(self) -> str:
436+
"""
437+
Return the version of the connected InfluxDB Server.
438+
439+
:return: The version of InfluxDB.
440+
"""
441+
ping_service = PingService(self.api_client)
442+
443+
response = ping_service.get_ping_with_http_info(_return_http_data_only=False)
444+
if response is not None and len(response) >= 3:
445+
if 'X-Influxdb-Version' in response[2]:
446+
return response[2]['X-Influxdb-Version']
447+
448+
return "unknown"
449+
414450
def ready(self) -> Ready:
415451
"""
416452
Get The readiness of the InfluxDB 2.0.

influxdb_client/client/write/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
2626
from influxdb_client.service.notification_rules_service import NotificationRulesService
2727
from influxdb_client.service.organizations_service import OrganizationsService
28+
from influxdb_client.service.ping_service import PingService
2829
from influxdb_client.service.query_service import QueryService
2930
from influxdb_client.service.ready_service import ReadyService
3031
from influxdb_client.service.rules_service import RulesService

influxdb_client/service/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
2626
from influxdb_client.service.notification_rules_service import NotificationRulesService
2727
from influxdb_client.service.organizations_service import OrganizationsService
28+
from influxdb_client.service.ping_service import PingService
2829
from influxdb_client.service.query_service import QueryService
2930
from influxdb_client.service.ready_service import ReadyService
3031
from influxdb_client.service.rules_service import RulesService
+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# coding: utf-8
2+
3+
"""
4+
Influx OSS API Service.
5+
6+
The InfluxDB v2 API provides a programmatic interface for all interactions with InfluxDB. Access the InfluxDB API using the `/api/v2/` endpoint. # noqa: E501
7+
8+
OpenAPI spec version: 2.0.0
9+
Generated by: https://openapi-generator.tech
10+
"""
11+
12+
13+
from __future__ import absolute_import
14+
15+
import re # noqa: F401
16+
17+
# python 2 and python 3 compatibility library
18+
import six
19+
20+
21+
class PingService(object):
22+
"""NOTE: This class is auto generated by OpenAPI Generator.
23+
24+
Ref: https://openapi-generator.tech
25+
26+
Do not edit the class manually.
27+
"""
28+
29+
def __init__(self, api_client=None): # noqa: E501,D401,D403
30+
"""PingService - a operation defined in OpenAPI."""
31+
if api_client is None:
32+
raise ValueError("Invalid value for `api_client`, must be defined.")
33+
self.api_client = api_client
34+
35+
def get_ping(self, **kwargs): # noqa: E501,D401,D403
36+
"""Checks the status of InfluxDB instance and version of InfluxDB..
37+
38+
This method makes a synchronous HTTP request by default. To make an
39+
asynchronous HTTP request, please pass async_req=True
40+
>>> thread = api.get_ping(async_req=True)
41+
>>> result = thread.get()
42+
43+
:param async_req bool
44+
:return: None
45+
If the method is called asynchronously,
46+
returns the request thread.
47+
""" # noqa: E501
48+
kwargs['_return_http_data_only'] = True
49+
if kwargs.get('async_req'):
50+
return self.get_ping_with_http_info(**kwargs) # noqa: E501
51+
else:
52+
(data) = self.get_ping_with_http_info(**kwargs) # noqa: E501
53+
return data
54+
55+
def get_ping_with_http_info(self, **kwargs): # noqa: E501,D401,D403
56+
"""Checks the status of InfluxDB instance and version of InfluxDB..
57+
58+
This method makes a synchronous HTTP request by default. To make an
59+
asynchronous HTTP request, please pass async_req=True
60+
>>> thread = api.get_ping_with_http_info(async_req=True)
61+
>>> result = thread.get()
62+
63+
:param async_req bool
64+
:return: None
65+
If the method is called asynchronously,
66+
returns the request thread.
67+
""" # noqa: E501
68+
local_var_params = locals()
69+
70+
all_params = [] # noqa: E501
71+
all_params.append('async_req')
72+
all_params.append('_return_http_data_only')
73+
all_params.append('_preload_content')
74+
all_params.append('_request_timeout')
75+
all_params.append('urlopen_kw')
76+
77+
for key, val in six.iteritems(local_var_params['kwargs']):
78+
if key not in all_params:
79+
raise TypeError(
80+
"Got an unexpected keyword argument '%s'"
81+
" to method get_ping" % key
82+
)
83+
local_var_params[key] = val
84+
del local_var_params['kwargs']
85+
86+
collection_formats = {}
87+
88+
path_params = {}
89+
90+
query_params = []
91+
92+
header_params = {}
93+
94+
form_params = []
95+
local_var_files = {}
96+
97+
body_params = None
98+
# Authentication setting
99+
auth_settings = [] # noqa: E501
100+
101+
# urlopen optional setting
102+
urlopen_kw = None
103+
if 'urlopen_kw' in kwargs:
104+
urlopen_kw = kwargs['urlopen_kw']
105+
106+
return self.api_client.call_api(
107+
'/ping', 'GET',
108+
path_params,
109+
query_params,
110+
header_params,
111+
body=body_params,
112+
post_params=form_params,
113+
files=local_var_files,
114+
response_type=None, # noqa: E501
115+
auth_settings=auth_settings,
116+
async_req=local_var_params.get('async_req'),
117+
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
118+
_preload_content=local_var_params.get('_preload_content', True),
119+
_request_timeout=local_var_params.get('_request_timeout'),
120+
collection_formats=collection_formats,
121+
urlopen_kw=urlopen_kw)
122+
123+
def head_ping(self, **kwargs): # noqa: E501,D401,D403
124+
"""Checks the status of InfluxDB instance and version of InfluxDB..
125+
126+
This method makes a synchronous HTTP request by default. To make an
127+
asynchronous HTTP request, please pass async_req=True
128+
>>> thread = api.head_ping(async_req=True)
129+
>>> result = thread.get()
130+
131+
:param async_req bool
132+
:return: None
133+
If the method is called asynchronously,
134+
returns the request thread.
135+
""" # noqa: E501
136+
kwargs['_return_http_data_only'] = True
137+
if kwargs.get('async_req'):
138+
return self.head_ping_with_http_info(**kwargs) # noqa: E501
139+
else:
140+
(data) = self.head_ping_with_http_info(**kwargs) # noqa: E501
141+
return data
142+
143+
def head_ping_with_http_info(self, **kwargs): # noqa: E501,D401,D403
144+
"""Checks the status of InfluxDB instance and version of InfluxDB..
145+
146+
This method makes a synchronous HTTP request by default. To make an
147+
asynchronous HTTP request, please pass async_req=True
148+
>>> thread = api.head_ping_with_http_info(async_req=True)
149+
>>> result = thread.get()
150+
151+
:param async_req bool
152+
:return: None
153+
If the method is called asynchronously,
154+
returns the request thread.
155+
""" # noqa: E501
156+
local_var_params = locals()
157+
158+
all_params = [] # noqa: E501
159+
all_params.append('async_req')
160+
all_params.append('_return_http_data_only')
161+
all_params.append('_preload_content')
162+
all_params.append('_request_timeout')
163+
all_params.append('urlopen_kw')
164+
165+
for key, val in six.iteritems(local_var_params['kwargs']):
166+
if key not in all_params:
167+
raise TypeError(
168+
"Got an unexpected keyword argument '%s'"
169+
" to method head_ping" % key
170+
)
171+
local_var_params[key] = val
172+
del local_var_params['kwargs']
173+
174+
collection_formats = {}
175+
176+
path_params = {}
177+
178+
query_params = []
179+
180+
header_params = {}
181+
182+
form_params = []
183+
local_var_files = {}
184+
185+
body_params = None
186+
# Authentication setting
187+
auth_settings = [] # noqa: E501
188+
189+
# urlopen optional setting
190+
urlopen_kw = None
191+
if 'urlopen_kw' in kwargs:
192+
urlopen_kw = kwargs['urlopen_kw']
193+
194+
return self.api_client.call_api(
195+
'/ping', 'HEAD',
196+
path_params,
197+
query_params,
198+
header_params,
199+
body=body_params,
200+
post_params=form_params,
201+
files=local_var_files,
202+
response_type=None, # noqa: E501
203+
auth_settings=auth_settings,
204+
async_req=local_var_params.get('async_req'),
205+
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
206+
_preload_content=local_var_params.get('_preload_content', True),
207+
_request_timeout=local_var_params.get('_request_timeout'),
208+
collection_formats=collection_formats,
209+
urlopen_kw=urlopen_kw)

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from influxdb_client.service.notification_endpoints_service import NotificationEndpointsService
2626
from influxdb_client.service.notification_rules_service import NotificationRulesService
2727
from influxdb_client.service.organizations_service import OrganizationsService
28+
from influxdb_client.service.ping_service import PingService
2829
from influxdb_client.service.query_service import QueryService
2930
from influxdb_client.service.ready_service import ReadyService
3031
from influxdb_client.service.rules_service import RulesService

0 commit comments

Comments
 (0)