
    g                     n    d dl mZ d dlmZ d dl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
)    )partial)	urlencode)GeocoderServiceError)DEFAULT_SENTINELERROR_CODE_MAPGeocoder)Location)logger)OpenCagec                   l     e Zd ZdZdZddeededd fd
Zdddddedd	Zdded
dZddZ	d Z
 xZS )r   aE  Geocoder using the OpenCageData API.

    Documentation at:
        https://opencagedata.com/api

    .. versionchanged:: 2.2
        Improved error handling by using the default errors map
        (e.g. to raise :class:`.exc.GeocoderQuotaExceeded` instead of
        :class:`.exc.GeocoderQueryError` for HTTP 402 error)
    z/geocode/v1/jsonzapi.opencagedata.comN)domainschemetimeoutproxies
user_agentssl_contextadapter_factoryc                    t         	|   ||||||       || _        |j                  d      | _        | j
                  d| j                  | j                  | _        y)a  

        :param str api_key: The API key required by OpenCageData
            to perform geocoding requests. You can get your key here:
            https://opencagedata.com/

        :param str domain: Currently it is ``'api.opencagedata.com'``, can
            be changed for testing purposes.

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

            .. versionadded:: 2.0
        )r   r   r   r   r   r   /z://N)super__init__api_keystripr   r   api_pathapi)
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/opencage.pyr   zOpenCage.__init__   s]    T 	!#+ 	 	
 ll3'"&++t{{DMMJ    T)boundscountrylanguageannotationsexactly_oner   c                   | j                   |d}|sd|d<   |r| j                  |d      |d<   |r||d<   |sg }t        |t              r|g}|rdj	                  |      |d<   d	j	                  | j
                  t        |      f      }	t        j                  d
| j                  j                  |	       t        | j                  |      }
| j                  |	|
|      S )a  
        Return a location point by address.

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

        :type bounds: list or tuple of 2 items of :class:`geopy.point.Point` or
            ``(latitude, longitude)`` or ``"%(latitude)s, %(longitude)s"``.
        :param bounds: Provides the geocoder with a hint to the region
            that the query resides in. This value will help the geocoder
            but will not restrict the possible results to the supplied
            region. The bounds parameter should be specified as 2
            coordinate points -- corners of a bounding box.
            Example: ``[Point(22, 180), Point(-22, -180)]``.

        :param country: Restricts the results to the specified
            country or countries. The country code is a 2 character code as
            defined by the ISO 3166-1 Alpha 2 standard (e.g. ``fr``).
            Might be a Python list of strings.
        :type country: str or list

        :param str language: an IETF format language code (such as `es`
            for Spanish or pt-BR for Brazilian Portuguese); if this is
            omitted a code of `en` (English) will be assumed by the remote
            service.

        :param bool annotations: Enable
            `annotations <https://opencagedata.com/api#annotations>`_
            data, which can be accessed via :attr:`.Location.raw`.
            Set to False if you don't need it to gain a little performance
            win.

            .. versionadded:: 2.2

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

        keyq   no_annotationsz#%(lon1)s,%(lat1)s,%(lon2)s,%(lat2)sr    r"   ,countrycode?z%s.geocode: %sr$   r   )r   _format_bounding_box
isinstancestrjoinr   r   r
   debugr   __name__r   _parse_json_call_geocoder)r   queryr    r!   r"   r#   r$   r   paramsurlcallbacks              r   geocodezOpenCage.geocodeQ   s    r <<
 '(F#$#88= ?F8!)F:Ggs#iG$'HHW$5F=!hh)F"345%t~~'>'>D4++E""3'"BBr   )r"   r$   r   c                D   | j                   | j                  |      d}|r||d<   dj                  | j                  t	        |      f      }t        j                  d| j                  j                  |       t        | j                  |      }| j                  |||      S )a  
        Return an address by location point.

        :param query: The coordinates for which you wish to obtain the
            closest human-readable addresses.
        :type query: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.

        :param str language: The language in which to return results.

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

        r&   r"   r-   z%s.reverse: %sr.   r/   )r   _coerce_point_to_stringr3   r   r   r
   r4   r   r5   r   r6   r7   )r   r8   r"   r$   r   r9   r:   r;   s           r   reversezOpenCage.reverse   s    @ <<--e4
 !)F:hh)F"345%t~~'>'>D4++E""3'"BBr   c                     |j                  dg       }t        |      s!| j                  |j                  d             yd }|r ||d         S |D cg c]
  } ||       c}S c c}w )z7Returns location, (latitude, longitude) from json feed.resultsstatusNc                 b    | j                  d      }| d   d   }| d   d   }t        |||f|       S )z4Get the location, lat, lng from a single json place.	formattedgeometrylatlng)getr	   )placelocationlatitude	longitudes       r   parse_placez)OpenCage._parse_json.<locals>.parse_place   sB    yy-HZ(/Hj)%0IHx&;UCCr   r   )rH   len_check_status)r   pager$   placesrM   rI   s         r   r6   zOpenCage._parse_json   sl     )R(6{txx12	D vay))4:;F5K&F;;;s   A%c                 f    |d   }|d   }|dk(  ry t        j                  |t              } ||      )Ncodemessage   )r   rH   r   )r   rB   status_coderT   exc_clss        r   rO   zOpenCage._check_status   s>    Vn## $$[2FGgr   )T)r5   
__module____qualname____doc__r   r   r   r<   r?   r6   rO   __classcell__)r   s   @r   r   r      se    	 "H *$$( 5Kv $OCj $)CV<(r   r   N)	functoolsr   urllib.parser   	geopy.excr   geopy.geocoders.baser   r   r   geopy.locationr	   
geopy.utilr
   __all__r    r   r   <module>rd      s-     " * K K # 
\x \r   