Skip to content

Commit bc9d640

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

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-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 #

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)