|
| 1 | +# Copyright © 2025 Province of British Columbia |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +# the License. You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +# specific language governing permissions and limitations under the License. |
| 11 | +"""Works with the document service api.""" |
| 12 | + |
| 13 | +import json |
| 14 | +from http import HTTPStatus |
| 15 | + |
| 16 | +import requests |
| 17 | +from flask import current_app, jsonify |
| 18 | + |
| 19 | +from legal_api.exceptions import BusinessException |
| 20 | +from legal_api.models import Business, Document |
| 21 | +from legal_api.services import AccountService |
| 22 | + |
| 23 | + |
| 24 | +class DocumentService: |
| 25 | + """Service to create document records in document service api.""" |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + """Initialize the document service.""" |
| 29 | + self.url = current_app.config.get('DOCUMENT_SVC_URL') |
| 30 | + self.product_code = current_app.config.get('DOCUMENT_PRODUCT_CODE') |
| 31 | + self.api_key = current_app.config.get('DOCUMENT_API_KEY') |
| 32 | + |
| 33 | + def get_content(self, response): |
| 34 | + """Get the content of the response useful for test methods.""" |
| 35 | + c = response.content |
| 36 | + try: |
| 37 | + c = c.decode() |
| 38 | + c = json.loads(c) |
| 39 | + except Exception: |
| 40 | + pass |
| 41 | + return c |
| 42 | + |
| 43 | + # pylint: disable=too-many-arguments |
| 44 | + def create_document_record( |
| 45 | + self, |
| 46 | + business_id: int, |
| 47 | + filing_id: int, |
| 48 | + report_type: str, |
| 49 | + file_key: str, |
| 50 | + file_name: str): |
| 51 | + """Create a document record in the document table.""" |
| 52 | + new_document = Document( |
| 53 | + business_id=business_id, |
| 54 | + filing_id=filing_id, |
| 55 | + type=report_type, |
| 56 | + file_key=file_key, |
| 57 | + file_name=file_name, |
| 58 | + ) |
| 59 | + new_document.save() |
| 60 | + |
| 61 | + def has_document( |
| 62 | + self, |
| 63 | + business_identifier: str, |
| 64 | + filing_identifier: int, |
| 65 | + report_type: str): |
| 66 | + """ |
| 67 | + Check if a document exists in the document service. |
| 68 | +
|
| 69 | + business_identifier: The business identifier. |
| 70 | + filing_identifier: The filing identifier. |
| 71 | + report_type: The report type. |
| 72 | + account_id: The account id. |
| 73 | + return: True if the document exists, False otherwise. |
| 74 | + """ |
| 75 | + business_id = Business.find_by_identifier(business_identifier).id |
| 76 | + document = Document.find_one_by( |
| 77 | + business_id, |
| 78 | + filing_identifier, |
| 79 | + report_type) |
| 80 | + return document if document else False |
| 81 | + |
| 82 | + # pylint: disable=too-many-arguments |
| 83 | + def create_document( |
| 84 | + self, |
| 85 | + business_identifier: str, |
| 86 | + filing_identifier: int, |
| 87 | + report_type: str, |
| 88 | + account_id: str, |
| 89 | + binary_or_url): |
| 90 | + """ |
| 91 | + Create a document in the document service. |
| 92 | +
|
| 93 | + business_identifier: The business identifier. |
| 94 | + filing_identifier: The filing identifier. |
| 95 | + report_type: The report type. |
| 96 | + account_id: The account id. |
| 97 | + binary_or_url: The binary (pdf) or url of the document. |
| 98 | + """ |
| 99 | + if self.has_document(business_identifier, filing_identifier, report_type): |
| 100 | + raise BusinessException('Document already exists', HTTPStatus.CONFLICT) |
| 101 | + |
| 102 | + token = AccountService.get_bearer_token() |
| 103 | + headers = { |
| 104 | + 'Content-Type': 'application/json', |
| 105 | + 'X-ApiKey': self.api_key, |
| 106 | + 'Account-Id': account_id, |
| 107 | + 'Authorization': 'Bearer ' + token |
| 108 | + } |
| 109 | + post_url = (f'{self.url}/application-reports/' |
| 110 | + f'{self.product_code}/{business_identifier}/' |
| 111 | + f'{filing_identifier}/{report_type}') |
| 112 | + response = requests.post(url=post_url, headers=headers, data=binary_or_url) |
| 113 | + content = self.get_content(response) |
| 114 | + if response.status_code != HTTPStatus.CREATED: |
| 115 | + return jsonify(message=str(content)), response.status_code |
| 116 | + self.create_document_record( |
| 117 | + Business.find_by_identifier(business_identifier).id, |
| 118 | + filing_identifier, report_type, content['identifier'], |
| 119 | + f'{business_identifier}_{filing_identifier}_{report_type}.pdf' |
| 120 | + ) |
| 121 | + return content, response.status_code |
| 122 | + |
| 123 | + # pylint: disable=too-many-arguments |
| 124 | + def get_document( |
| 125 | + self, |
| 126 | + business_identifier: str, |
| 127 | + filing_identifier: int, |
| 128 | + report_type: str, |
| 129 | + account_id: str, |
| 130 | + file_key: str = None): |
| 131 | + """ |
| 132 | + Get a document from the document service. |
| 133 | +
|
| 134 | + business_identifier: The business identifier. |
| 135 | + filing_identifier: The filing identifier. |
| 136 | + report_type: The report type. |
| 137 | + account_id: The account id. |
| 138 | + return: The document url (or binary). |
| 139 | + """ |
| 140 | + token = AccountService.get_bearer_token() |
| 141 | + headers = { |
| 142 | + 'X-ApiKey': self.api_key, |
| 143 | + 'Account-Id': account_id, |
| 144 | + 'Content-Type': 'application/pdf', |
| 145 | + 'Authorization': 'Bearer ' + token |
| 146 | + } |
| 147 | + get_url = '' |
| 148 | + if file_key is not None: |
| 149 | + get_url = f'{self.url}/application-reports/{self.product_code}/{file_key}' |
| 150 | + else: |
| 151 | + document = self.has_document(business_identifier, filing_identifier, report_type) |
| 152 | + if document is False: |
| 153 | + raise BusinessException('Document not found', HTTPStatus.NOT_FOUND) |
| 154 | + get_url = f'{self.url}/application-reports/{self.product_code}/{document.file_key}' |
| 155 | + |
| 156 | + if get_url != '': |
| 157 | + response = requests.get(url=get_url, headers=headers) |
| 158 | + content = self.get_content(response) |
| 159 | + if response.status_code != HTTPStatus.OK: |
| 160 | + return jsonify(message=str(content)), response.status_code |
| 161 | + return content, response.status_code |
| 162 | + return jsonify(message='Document not found'), HTTPStatus.NOT_FOUND |
0 commit comments