
    g                     r    d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ dZ G d d	e
      Zy
)    )partial)	urlencode)GeocoderAuthenticationFailureGeocoderQuotaExceededGeocoderServiceError)DEFAULT_SENTINELGeocoder)Location)logger)GeocodeFarmc                   l     e Zd ZdZdZdZ	 deededdd fdZdedd	Zdedd
Z	d Z
d Zd Z xZS )r   zGeocoder using the GeocodeFarm API.

    Documentation at:
        https://www.geocode.farm/geocoding/free-api-documentation/
    z/v3/json/forward/z/v3/json/reverse/N)timeoutproxies
user_agentssl_contextadapter_factoryschemec                    t         	|   ||||||       || _        d}| j                  d|| j                  | _        | j                  d|| j                  | _        y)a(  

        :param str api_key: (optional) The API key required by GeocodeFarm
            to perform geocoding requests.

        :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

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.
        )r   r   r   r   r   r   zwww.geocode.farmz://N)super__init__api_keyr   geocode_pathapireverse_pathreverse_api)
selfr   r   r   r   r   r   r   domain	__class__s
            [/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/geocodefarm.pyr   zGeocodeFarm.__init__   sq    J 	!#+ 	 	
 #;;0A0AB 	  ;;0A0AB 	    T)exactly_oner   c                8   d|i}| j                   r| 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.

        :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``.
        addrkey?z%s.geocode: %sr!   r   )r   joinr   r   r   debugr   __name__r   _parse_json_call_geocoder)r   queryr!   r   paramsurlcallbacks          r   geocodezGeocodeFarm.geocodeP   s    $ E
 << LLF5Mhh)F"345%t~~'>'>D4++E""3'"BBr    c                   	 | j                  |      j                  d      \  }}||d}| j                  r| j                  |d<   dj	                  | j
                  t        |      f      }t        j                  d| j                  j                  |       t        | j                  |      }| j                  |||      S # t        $ r t        d      w xY w)	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 bool exactly_one: Return one result or a list of results, if
            available. GeocodeFarm's API will always return at most one
            result.

        :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``.
        ,z"Must be a coordinate pair or Point)latlonr$   r%   z%s.reverse: %sr&   r'   )_coerce_point_to_stringsplit
ValueErrorr   r(   r   r   r   r)   r   r*   r   r+   r,   )	r   r-   r!   r   r4   r5   r.   r/   r0   s	            r   reversezGeocodeFarm.reversek   s    *	C33E:@@EHC 
 << LLF5Mhh(()F*;<=%t~~'>'>D4++E""3'"BB  	CABB	Cs   #C Cc                    g }|j                  d      D ]  }|j                  di       }|j                  di       }|j                  dd       }|j                  dd       }|j                  dd       }||j                  dd       }||j                  dd       }|r|rt        |      }t        |      }|j                  t        |||f|              |S )	NRESULTSCOORDINATESADDRESSlatitude	longitudeaddress_returnedaddressformatted_address)getfloatappendr
   )	r   resultsplacesresultcoordinatesrA   r>   r?   	placenames	            r   _parse_codezGeocodeFarm._parse_code   s    kk),F **]B7KjjB/G"z48H#T:I$6=I #KK	48	 "JJ':DA	I ?!),	MM(9x.CVLM - r    c                     |y |d   }| j                  |       d|j                  di       j                  dd      v ry | j                  |      }|r|d   S |S )Ngeocoding_results
NO_RESULTSSTATUSstatus r   )_check_for_api_errorsrC   rK   )r   
api_resultr!   rM   rG   s        r   r+   zGeocodeFarm._parse_json   so    &':;""#45,002>BB8RPP!!"34!9Mr    c                     |j                  di       }d|j                  dd      v ry|j                  dd      dk(  }|s<|j                  d      }t        t        d}|j                  |t              } ||      y)	zc
        Raise any exceptions if there were problems reported
        in the api response.
        rO   rN   rP   rQ   NSUCCESSaccess)API_KEY_INVALIDOVER_QUERY_LIMIT)rC   r   r   r   )r   rM   status_resultapi_call_successaccess_erroraccess_error_to_exceptionexception_clss          r   rR   z!GeocodeFarm._check_for_api_errors   s    
 *--h;=,,Xr::(,,Xr:iG(,,X6L#@$9)% 6992M  --  r    )N)r*   
__module____qualname____doc__r   r   r   r   r1   r9   rK   r+   rR   __classcell__)r   s   @r   r   r      sd     'L&L 4
 %$( 4
l -1:J C6 -1:J "CH&.r    r   N)	functoolsr   urllib.parser   	geopy.excr   r   r   geopy.geocoders.baser   r	   geopy.locationr
   
geopy.utilr   __all__r    r    r   <module>rj      s3     " 
 < # 
s.( s.r    