
    g                         d dl Zd dl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mZmZ d dlmZ d dlmZ d	Z G d
 de      Zy)    N)partial)	urlencode)AdapterHTTPError)GeocoderQueryErrorGeocoderQuotaExceeded)DEFAULT_SENTINELNONE_RESULTGeocoder)Location)logger)Geocodioc                   |     e Zd ZdZh dZdZdZdZdeededdd fd
Z	dd	ed
dZ
d	edddZddZd Zd Z xZS )r   zGeocoder using the Geocod.io API.

    Documentation at:
        https://www.geocod.io/docs/

    Pricing details:
        https://www.geocod.io/pricing/

    .. versionadded:: 2.2
    >   citystatestreetcountrypostal_codezapi.geocod.ioz/v1.6/geocodez/v1.6/reverseN)schemetimeoutproxies
user_agentssl_contextadapter_factorydomainc                R    t         	|   ||||||       || _        |r|| _        yy)aG  
        :param str api_key:
            A valid Geocod.io API key. (https://dash.geocod.io/apikey/create)

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :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`.

        :param str domain: base api domain

            .. versionadded:: 2.4
        )r   r   r   r   r   r   N)super__init__api_keyr   )
selfr   r   r   r   r   r   r   r   	__class__s
            X/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/geocodio.pyr   zGeocodio.__init__'   sB    N 	!#+ 	 	
  DK     T)limitexactly_oner   c                .   t        |t        j                  j                        r2|j	                         D ci c]  \  }}|| j
                  v r|| }}}nd|i}| j                  |d<   |r||d<   |rd|d<   | j                  d| j                  | j                  }dj                  |t        |      f      }	t        j                  d| j                  j                  |	       t!        | j"                  |      }
| j%                  |	|
|	      S c c}}w )
a  
        Return a location point by address.

        :param query: The address, query or a structured query
            you wish to geocode.

            For a structured query, provide a dictionary whose keys
            are one of: `street`, `city`, `state`, `postal_code` or `country`.
        :type query: dict or str

        :param int limit: The maximum number of matches to return. This will be reset
            to 1 if ``exactly_one`` is ``True``.

        :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.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        qr   r#      ://?z%s.geocode: %sr$   r   )
isinstancecollectionsabcMappingitemsstructured_query_paramsr   r   r   geocode_pathjoinr   r   debugr    __name__r   _parse_json_call_geocoder)r   queryr#   r$   r   keyvalparamsapiurlcallbacks              r!   geocodezGeocodio.geocodeZ   s	   B e[__445 ;;= ! C$666 S    5\F LLy#F7OF7O![[$++t7H7HIhhYv./0%t~~'>'>D4++E""3'"BB+s   D)r$   r   r#   c                   | j                  |      | j                  d}|rd}|||d<   | j                  d| j                  | j                  }dj                  |t        |      f      }t        j                  d| j                  j                  |       t        | j                  |      }| j                  |||      S )	a5  Return an address by location point.

        :param str query: The coordinates for which you wish to obtain the
            closest human-readable addresses

        :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 limit: The maximum number of matches to return. This will be reset
            to 1 if ``exactly_one`` is ``True``.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        )r&   r   r'   r#   r(   r)   z%s.reverse: %sr*   r+   )_coerce_point_to_stringr   r   r   reverse_pathr3   r   r   r4   r    r5   r   r6   r7   )	r   r8   r$   r   r#   r;   r<   r=   r>   s	            r!   reversezGeocodio.reverse   s    8 --e4||
 E#F7O![[$++t7H7HIhhYv./0%t~~'>'>D4++E""3'"BBr"   c                     |j                  dg       }|syd }|r ||d         S |D cg c]
  } ||       c}S c c}w )z7Returns location, (latitude, longitude) from json feed.resultsNc                 b    | j                  d      }| d   d   }| d   d   }t        |||f|       S )z4Get the location, lat, lng from a single json place.formatted_addresslocationlatlng)getr   )placerH   latitude	longitudes       r!   parse_placez)Geocodio._parse_json.<locals>.parse_place   sC    yy!45HZ(/Hj)%0IHx&;UCCr"   r   )rK   )r   pager$   placesrO   rL   s         r!   r6   zGeocodio._parse_json   sQ     )R(	D vay))4:;F5K&F;;;s   <c                 j   t        |t              sy|j                  |j                  y|j                  dk(  rG| j	                  |      }d|j                         v rd|j                         v rt        S t        |      ||j                  dk(  r$| j	                  |      }d}||v rt        |      |yy)a  Custom exception handling for invalid queries and exceeded quotas.

        Geocod.io returns a ``422`` status code for invalid queries, which is not mapped
        in :const:`~geopy.geocoders.base.ERROR_CODE_MAP`. The service also returns a
        ``403`` status code for exceeded quotas instead of the ``429`` code mapped in
        :const:`~geopy.geocoders.base.ERROR_CODE_MAP`
        Ni  zcould not geocode addresszpostal code or city requiredi  z>You can't make this request as it is above your daily maximum.)	r,   r   status_codetext_get_error_messagelowerr	   r   r   )r   errorerror_messagequota_exceeded_snippets       r!   _geocoder_exception_handlerz$Geocodio._geocoder_exception_handler   s     %!12$

(:# 33E:M+}/B/B/DD2m6I6I6KK""$]3># 33E:M&A"%6+M:E 7	 $r"   c                     	 t        j                  |j                        j                  d      }|xs |j                  S # t        $ r d}Y w xY w)zVTry to extract an error message from the 'error' property of a JSON response.
        rW   N)jsonloadsrT   rK   
ValueError)r   rW   rX   s      r!   rU   zGeocodio._get_error_message   sK    	! JJuzz266w?M *

*  	! M	!s   .A   AA)T)r5   
__module____qualname____doc__r1   r   r2   rB   r   r   r?   rC   r6   rZ   rU   __classcell__)r    s   @r!   r   r      sr    	 F"L"L   $1!n  7Cz  )CV<&F6+r"   r   )collections.abcr-   r\   	functoolsr   urllib.parser   geopy.adaptersr   	geopy.excr   r   geopy.geocoders.baser   r	   r
   geopy.locationr   
geopy.utilr   __all__r    r"   r!   <module>rm      s6       " + ? H H # 
d+x d+r"   