-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract the common code from client_interface.py to the bas…
…e_client_interface.py. Signed-off-by: Hongli Chen <[email protected]>
- Loading branch information
1 parent
2f4946b
commit 2766d8b
Showing
3 changed files
with
135 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/openjd/adaptor_runtime_client/posix_client_interface.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
from __future__ import annotations | ||
|
||
import signal as _signal | ||
from .base_client_interface import Response as _Response | ||
from typing import Dict as _Dict | ||
|
||
from .base_client_interface import BaseClientInterface | ||
from .connection import UnixHTTPConnection as _UnixHTTPConnection | ||
from urllib.parse import urlencode as _urlencode | ||
|
||
|
||
# Set timeout to None so our requests are blocking calls with no timeout. | ||
# See socket.settimeout | ||
_REQUEST_TIMEOUT = None | ||
|
||
|
||
class HTTPClientInterface(BaseClientInterface): | ||
socket_path: str | ||
|
||
def __init__(self, socket_path: str) -> None: | ||
"""When the client is created, we need the port number to connect to the server. | ||
Args: | ||
socket_path (str): The path to the UNIX domain socket to use. | ||
""" | ||
super().__init__() | ||
self.socket_path = socket_path | ||
# NOTE: The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. | ||
# Reference: https://man7.org/linux/man-pages/man7/signal.7.html | ||
# SIGTERM graceful shutdown. | ||
_signal.signal(_signal.SIGTERM, self.graceful_shutdown) | ||
|
||
def _send_request( | ||
self, method: str, request_path: str, *, query_string_params: _Dict | None = None | ||
) -> _Response: | ||
""" | ||
Send a request to the server and return the response. | ||
Args: | ||
method (str): The HTTP method, e.g. 'GET', 'POST'. | ||
request_path (str): The path for the request. | ||
query_string_params (_Dict | None, optional): Query string parameters to include in the request. | ||
Defaults to None. In Linux, the query string parameters will be added to the URL | ||
Returns: | ||
Response: The response from the server. | ||
""" | ||
headers = { | ||
"Content-type": "application/json", | ||
} | ||
connection = _UnixHTTPConnection(self.socket_path, timeout=_REQUEST_TIMEOUT) | ||
if query_string_params: | ||
request_path += "?" + _urlencode(query_string_params) | ||
connection.request(method, request_path, headers=headers) | ||
response = connection.getresponse() | ||
connection.close() | ||
length = response.length if response.length else 0 | ||
body = response.read().decode() if length else "" | ||
return _Response(response.status, body, response.reason, length) |