-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhello_rest_handler.py
33 lines (28 loc) · 1.43 KB
/
hello_rest_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
#
# Copyright OpenSearch Contributors
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
import logging
from opensearch_sdk_py.rest.extension_rest_handler import ExtensionRestHandler
from opensearch_sdk_py.rest.extension_rest_request import ExtensionRestRequest
from opensearch_sdk_py.rest.extension_rest_response import ExtensionRestResponse
from opensearch_sdk_py.rest.named_route import NamedRoute
from opensearch_sdk_py.rest.rest_method import RestMethod
from opensearch_sdk_py.rest.rest_status import RestStatus
class HelloRestHandler(ExtensionRestHandler):
def handle_request(self, rest_request: ExtensionRestRequest) -> ExtensionRestResponse:
logging.debug(f"handling {rest_request}")
name = rest_request.param("name")
if name is not None:
response_bytes = bytes(f"Hello {name}! 👋\n", "utf-8")
else:
response_bytes = bytes("Hello from Python! 👋\n", "utf-8")
return ExtensionRestResponse(rest_request, RestStatus.OK, response_bytes, ExtensionRestResponse.TEXT_CONTENT_TYPE)
@property
def routes(self) -> list[NamedRoute]:
return [NamedRoute(method=RestMethod.GET, path="/hello", unique_name="greeting"), NamedRoute(method=RestMethod.GET, path="/hello/{name}", unique_name="personal_greeting")]