o
    h                     @  s:   d Z ddlmZ ddlZddlmZmZ 	ddddZdS )zAzure helpers.    )annotationsN)AnyOptional   resourcestr	client_idOptional[str]timeoutfloatreturndict[str, Any]c              
   C  s.  ddl m}m} d}|d7 }|d|  7 }|r|d| 7 }ddd	}|||d
}z"|||d}|j}	| d}
W d    n1 sEw   Y  W n ty` } z	d| }t|d d }~ww |	dkrkd}t|zt	|
}W n ty~   tdd w dD ]}|
|sd}|||
f }t|q|S )Nr   )Requesturlopenz5http://169.254.169.254/metadata/identity/oauth2/tokenz?api-version=2018-02-01z
&resource=z&client_id=truezapplication/json)ZMetadataAccept)headers)r
   utf8z'Failed to acquire IMDS access token: %s   z$Failed to acquire IMDS access token.z*Azure IMDS response must be in JSON format)Zaccess_tokenZ
expires_inz0Azure IMDS response must contain %s, but was %s.)urllib.requestr   r   statusreaddecode	Exception
ValueErrorjsonloadsget)r   r   r
   r   r   urlr   requestresponser   bodyemsgdatakey r&   I/var/www/html/venv/lib/python3.10/site-packages/pymongo/_azure_helpers.py_get_azure_response   sD   



r(   )Nr   )r   r   r   r	   r
   r   r   r   )__doc__
__future__r   r   typingr   r   r(   r&   r&   r&   r'   <module>   s   