-
Notifications
You must be signed in to change notification settings - Fork 81
24632 Document Record Service Cont. In Integration #3141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature-drs-integration
Are you sure you want to change the base?
Changes from all commits
4738d15
22093c8
a5270e9
b9a688b
21b5efe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,177 @@ | ||||||
| # Copyright © 2021 Province of British Columbia | ||||||
| # | ||||||
| # Licensed under the Apache License, Version 2.0 (the 'License'); | ||||||
| # you may not use this file except in compliance with the License. | ||||||
| # You may obtain a copy of the License at | ||||||
| # | ||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| # | ||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||
| # distributed under the License is distributed on an 'AS IS' BASIS, | ||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
| """This module is a wrapper for Document Record Service.""" | ||||||
|
|
||||||
| import base64 | ||||||
| from typing import Optional | ||||||
| import requests | ||||||
| from flask import current_app, request | ||||||
| from flask_babel import _ | ||||||
| import PyPDF2 | ||||||
|
|
||||||
| from legal_api.constants import DocumentTypeEnum | ||||||
|
|
||||||
| class DocumentRecordService: | ||||||
| """Document Storage class.""" | ||||||
|
|
||||||
|
|
||||||
| @staticmethod | ||||||
| def upload_document(document_class: str, document_type: str) -> dict: | ||||||
| """Upload document to Docuemtn Record Service.""" | ||||||
| query_params = request.args.to_dict() | ||||||
| file = request.data.get('file') | ||||||
| # Ensure file exists | ||||||
| if not file: | ||||||
| current_app.logger.debug('No file found in request.') | ||||||
| return {'data': 'File not provided'} | ||||||
| current_app.logger.debug(f'Upload file to document record service {file.filename}') | ||||||
| DRS_BASE_URL = current_app.config.get('DRS_BASE_URL', '') # pylint: disable=invalid-name | ||||||
| url = f'{DRS_BASE_URL}/documents/{document_class}/{document_type}' | ||||||
|
|
||||||
| # Validate file size and encryption status before submitting to DRS. | ||||||
| validation_error = DocumentRecordService.validate_pdf(file, request.content_length, document_type) | ||||||
| if validation_error: | ||||||
| return { | ||||||
| 'error': validation_error | ||||||
| } | ||||||
|
|
||||||
| try: | ||||||
| # Read and encode the file content as base64 | ||||||
| file_content = file.read() | ||||||
| file_base64 = base64.b64encode(file_content).decode('utf-8') | ||||||
|
|
||||||
| response_body = requests.post( | ||||||
| url, | ||||||
| params=query_params, | ||||||
| json={ | ||||||
| 'filename': file.filename, | ||||||
| 'content': file_base64, | ||||||
| 'content_type': file.content_type, | ||||||
| }, | ||||||
| headers={ | ||||||
| 'x-apikey': current_app.config.get('DRS_X_API_KEY', ''), | ||||||
| 'Account-Id': current_app.config.get('DRS_ACCOUNT_ID', ''), | ||||||
| 'Content-Type': file.content_type | ||||||
| } | ||||||
| ).json() | ||||||
|
|
||||||
| current_app.logger.debug(f'Upload file to document record service {response_body}') | ||||||
| return { | ||||||
| 'documentServiceId': response_body['documentServiceId'], | ||||||
| 'consumerDocumentId': response_body['consumerDocumentId'], | ||||||
| 'consumerFilename': response_body['consumerFilename'] | ||||||
| } | ||||||
| except Exception as e: | ||||||
| current_app.logger.debug(f"Error on uploading document {e}") | ||||||
| return {} | ||||||
|
|
||||||
| @staticmethod | ||||||
| def delete_document(document_service_id: str) -> dict: | ||||||
| """Delete document from Document Record Service.""" | ||||||
| DRS_BASE_URL = current_app.config.get('DRS_BASE_URL', '') # pylint: disable=invalid-name | ||||||
| url = f'{DRS_BASE_URL}/documents/{document_service_id}' | ||||||
|
|
||||||
| try: | ||||||
| response = requests.patch( | ||||||
| url, json={ 'removed': True }, | ||||||
| headers={ | ||||||
| 'x-apikey': current_app.config.get('DRS_X_API_KEY', ''), | ||||||
| 'Account-Id': current_app.config.get('DRS_ACCOUNT_ID', ''), | ||||||
| } | ||||||
| ).json() | ||||||
| current_app.logger.debug(f'Delete document from document record service {response}') | ||||||
| return response | ||||||
| except Exception as e: | ||||||
| current_app.logger.debug(f'Error on deleting document {e}') | ||||||
| return {} | ||||||
|
|
||||||
| @staticmethod | ||||||
| def get_document(document_class: str, document_service_id: str) -> dict: | ||||||
| """Get document record from Document Record Service.""" | ||||||
| DRS_BASE_URL = current_app.config.get('DRS_BASE_URL', '') # pylint: disable=invalid-name | ||||||
| url = f'{DRS_BASE_URL}/searches/{document_class}?documentServiceId={document_service_id}' | ||||||
| try: | ||||||
| response = requests.get( | ||||||
| url, | ||||||
| headers={ | ||||||
| 'x-apikey': current_app.config.get('DRS_X_API_KEY', ''), | ||||||
| 'Account-Id': current_app.config.get('DRS_ACCOUNT_ID', ''), | ||||||
| } | ||||||
| ).json() | ||||||
| current_app.logger.debug(f'Get document from document record service {response}') | ||||||
| return response[0] | ||||||
| except Exception as e: | ||||||
| current_app.logger.debug(f'Error on getting a document object {e}') | ||||||
| return {} | ||||||
|
|
||||||
| @staticmethod | ||||||
| def download_document(document_class: str, document_service_id: str) -> dict: | ||||||
| """Download document from Document Record Service.""" | ||||||
| doc_object = DocumentRecordService.get_document(document_class, document_service_id) | ||||||
|
|
||||||
| response = requests.get(doc_object['documentURL']) # Download file from storage | ||||||
| response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx) | ||||||
|
|
||||||
| return response | ||||||
|
|
||||||
| @staticmethod | ||||||
| def update_business_identifier(business_identifier: str, document_service_id: str): | ||||||
| """Update business identifier up on approval.""" | ||||||
| DRS_BASE_URL = current_app.config.get('DRS_BASE_URL', '') # pylint: disable=invalid-name | ||||||
| url = f'{DRS_BASE_URL}/documents/{document_service_id}' | ||||||
|
|
||||||
| try: | ||||||
| response = requests.patch( | ||||||
| url, json={ 'consumerIdentifer': business_identifier }, | ||||||
|
||||||
| url, json={ 'consumerIdentifer': business_identifier }, | |
| url, json={ 'consumerIdentifier': business_identifier }, |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |||||
|
|
||||||
| from legal_api.errors import Error | ||||||
| from legal_api.models import Business | ||||||
| from legal_api.services import MinioService, flags, namex | ||||||
| from legal_api.services import MinioService, flags, namex, DocumentRecordService | ||||||
| from legal_api.services.utils import get_str | ||||||
| from legal_api.utils.datetime import datetime as dt | ||||||
|
|
||||||
|
|
@@ -329,3 +329,12 @@ def validate_foreign_jurisdiction(foreign_jurisdiction: dict, | |||||
| msg.append({'error': 'Invalid region.', 'path': f'{foreign_jurisdiction_path}/region'}) | ||||||
|
|
||||||
| return msg | ||||||
|
|
||||||
| def validate_file_on_drs(document_class: str, document_service_id: str, path) -> bool: | ||||||
|
||||||
| def validate_file_on_drs(document_class: str, document_service_id: str, path) -> bool: | |
| def validate_file_on_drs(document_class: str, document_service_id: str, path) -> list: |
Uh oh!
There was an error while loading. Please reload this page.