Skip to content

Commit

Permalink
Added custom submission fetching in utils (AOT-Technologies#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhaib-aot authored Jul 8, 2024
1 parent cf869aa commit bf1c717
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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"
]
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
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

0 comments on commit bf1c717

Please sign in to comment.