Skip to content

Commit 5597940

Browse files
committed
Add initial Kibana client
1 parent c3f9839 commit 5597940

File tree

6 files changed

+344
-0
lines changed

6 files changed

+344
-0
lines changed

elasticsearch/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
TransportError,
6262
UnsupportedProductError,
6363
)
64+
from .kibana._async.client import AsyncKibana as AsyncKibana
65+
from .kibana._sync.client import Kibana as Kibana
6466
from .serializer import JSONSerializer, JsonSerializer
6567

6668
try:
@@ -75,9 +77,11 @@
7577
__all__ = [
7678
"ApiError",
7779
"AsyncElasticsearch",
80+
"AsyncKibana",
7881
"BadRequestError",
7982
"Elasticsearch",
8083
"JsonSerializer",
84+
"Kibana",
8185
"SerializationError",
8286
"TransportError",
8387
"NotFoundError",
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
import logging
20+
import typing as t
21+
22+
from elastic_transport import (
23+
ApiResponse,
24+
AsyncTransport,
25+
HttpHeaders,
26+
ObjectApiResponse,
27+
)
28+
from elastic_transport.client_utils import DEFAULT, DefaultType
29+
30+
from elasticsearch._async.client.utils import ( # _quote,; _rewrite_parameters,
31+
_TYPE_HOSTS,
32+
_base64_auth_header,
33+
_quote_query,
34+
client_node_configs,
35+
)
36+
37+
logger = logging.getLogger("elasticsearch.kibana")
38+
39+
40+
def resolve_auth_headers(
41+
api_key: t.Union[DefaultType, None, t.Tuple[str, str], str] = DEFAULT,
42+
) -> HttpHeaders:
43+
headers = HttpHeaders()
44+
if api_key is not DEFAULT and api_key is not None:
45+
headers["authorization"] = f"ApiKey {_base64_auth_header(api_key)}"
46+
47+
return headers
48+
49+
50+
class AsyncKibana:
51+
def __init__(
52+
self,
53+
hosts: _TYPE_HOSTS,
54+
*,
55+
api_key: t.Optional[t.Union[str, t.Tuple[str, str]]] = None,
56+
basic_auth: t.Optional[t.Union[str, t.Tuple[str, str]]] = None,
57+
) -> None:
58+
node_configs = client_node_configs(hosts, cloud_id=None)
59+
self._headers = resolve_auth_headers(api_key=api_key)
60+
self.transport = AsyncTransport(node_configs)
61+
62+
async def __aenter__(self) -> "AsyncKibana":
63+
try:
64+
await self.transport._async_call()
65+
except AttributeError:
66+
pass
67+
return self
68+
69+
async def __aexit__(self, *_: t.Any) -> None:
70+
await self.close()
71+
72+
async def close(self) -> None:
73+
"""Closes the Transport and all internal connections"""
74+
await self.transport.close()
75+
76+
async def perform_request(
77+
self,
78+
method: str,
79+
path: str,
80+
*,
81+
params: t.Optional[t.Mapping[str, t.Any]] = None,
82+
headers: t.Optional[t.Mapping[str, str]] = None,
83+
body: t.Optional[t.Any] = None,
84+
) -> ApiResponse[t.Any]:
85+
if headers:
86+
request_headers = self._headers.copy()
87+
request_headers.update(headers)
88+
else:
89+
request_headers = self._headers
90+
91+
if params:
92+
target = f"{path}?{_quote_query(params)}"
93+
else:
94+
target = path
95+
96+
meta, resp_body = await self.transport.perform_request(
97+
method, target, headers=request_headers, body=body
98+
)
99+
100+
return ObjectApiResponse(body=resp_body, meta=meta)
101+
102+
# AUTO-GENERATED-API-DEFINITIONS #
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class C:
19+
20+
@_rewrite_parameters()
21+
async def system_status(
22+
self,
23+
*,
24+
v7format: t.Optional[bool] = None,
25+
v8format: t.Optional[bool] = None,
26+
) -> ObjectApiResponse[t.Any]:
27+
"""
28+
Get Kibana's current status
29+
30+
:param v7format: Set to "true" to get the response in v7 format.
31+
:param v8format: Set to "true" to get the response in v8 format.
32+
"""
33+
__path_parts: t.Dict[str, str] = {}
34+
__path = "/api/status"
35+
__query: t.Dict[str, t.Any] = {}
36+
if v7format is not None:
37+
__query["v7format"] = v7format
38+
if v8format is not None:
39+
__query["v8format"] = v8format
40+
__headers = {"accept": "application/json"}
41+
return await self.perform_request( # type: ignore[return-value]
42+
"GET",
43+
__path,
44+
params=__query,
45+
headers=__headers,
46+
endpoint_id="_global.system_status",
47+
path_parts=__path_parts,
48+
)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
import logging
20+
import typing as t
21+
22+
from elastic_transport import ApiResponse, HttpHeaders, ObjectApiResponse, Transport
23+
from elastic_transport.client_utils import DEFAULT, DefaultType
24+
25+
from elasticsearch._async.client.utils import (
26+
_TYPE_HOSTS,
27+
_base64_auth_header,
28+
_quote,
29+
_quote_query,
30+
_rewrite_parameters,
31+
client_node_configs,
32+
)
33+
34+
logger = logging.getLogger("elasticsearch.kibana")
35+
36+
37+
def resolve_auth_headers(
38+
api_key: t.Union[DefaultType, None, t.Tuple[str, str], str] = DEFAULT,
39+
) -> HttpHeaders:
40+
headers = HttpHeaders()
41+
if api_key is not DEFAULT and api_key is not None:
42+
headers["authorization"] = f"ApiKey {_base64_auth_header(api_key)}"
43+
44+
return headers
45+
46+
47+
class Kibana:
48+
def __init__(
49+
self,
50+
hosts: _TYPE_HOSTS,
51+
*,
52+
api_key: t.Optional[t.Union[str, t.Tuple[str, str]]] = None,
53+
basic_auth: t.Optional[t.Union[str, t.Tuple[str, str]]] = None,
54+
) -> None:
55+
node_configs = client_node_configs(hosts, cloud_id=None)
56+
self._headers = resolve_auth_headers(api_key=api_key)
57+
self.transport = Transport(node_configs)
58+
59+
def __enter__(self) -> "Kibana":
60+
try:
61+
self.transport._async_call()
62+
except AttributeError:
63+
pass
64+
return self
65+
66+
def __exit__(self, *_: t.Any) -> None:
67+
self.close()
68+
69+
def close(self) -> None:
70+
"""Closes the Transport and all internal connections"""
71+
self.transport.close()
72+
73+
def perform_request(
74+
self,
75+
method: str,
76+
path: str,
77+
*,
78+
params: t.Optional[t.Mapping[str, t.Any]] = None,
79+
headers: t.Optional[t.Mapping[str, str]] = None,
80+
body: t.Optional[t.Any] = None,
81+
endpoint_id,
82+
path_parts,
83+
) -> ApiResponse[t.Any]:
84+
if headers:
85+
request_headers = self._headers.copy()
86+
request_headers.update(headers)
87+
else:
88+
request_headers = self._headers
89+
90+
if params:
91+
target = f"{path}?{_quote_query(params)}"
92+
else:
93+
target = path
94+
95+
meta, resp_body = self.transport.perform_request(
96+
method, target, headers=request_headers, body=body
97+
)
98+
99+
return ObjectApiResponse(body=resp_body, meta=meta)
100+
101+
# AUTO-GENERATED-API-DEFINITIONS #
102+
103+
@_rewrite_parameters()
104+
def system_status(
105+
self,
106+
*,
107+
v7format: t.Optional[bool] = None,
108+
v8format: t.Optional[bool] = None,
109+
) -> ObjectApiResponse[t.Any]:
110+
"""
111+
Get Kibana's current status
112+
113+
:param v7format: Set to "true" to get the response in v7 format.
114+
:param v8format: Set to "true" to get the response in v8 format.
115+
"""
116+
__path_parts: t.Dict[str, str] = {}
117+
__path = "/api/status"
118+
__query: t.Dict[str, t.Any] = {}
119+
if v7format is not None:
120+
__query["v7format"] = v7format
121+
if v8format is not None:
122+
__query["v8format"] = v8format
123+
__headers = {"accept": "application/json"}
124+
return self.perform_request( # type: ignore[return-value]
125+
"GET",
126+
__path,
127+
params=__query,
128+
headers=__headers,
129+
endpoint_id="system_status",
130+
path_parts=__path_parts,
131+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class C:
19+
20+
@_rewrite_parameters()
21+
def system_status(
22+
self,
23+
*,
24+
v7format: t.Optional[bool] = None,
25+
v8format: t.Optional[bool] = None,
26+
) -> ObjectApiResponse[t.Any]:
27+
"""
28+
Get Kibana's current status
29+
30+
:param v7format: Set to "true" to get the response in v7 format.
31+
:param v8format: Set to "true" to get the response in v8 format.
32+
"""
33+
__path_parts: t.Dict[str, str] = {}
34+
__path = "/api/status"
35+
__query: t.Dict[str, t.Any] = {}
36+
if v7format is not None:
37+
__query["v7format"] = v7format
38+
if v8format is not None:
39+
__query["v8format"] = v8format
40+
__headers = {"accept": "application/json"}
41+
return self.perform_request( # type: ignore[return-value]
42+
"GET",
43+
__path,
44+
params=__query,
45+
headers=__headers,
46+
endpoint_id="_global.system_status",
47+
path_parts=__path_parts,
48+
)

utils/run-unasync.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def main(check: bool = False):
8888
# Handling typing.Awaitable[...] isn't done yet by unasync.
8989
"_TYPE_ASYNC_SNIFF_CALLBACK": "_TYPE_SYNC_SNIFF_CALLBACK",
9090
},
91+
)
92+
)
93+
run(
94+
rule=unasync.Rule(
95+
fromdir="/elasticsearch/kibana/_async/client/",
96+
todir="/elasticsearch/kibana/_sync/client/",
97+
additional_replacements={
98+
# We want to rewrite to 'Transport' instead of 'SyncTransport', etc
99+
"AsyncTransport": "Transport",
100+
"AsyncKibana": "Kibana",
101+
},
91102
),
92103
check=check,
93104
)

0 commit comments

Comments
 (0)