|
| 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 # |
0 commit comments