o
    ÙKÕi[  ã                	   @   sö   d dl Z d dlmZ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 dZG d	d
„ d
eƒZG dd„ deeƒZdedefdd„Zdede	defdd„Zeddeed�Zdddeejdfejdfejdfejdfgddd œZdS )!é    N)Ú
exceptionsÚsecret_manager)ÚAPIEndpoint)ÚAdditionalInfo)ÚUAConfig)ÚUAContractClient)Ú
DataObjectÚFieldÚIntDataValueÚStringDataValueéF   c                   @   s*   e Zd Zededd�gZdefdd„ZdS )ÚMagicAttachWaitOptionsÚmagic_tokenz,The Token provided by the initiate endpoint.©Údocc                 C   s
   || _ d S ©N©r   )Úselfr   © r   úI/usr/lib/python3/dist-packages/uaclient/api/u/pro/attach/magic/wait/v1.pyÚ__init__   s   
zMagicAttachWaitOptions.__init__N)Ú__name__Ú
__module__Ú__qualname__r	   r   ÚfieldsÚstrr   r   r   r   r   r      s    ýÿr   c                   @   sz   e Zd Zededd�ededd�ededd�eded	d�ed
edd�ededd�gZdedededed
edefdd„Z	dS )ÚMagicAttachWaitResultÚ	user_codezACode the user will see in the UI when confirming the Magic Attachr   Útokenz1The same Magic Token that was sent as an argumentÚexpiresz0Timestamp of the Magic Attach process expirationÚ
expires_inz/Seconds before the Magic Attach process expiresÚcontract_idz2ID of the contract the machine will be attached toÚcontract_tokenz(The contract Token to attach the machinec                 C   s(   || _ || _|| _|| _|| _|| _d S r   ©r   r   r   r    r!   r"   )r   r   r   r   r    r!   r"   r   r   r   r   D   s   	
zMagicAttachWaitResult.__init__N)
r   r   r   r	   r   r
   r   r   Úintr   r   r   r   r   r      sZ    üýýýýýã$þýüûúùr   ÚoptionsÚreturnc                 C   s   t | tƒ ƒS r   )Ú_waitr   )r%   r   r   r   ÚwaitU   s   r(   Úcfgc              
   C   s  t |ƒ}d}d}d}tj | j¡ |tk rŠd}z|j| jd�}d}W n5 tjy/   Y t ¡ ‚ tj	y:   d}Y n tj
yW } z|dk rK|d7 }n|‚W Y d}~nd}~ww |ry| d¡duryt|d	 |d
 |d t|d ƒ|d |d d�S |r}d}t |¡ |d7 }|tk st ¡ ‚)zi
    This endpoint polls the contracts service waiting for the user to confirm
    the Magic Attach.
    r   é
   Nr   é   é   é   ÚcontractTokenÚuserCoder   r   Ú	expiresInÚ
contractIDr#   )r   r   ÚsecretsÚ
add_secretr   ÚMAXIMUM_ATTEMPTSÚget_magic_attach_token_infor   ÚMagicAttachTokenErrorÚMagicAttachUnavailableÚConnectivityErrorÚgetr   r$   ÚtimeÚsleep)r%   r)   ÚcontractÚnum_attemptsÚnum_connection_errorsÚ	wait_timeÚ	wait_respÚer   r   r   r'   [   sN   ÿ æ
þ€þ
ú
Ù)r'   Úv1ÚMagicAttachWait)ÚversionÚnameÚfnÚoptions_clsz27.11Tz¨
from uaclient.api.u.pro.attach.magic.wait.v1 import MagicAttachWaitOptions, wait

options = MagicAttachWaitOptions(magic_token="<magic_token>")
result = wait(options)
zARaised if it is not possible to connect to the contracts service.zLRaised if there is an unexpected error in the contracts service interaction.z-Raised when an invalid/expired Token is sent.zHRaised if the Magic Attach service is busy or unavailable at the moment.zCpro api u.pro.attach.magic.wait.v1 --args magic_token=<magic_token>zÑ
{
    "user_code":"<UI_code>",
    "token":"<magic_token>",
    "expires": "<yyyy-MM-dd>T<HH:mm:ss>.<TZ>",
    "expires_in": 500,
    "contract_id": "<Contract-ID>",
    "contract_token": "<attach_token>",
}
)Úintroduced_inÚrequires_networkÚexample_pythonÚresult_classr   Úexample_cliÚexample_json)r:   Úuaclientr   r   Úuaclient.api.apir   Úuaclient.api.data_typesr   Úuaclient.configr   Úuaclient.contractr   Úuaclient.data_typesr   r	   r
   r   r4   r   r   r(   r'   Úendpointr8   ÚContractAPIErrorr6   r7   Ú_docr   r   r   r   Ú<module>   s`    6ÿ
þÿÿ
þ:üýýþýí
Ú