o
    pj)                     @  s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ edZeddddeddddfedddddgdddedddddd gdd!d"d#ed$d%dd&d'gd(d)d*d+d,d&d-d.d/dd'd0gddd!d1d2ed3d4d5d6d7d8d9d:d;d<d=
d>	d?ed@dAdBdCdDdEdFdGZdHdI ZdJdK ZdLdM ZdNdO Z dbdRdSZ!dTdU Z"dcdWdXZ#ddd[d\Z$d]d^ Z%d_d` Z&daS )ez>Platform App Standard v1 entrypoint for Australian ABN Lookup.    )annotations)RequestException)AbnLookupError)lookup_abn_v1)lookup_abn_basic)ActionContractAgentCapabilityContractAppManifestCredentialFieldWordPressPackageContract)error_envelopesuccess_envelope)get_platform_loggerlog_structured)SecretConfigurationError)SecretsManagerabn_lookup_appz
ABN Lookupz1.0.0z4Look up Australian Business Register records by ABN.abr_lookup_guidzABR authentication GUIDz+Issued by the Australian Business Register.)namelabel	help_textzplatform.healthz/Report local app health without contacting ABR.objecthealthycredential_configured)typerequired )
capabilitydescriptionoutput_schemaerrorszplatform.credentials.testz3Validate the tenant ABR GUID with a minimal lookup.)r   	connectedprovider   )credential_not_configuredprovider_connection_failed)r   r   required_credentialsr   timeout_secondsr    business.au.abn.lookupz:Return one normalized Australian Business Register record.Fabnstringz^[0-9]{11}$z*Exact 11-digit Australian Business Number.)r   patternr   boolean)r   default)r)   include_history)r   additionalPropertiesr   
propertiesrecordbusiness)invalid_inputinvalid_abnr$   provider_unavailableprovider_invalid_responseTzLook up an Australian ABNz=Retrieve the normalized ABR record for an exact 11-digit ABN.)australian_business_registrysupplier_verification)z6The user provides an exact Australian Business Number.z>The user asks to validate an Australian business registration.)z'The user provides only a business name.z3The request is unrelated to an Australian business.readlownever)zabn_lookup.read)
exposedtitlesummarydomainsuse_whendo_not_use_whenrisk_category
risk_levelconfirmationrequired_permissions)	r   r   input_schemar   r&   r'   data_classificationr    agent)	health_v1test_connection_v1	lookup_v1abn_lookup_wpz	1.1.1-devzwordpress-pack/abn-lookup-wpzabn-lookup-wp.phpzabn-lookup-wp/abn-lookup-wp.php)r(   )
package_idversionsource_directory
entrypointplugin_filecapabilities)app_iddisplay_nameapp_versionr   credentialsactionswordpress_packagec                 C  sl   |dkrt | S |dkrt| S |dkrt| S |dkr t| S |dkr(t| S ttj|t| dddd	fS )
zCDispatch standard actions and preserve legacy action compatibility.testlookuprI   rJ   rK   action_not_foundz&The requested action is not supported.rS   action
request_idcodemessagei  )	_legacy_health_legacy_lookup_health_test_connection_lookupr   APP_MANIFESTrS   _request_idcontextr]   r   r   apps/abn_lookup_app/__init__.pyhandle_requesto   s&   rk   c              	   C  sP   d}d}zt | d u}W n ty   d}Y nw ttjdt| d||dddS )NFTrI   )r   vault_readyr   provider_checkedrS   r]   r^   data)_guidr   r   rf   rS   rg   )ri   
configuredrl   r   r   rj   rc      s"   rc   c                 C  s   t | }|st| dS t| pi dpi dpd}zt||d}W n ttfy=   ttj	dt
| dddd	d
f Y S w ttj	dt
| t|dd|ddS )NrJ   payload	probe_abn51824753556guidr%   z'ABR credentials could not be validated.TrS   r]   r^   r_   r`   	retryable  r)   Australian Business Register)r!   r"   rs   rn   )rp   _credential_missingstrgetr   r   r   r   rf   rS   rg   r   bool)ri   rv   rs   r1   r   r   rj   rd      s6   
 	rd   c                 C  sp  | pi  dpi }t| dpd }|s$ttjdt| ddddfS t| }|s/t| dS zt	|t
| d	|d
}W n> ty\   t| |ddd ttjdt| ddddf Y S  ty{   t| |ddd ttjdt| dddddf Y S w | dst| |ddd ttjdt| dddddfS t| |d| dd ttjdt| |dd|dddidS )Nrr   r)    rK   r3   zabn is required.r\     r.   )r.   rv   rejectedr4   )
error_codez#ABN must contain exactly 11 digits.failedr5   z8Australian Business Register is temporarily unavailable.Trw   ry   r6   z;Australian Business Register returned an incomplete record.	succeededstatus)r    )r)   r1   r"   rz   )rS   r]   r^   ro   meta)r}   r|   stripr   rf   rS   rg   rp   r{   r   r~   r   _log_lookupr   r   replace)ri   rr   r)   rv   r1   r   r   rj   re      sr   


re   return
str | Nonec                 C  s<   t | pi dp	i dpd}|sd S t j|tjddS )Ntenant	tenant_idr   r   )r   rS   secret_name)r|   r}   r   
get_secretrf   rS   )ri   r   r   r   rj   rp      s    rp   c                 C  s   t tj|t| ddddfS )Nr$   z3ABR credentials are not configured for this tenant.r\     )r   rf   rS   rg   rh   r   r   rj   r{      s   r{   r|   c                 C  s   t | pi dp	dS )Nr^   r   )r|   r}   ri   r   r   rj   rg      s   rg   outcomeNonec                 K  sh   d dd t|D }ttdft| t| pi dpi dp!d||r+|dd  ndd| d S )	Nr   c                 s  s    | ]	}|  r|V  qd S )N)isdigit).0	characterr   r   rj   	<genexpr>   s    z_log_lookup.<locals>.<genexpr>abn_lookup_resultr   r   )r^   r   r   
abn_suffix)joinr|   r   LOGGERrg   r}   )ri   r)   r   fields
normalizedr   r   rj   r      s   
r   c                 C  s"   dt j| pi dpi ddS )Nokr   r   )r   appr   )rf   rS   r}   r   r   r   rj   ra     s   ra   c                 C  s   | pi  dpi }t| dpd }|sdtjdddfS t| }|s.dtjddd	fS t||d
}dtjd| dp>|t| d|ddS )z:Temporary v0 response retained until wp_invoices migrates.rr   r)   r   errorzMissing abn)r   r   r`   r   z ABR credential is not configuredr   ru   r   rZ   tax_id
abn_exists)r)   validro   )r   r   r]   result)r}   r|   r   rf   rS   rp   r   r~   )ri   rr   r)   rv   ro   r   r   rj   rb     s$   rb   N)r   r   )r   r|   )r)   r|   r   r|   r   r   )'__doc__
__future__r   requestsr   (apps.abn_lookup_app.abn_lookup_v1.modelsr   )apps.abn_lookup_app.abn_lookup_v1.servicer   apps.abn_lookup_app.corer   bridge_platform.apps.contractsr   r   r	   r
   r   bridge_platform.apps.envelopesr   r   'bridge_platform.logging.platform_loggerr   r   "bridge_platform.secrets.encryptionr   'bridge_platform.secrets.secrets_managerr   r   rf   rk   rc   rd   re   rp   r{   rg   r   ra   rb   r   r   r   rj   <module>   s    >U
3

