U
    ™j7g]  ã                   @   s„   d Z ddlZddlZddlZddlZddlmZ dZdZdZ	dZ
d	Zd
ZdZdZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )z
Utilities used in handlers.
é    Né   )ÚSDK_VERSIONzhttps://ipinfo.ioz4https://cdn.ipinfo.io/static/images/countries-flags/iè  i   i€Q Ú1é   é   c                 C   s<   dj tjd tdddœ}|r&||–}| r8d  | ¡|d< |S )z(Build headers for request to IPinfo API.z*IPinfoClient/Python{version}/{sdk_version}r   )ÚversionZsdk_versionzapplication/json)z
user-agentÚacceptz	Bearer {}Úauthorization)ÚformatÚsysÚversion_infor   )Zaccess_tokenZcustom_headersÚheaders© r   ú8/tmp/pip-unpacked-wheel-bv8pilsi/ipinfo/handler_utils.pyÚget_headers'   s     ÿür   c                 C   s¬   |  |   d¡¡| d< |   d¡|k| d< t|   d¡p4d d | d< t |  |   d¡¡¡| d< t |  |   d¡¡¡| d< t |  |   d¡¡¡| d	< t|   d
¡ƒ\| d< | d< dS )z2
    Format details given a countries object.
    ZcountryZcountry_nameZisEUÚ z.svgZcountry_flag_urlZcountry_flagZcountry_currencyZ	continentÚlocZlatitudeZ	longitudeN)ÚgetÚCOUNTRY_FLAGS_URLÚcopyÚdeepcopyÚread_coords)ÚdetailsZ	countriesZeu_countriesZcountries_flagsZcountries_currenciesZ
continentsr   r   r   Úformat_details9   s    ÿÿÿÿr   c                 C   sT   d\}}| rt |  d¡ƒnd}t|ƒdkrL|d rL|d rL|d |d  }}||fS )z¦
    Given a location of the form `<lat>,<lon>`, returns the latitude and
    longitude as a tuple.

    Returns None for each tuple item if the form is invalid.
    )NNú,r   r   r   r   )ÚtupleÚsplitÚlen)ÚlocationZlatZlonZcoordsr   r   r   r   U   s
    r   c              	   C   s@   t j t j t¡| ¡} t| dd}| ¡ }W 5 Q R X t |¡S )NÚutf8)Úencoding)	ÚosÚpathÚjoinÚdirnameÚ__file__ÚopenÚreadÚjsonÚloads)Z	json_fileÚfZ	json_datar   r   r   Úread_json_filec   s    r+   c                 C   s   | r
|‚n|S dS )zC
    Either throws `e` if `raise_on_fail` or else returns `v`.
    Nr   )Zraise_on_failÚeÚvr   r   r   Úreturn_or_failk   s    r.   c                 C   s   | › dt › S )zA
    Transforms a user-input key into a versioned cache key.
    ú:)ÚCACHE_KEY_VSN)Úkr   r   r   Ú	cache_keyu   s    r2   )Ú__doc__r(   r!   r   r   r   r   ZAPI_URLr   ZBATCH_MAX_SIZEZCACHE_MAXSIZEZ	CACHE_TTLr0   ZREQUEST_TIMEOUT_DEFAULTZBATCH_REQ_TIMEOUT_DEFAULTr   r   r   r+   r.   r2   r   r   r   r   Ú<module>   s&   
