from dataclasses import dataclass from flask import g from account.api import ows_client from account.constants import error, service from account.utils.exception import ExceptionDictOwsResponse @dataclass class VendorTermsAndConditions: latest_version: int latest_template: str agreed_version: int agreed_date: str def get_vendor_terms_and_conditions(vendor_id: int) -> VendorTermsAndConditions | None: """ Get terms and conditions info for a specific vendor. Returns: VendorTermsAndConditions | None: A dataclass containing terms and conditions information for the specified vendor, or None if no data is available. """ params = {'vendor_id': vendor_id} headers = { 'Orchard-Profile-Type': g.request_context.profile_type, 'Orchard-Profile-Id': g.request_context.profile_id, 'Orchard-Identity-Id': g.request_context.jwt_identity_id, } result = ows_client.get( service.OWS_COLLABORATOR, '/terms-and-conditions', headers=headers, params=params, ) if result.status_code != 200: raise ExceptionDictOwsResponse( status=result.status_code, code=error.ERROR_CODE_OWS_COLLABORATOR_REQUEST, message=result.text, ) data = result.json() # Handle null response data if data is None: return None # Validate response schema required_fields = ['latest_version', 'latest_template', 'agreed_version', 'agreed_date'] missing_fields = [field for field in required_fields if field not in data] if missing_fields: raise ExceptionDictOwsResponse( status=500, code=error.ERROR_CODE_OWS_COLLABORATOR_REQUEST, message=f'Invalid response schema: missing fields {missing_fields}', ) # Validate data types try: latest_version = data['latest_version'] if not isinstance(latest_version, int): raise ValueError(f'latest_version must be int, got {type(latest_version).__name__}') latest_template = data['latest_template'] if not isinstance(latest_template, str): raise ValueError(f'latest_template must be str, got {type(latest_template).__name__}') agreed_version = data['agreed_version'] if not isinstance(agreed_version, int): raise ValueError(f'agreed_version must be int, got {type(agreed_version).__name__}') agreed_date = data['agreed_date'] if not isinstance(agreed_date, str): raise ValueError(f'agreed_date must be str, got {type(agreed_date).__name__}') return VendorTermsAndConditions( latest_version=latest_version, latest_template=latest_template, agreed_version=agreed_version, agreed_date=agreed_date, ) except (TypeError, ValueError) as e: raise ExceptionDictOwsResponse( status=500, code=error.ERROR_CODE_OWS_COLLABORATOR_REQUEST, message=f'Invalid response data types: {str(e)}', )