forked from AOT-Technologies/forms-flow-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom submission fetching in utils (AOT-Technologies#2135)
- Loading branch information
1 parent
cf869aa
commit bf1c717
Showing
3 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
forms-flow-api-utils/src/formsflow_api_utils/services/__init__.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
"""This exports all of the services used by the application.""" | ||
|
||
from formsflow_api_utils.services.external.formio import FormioService | ||
|
||
from formsflow_api_utils.services.external.custom_submission import CustomSubmissionService | ||
__all__ = [ | ||
"FormioService", | ||
"CustomSubmissionService" | ||
] |
1 change: 1 addition & 0 deletions
1
forms-flow-api-utils/src/formsflow_api_utils/services/external/__init__.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""This exports all of the external communication services used by the application.""" | ||
|
||
from .formio import FormioService | ||
from .custom_submission import CustomSubmissionService |
33 changes: 33 additions & 0 deletions
33
forms-flow-api-utils/src/formsflow_api_utils/services/external/custom_submission.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,33 @@ | ||
import requests | ||
from flask import current_app | ||
|
||
from formsflow_api_utils.exceptions import BusinessException, ExternalError | ||
from formsflow_api_utils.utils import HTTP_TIMEOUT | ||
from typing import Any | ||
|
||
class CustomSubmissionService: | ||
|
||
def __init__(self) -> None: | ||
self.base_url = current_app.config.get("CUSTOM_SUBMISSION_URL") | ||
|
||
def __get_headers(self, token: str) -> dict: | ||
"""Returns the headers for the request.""" | ||
return {"Authorization": token, "Content-Type": "application/json"} | ||
|
||
def fetch_submission_data(self, token: str, form_id: str, submission_id: str) -> Any: | ||
"""Returns the submission data from the form adapter.""" | ||
if not self.base_url: | ||
raise BusinessException("Base URL for custom submission is not configured.") | ||
|
||
submission_url = f"{self.base_url}/form/{form_id}/submission/{submission_id}" | ||
current_app.logger.debug(f"Fetching custom submission data: {submission_url}") | ||
headers = self.__get_headers(token) | ||
|
||
try: | ||
response = requests.get(submission_url, headers=headers, timeout=HTTP_TIMEOUT) | ||
current_app.logger.debug(f"Custom submission response code: {response.status_code}") | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
current_app.logger.error(f"Error fetching custom submission data: {str(e)}") | ||
raise ExternalError("Failed to fetch custom submission data.") from e |