o
    ÔèFhƒ  ã                   @   sp   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
 d dlmZ d dlmZ dZG d	d
„ d
e
ƒZdS )é    )Úpartial)Ú	urlencode)ÚAdapterHTTPError)ÚGeocoderQuotaExceeded)ÚDEFAULT_SENTINELÚGeocoder)ÚLocation)Úlogger)ÚLiveAddressc                       s^   e Zd ZdZdZeededdœ‡ fdd„
Zdedd	œd
d„Zdd„ Zddd„Z	dd„ Z
‡  ZS )r
   z”Geocoder using the LiveAddress API provided by SmartyStreets.

    Documentation at:
        https://smartystreets.com/docs/cloud/us-street-api
    z/street-addressN)ÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryc          	         s@   t ƒ jd|||||d� || _|| _d}d| j|| jf | _dS )aÝ  

        :param str auth_id: Valid `Auth ID` from SmartyStreets.

        :param str auth_token: Valid `Auth Token` from SmartyStreets.

        :param int timeout:
            See :attr:`geopy.geocoders.options.default_timeout`.

        :param dict proxies:
            See :attr:`geopy.geocoders.options.default_proxies`.

        :param str user_agent:
            See :attr:`geopy.geocoders.options.default_user_agent`.

        :type ssl_context: :class:`ssl.SSLContext`
        :param ssl_context:
            See :attr:`geopy.geocoders.options.default_ssl_context`.

        :param callable adapter_factory:
            See :attr:`geopy.geocoders.options.default_adapter_factory`.

            .. versionadded:: 2.0
        Úhttps)Úschemer   r   r   r   r   zapi.smartystreets.comz	%s://%s%sN)ÚsuperÚ__init__Úauth_idÚ
auth_tokenr   Úgeocode_pathÚapi)	Úselfr   r   r   r   r   r   r   Údomain©Ú	__class__© úV/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/smartystreets.pyr      s   #úzLiveAddress.__init__Té   )Úexactly_oner   Ú
candidatesc                C   sz   d|  krdkst dƒ‚ t dƒ‚| j| j||dœ}dj| jt|ƒd�}t d| jj	|¡ t
| j|d�}| j|||d	�S )
a3  
        Return a location point by address.

        :param str query: The address or query you wish to geocode.

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :param int candidates: An integer between 1 and 10 indicating the max
            number of candidate addresses to return if a valid address
            could be found.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r   é
   z#candidates must be between 1 and 10)zauth-idz
auth-tokenÚstreetr    z{url}?{query})ÚurlÚqueryz%s.geocode: %s)r   )r   )Ú
ValueErrorr   r   Úformatr   r   r	   Údebugr   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r   r$   r   r   r    r#   Úcallbackr   r   r   ÚgeocodeG   s   ÿüzLiveAddress.geocodec                 C   sR   d}t |tƒr%|t|ƒ ¡ v rtt|ƒƒ|‚||jpd ¡ v r't|jƒ|‚d S d S )Nzno active subscriptions foundÚ )Ú
isinstancer   ÚstrÚlowerr   Útext)r   ÚerrorÚsearchr   r   r   Ú_geocoder_exception_handlers   s   
üz'LiveAddress._geocoder_exception_handlerc                    s0   t |ƒsdS |rˆ  |d ¡S ‡ fdd„|D ƒS )z2
        Parse responses as JSON objects.
        Nr   c                    s   g | ]}ˆ   |¡‘qS r   )Ú_format_structured_address)Ú.0Úc©r   r   r   Ú
<listcomp>„   s    z+LiveAddress._parse_json.<locals>.<listcomp>)Úlenr5   )r   Úresponser   r   r8   r   r)   {   s
   zLiveAddress._parse_jsonc                 C   sN   |d   d¡}|d   d¡}td |d |d f¡|r#|r#||f|ƒS d|ƒS )zA
        Pretty-print address and return lat, lon tuple.
        ÚmetadataÚlatitudeÚ	longitudez, Údelivery_line_1Ú	last_lineN)Úgetr   Újoin)r   Úaddressr=   r>   r   r   r   r5   †   s   ýýz&LiveAddress._format_structured_address)T)r(   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r,   r4   r)   r5   Ú__classcell__r   r   r   r   r
      s     ÷5ú,
r
   N)Ú	functoolsr   Úurllib.parser   Úgeopy.adaptersr   Ú	geopy.excr   Úgeopy.geocoders.baser   r   Úgeopy.locationr   Ú
geopy.utilr	   Ú__all__r
   r   r   r   r   Ú<module>   s    