Skip to content
Snippets Groups Projects
Commit 8ffd0382 authored by Neda Moeini's avatar Neda Moeini
Browse files

Applied review suggestions.

parent e18f04fd
No related branches found
No related tags found
1 merge request!142Added middleware to modify porecess details response and replace the callback...
Pipeline #85401 failed
API Reference
=============
This page contains auto-generated API reference documentation [#f1]_.
.. toctree::
:titlesonly:
gso/gso
.. [#f1] Created with `sphinx-autodoc2 <https://github.com/chrisjsewell/sphinx-autodoc2>`_
``gso.api.v1.processes``
============================
.. automodule:: gso.api.v1.processes
:members:
:show-inheritance:
...@@ -24,18 +24,14 @@ class CallBackResultsBaseModel(OrchestratorBaseModel): ...@@ -24,18 +24,14 @@ class CallBackResultsBaseModel(OrchestratorBaseModel):
def callback_results(step_id: UUID) -> dict[str, Any]: def callback_results(step_id: UUID) -> dict[str, Any]:
"""Retrieve callback results for a specific process step. """Retrieve callback results for a specific process step.
Args: :param step_id: The unique identifier of the process step.
---- :type step_id: UUID
step_id (UUID): The unique identifier of the process step.
:return: Dictionary containing callback results.
Returns: :rtype: dict[str, Any]
-------
dict: Dictionary containing callback results. :raises HTTPException: 404 status code if the specified step_id is not found or if the 'callback_result' key
is not present in the state.
Raises:
------
HTTPException: 404 status code if the specified step_id is not found or if
the 'callback_result' key is not present in the state.
""" """
step = ProcessStepTable.query.filter(ProcessStepTable.step_id == step_id).first() step = ProcessStepTable.query.filter(ProcessStepTable.step_id == step_id).first()
......
...@@ -17,19 +17,21 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -17,19 +17,21 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: Callable) -> Response: async def dispatch(self, request: Request, call_next: Callable) -> Response:
"""Middleware to modify the response for Process details endpoint. """Middleware to modify the response for Process details endpoint.
Args: :param request: The incoming HTTP request.
---- :type request: Request
request (Request): The incoming HTTP request.
call_next (Callable): The next middleware or endpoint in the stack. :param call_next: The next middleware or endpoint in the stack.
:type call_next: Callable
Returns:
------- :return: The modified HTTP response.
Response: The modified HTTP response. :rtype: Response
""" """
response = await call_next(request) response = await call_next(request)
path_pattern = r"/api/processes/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})" path_pattern = re.compile(
r"/api/processes/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"
)
match = re.match(path_pattern, request.url.path) match = path_pattern.match(request.url.path)
if match and response.status_code == HTTP_200_OK: if match and response.status_code == HTTP_200_OK:
# Modify the response body as needed # Modify the response body as needed
...@@ -38,7 +40,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -38,7 +40,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
response_body += chunk response_body += chunk
try: try:
json_content = json.loads(response_body) json_content = json.loads(response_body)
await self.modify_response_body(json_content, request) await self._modify_response_body(json_content, request)
modified_response_body = json.dumps(json_content).encode() modified_response_body = json.dumps(json_content).encode()
headers = dict(response.headers) headers = dict(response.headers)
headers["content-length"] = str(len(modified_response_body)) headers["content-length"] = str(len(modified_response_body))
...@@ -58,13 +60,11 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -58,13 +60,11 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
async def _get_token(request: Request) -> str: async def _get_token(request: Request) -> str:
"""Get the token from the request headers. """Get the token from the request headers.
Args: :param request: The incoming HTTP request.
---- :type request: Request
request (Request): The incoming HTTP request.
Returns: :return: The token from the request headers in specific format.
------- :rtype: str
str: The token from the request headers in specific format.
""" """
bearer_prefix = "Bearer " bearer_prefix = "Bearer "
authorization_header = request.headers.get("Authorization") authorization_header = request.headers.get("Authorization")
...@@ -74,13 +74,15 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -74,13 +74,15 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
return f"?token={token}" return f"?token={token}"
return "" return ""
async def modify_response_body(self, response_body: dict[str, Any], request: Request) -> None: async def _modify_response_body(self, response_body: dict[str, Any], request: Request) -> None:
"""Modify the response body as needed. """Modify the response body as needed.
Args: :param response_body: The response body in dictionary format.
---- :type response_body: dict[str, Any]
response_body (Dict[str, Any]): The response body in dictionary format. :param request: The incoming HTTP request.
request (Request): The incoming HTTP request. :type request: Request
:return: None
""" """
max_output_length = 500 max_output_length = 500
token = await self._get_token(request) token = await self._get_token(request)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment