
    gC^                         d Z ddlZddlZddlZddlZddlZddlZ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 ddlmZmZmZmZmZmZ ddlmZmZmZmZmZ dd	lmZ 	 ddl Z dd
l!m"Z# dZ$	 ddl'Z'ddl(Z'ddl)Z)dZ* G d de+      Z,d Z- G d dej\                        Z/ G d de/      Z0 G d de/      Z1d Z2 G d de0      Z3 G d de0      Z4 G d de1      Z5 G d de#      Z6y# e%$ r e&Z#dZ$Y w xY w# e%$ r dZ*Y w xY w)a  
Adapters are HTTP client implementations used by geocoders.

Some adapters might support keep-alives, request retries, http2,
persistence of Cookies, response compression and so on.

Adapters should be considered an implementation detail. Most of the time
you wouldn't need to know about their existence unless you want to tune
HTTP client settings.

.. versionadded:: 2.0
   Adapters are currently provided on a `provisional basis`_.

    .. _provisional basis: https://docs.python.org/3/glossary.html#term-provisional-api
    Ntimeout)SSLError)	HTTPError)urlparse)HTTPSHandlerProxyHandlerRequestURLErrorbuild_opener
getproxies)GeocoderParseErrorGeocoderServiceErrorGeocoderTimedOutGeocoderUnavailable
GeopyError)logger)HTTPAdapterTFc                   "     e Zd ZdZ fdZ xZS )AdapterHTTPErrorzAn exception which must be raised by adapters when an HTTP response
    with a non-successful status code has been received.

    Base Geocoder class translates this exception to an instance of
    :class:`geopy.exc.GeocoderServiceError`.

    c                N    || _         || _        || _        t        |   |       y)a;  

        :param str message: Standard exception message.
        :param int status_code: HTTP status code.
        :param dict headers: HTTP response readers. A mapping object
            with lowercased or case-insensitive keys.

            .. versionadded:: 2.2
        :param str text: HTTP body text.
        N)status_codeheaderstextsuper__init__)selfmessager   r   r   	__class__s        N/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/adapters.pyr   zAdapterHTTPError.__init__J   s(     '	!    )__name__
__module____qualname____doc__r   __classcell__r   s   @r    r   r   A   s    " "r!   r   c                 x   	 | d   }|sy|j                         }	 t        |      }|dk  rd}|S # t         $ r Y yw xY w# t        $ rr t        j
                  j                  |      }|t        j                  d|       Y yt        j
                  j                  |      }|t        j                         z
  }Y w xY w)zKReturn Retry-After header value in seconds.

    .. versionadded:: 2.2
    zretry-afterNzInvalid Retry-After header: %sr   )KeyErrorstripint
ValueErroremailutilsparsedate_tzr   warning	mktime_tztime)r   retry_aftersecondsretry_date_tuple
retry_dates        r    get_retry_afterr7   [   s    m, ##%K

+k" {N5    + ;;33K@#NN;[I[[**+;<
tyy{*+s"   / > 	;;A B9 6B98B9c                   b    e Zd ZdZdZd Zej                  d        Zej                  d        Z	y)BaseAdaptera  Base class for an Adapter.

    There are two types of adapters:

    - :class:`.BaseSyncAdapter` -- synchronous adapter,
    - :class:`.BaseAsyncAdapter` -- asynchronous (asyncio) adapter.

    Concrete adapter implementations must extend one of the two
    base adapters above.

    See :attr:`geopy.geocoders.options.default_adapter_factory`
    for details on how to specify an adapter to be used by geocoders.

    Tc                     y)ap  Initialize adapter.

        :param dict proxies: An urllib-style proxies dict, e.g.
            ``{"http": "192.0.2.0:8080", "https": "192.0.2.0:8080"}``,
            ``{"https": "http://user:passw0rd@192.0.2.0:8080""}``.
            See :attr:`geopy.geocoders.options.default_proxies` (note
            that Adapters always receive a dict: the string proxy
            is transformed to dict in the base
            :class:`geopy.geocoders.base.Geocoder` class.).

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

        N )r   proxiesssl_contexts      r    r   zBaseAdapter.__init__       r!   c                     y)a  Same as ``get_text`` except that the response is expected
        to be a valid JSON. The value returned is the parsed JSON.

        :class:`geopy.exc.GeocoderParseError` must be raised if
        the response cannot be parsed.

        :param str url: The target URL.

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

        :param dict headers: A dict with custom HTTP request headers.
        Nr;   r   urlr   r   s       r    get_jsonzBaseAdapter.get_json   r>   r!   c                     y)aj  Make a GET request and return the response as string.

        This method should not raise any exceptions other than these:

        - :class:`geopy.adapters.AdapterHTTPError` should be raised if the response
          was successfully retrieved but the status code was non-successful.
        - :class:`geopy.exc.GeocoderTimedOut` should be raised when the request
          times out.
        - :class:`geopy.exc.GeocoderUnavailable` should be raised when the target
          host is unreachable.
        - :class:`geopy.exc.GeocoderServiceError` is the least specific error
          in the exceptions hierarchy and should be raised in any other cases.

        :param str url: The target URL.

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

        :param dict headers: A dict with custom HTTP request headers.
        Nr;   r@   s       r    get_textzBaseAdapter.get_text   r>   r!   N)
r"   r#   r$   r%   is_availabler   abcabstractmethodrB   rD   r;   r!   r    r9   r9      sG    " L" 	  	 r!   r9   c                       e Zd ZdZd Zd Zy)BaseSyncAdapterz)Base class for synchronous adapters.
    c                     | S Nr;   r   s    r    	__enter__zBaseSyncAdapter.__enter__       r!   c                      y rK   r;   r   exc_typeexc_valexc_tbs       r    __exit__zBaseSyncAdapter.__exit__   s    r!   N)r"   r#   r$   r%   rM   rT   r;   r!   r    rI   rI      s    r!   rI   c                       e Zd ZdZd Zd Zy)BaseAsyncAdapterzYBase class for asynchronous adapters.

    See also: :ref:`Async Mode <async_mode>`.
    c                    K   | S wrK   r;   rL   s    r    
__aenter__zBaseAsyncAdapter.__aenter__           c                    K   y wrK   r;   rP   s       r    	__aexit__zBaseAsyncAdapter.__aexit__   s	     s   N)r"   r#   r$   r%   rX   r\   r;   r!   r    rV   rV      s    
r!   rV   c                 z    | 
t               } | si S i }| j                         D ]  \  }}|r	d|vrd|z  }|||<    |S )zNormalize user-supplied `proxies`:

    - For `None` -- retrieve System proxies using
      :func:`urllib.request.getproxies`
    - Add `http://` scheme to proxy urls if missing.
    z://z	http://%s)r   items)r<   
normalizedschemerA   s       r    _normalize_proxiesra      sY     ,	J}}5# #C 
6 ' r!   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )URLLibAdaptera~  The fallback adapter which uses urllib from the Python standard
    library, see :func:`urllib.request.urlopen`.

    urllib doesn't support keep-alives, request retries,
    doesn't persist Cookies and is HTTP/1.1 only.

    urllib was the only available option
    for making requests in geopy 1.x, so this adapter behaves the same
    as geopy 1.x in terms of HTTP requests.
    c                    t        |      }t        | 	  ||       t        t	        |      t        |            }|j                  | _        y )Nr<   r=   )context)ra   r   r   r   r   r	   openurlopen)r   r<   r=   openerr   s       r    r   zURLLibAdapter.__init__  sH    $W-kB -!
 {{r!   c                    | j                  |||      }	 t        j                  |      S # t        $ r t	        d|z        w xY wNr   r   z,Could not deserialize using deserializer:
%s)rD   jsonloadsr,   r   )r   rA   r   r   r   s        r    rB   zURLLibAdapter.get_json  sN    }}S'7}C	::d## 	$?$F 	s	   + Ac                   t        ||      }	 | j                  ||      }| j                  |      }|j                         }|dk\  rM|j                  j                         D 	ci c]  \  }}	|j                         |	 }
}}	t        d|z  ||
|      |S c c}	}w # t        $ r2}t        |j                        rt        |j                  d         n
t        |      }t        |t              rr|j                         }|j                  j                         D 	ci c]  \  }}	|j                         |	 nc c}	}w }
}}	| j                  |      }t        |||
|      t        |t              rd|v rt!        d      d	|v rEt#        d
      t        |t$              rt!        d      t        |t&              rd|v rt!        d      t)        |      d }~ww xY w)N)rA   r   r     Non-successful status code %sr   r   r   r   	timed outService timed outunreachablezService not available)r
   rh   _decode_pagegetcoder   r^   lowerr   	Exceptionlenargsstr
isinstancer   _read_http_error_bodyr   r   r   SocketTimeoutr   r   )r   rA   r   r   reqpager   r   namevalueresponse_headerserrorr   codebodys                  r    rD   zURLLibAdapter.get_text'  s   #w/)	<<W<5D8 $$T*D,,.Kc! (,||'9'9';$';e JJL%''; ! $ '3kA +,	  $=  	0,/

Oc%**Q-(UG%+}} (-}}':':'<$'<e JJL%''<$  $ 11%8& $,	  E8,')*+>??"g--.EFFE=1&':;;E8,')*+>??&w//3	0s+   B %B
G'A5GD87BGGc                 r    	 | j                  |      S # t        $ r t        j                  dd       Y y w xY w)Nz7Unable to fetch body for a non-successful HTTP responseT)exc_info)rv   ry   r   debug)r   r   s     r    r~   z#URLLibAdapter._read_http_error_bodyV  s>    	$$U++ 	LLITX 		s     66c                     |j                   j                         xs d}	 |j                         }	 t        ||      S # t        $ r t	        d      w xY w# t        $ r t        d      w xY w)Nzutf-8zUnable to read the response)encodingz#Unable to decode the response bytes)r   get_content_charsetreadry   r   r|   r,   r   )r   r   r   
body_bytess       r    rv   zURLLibAdapter._decode_page_  sz    <<335@	FJ	LzH55	  	F&'DEE	F
  	L$%JKK	Ls   > A AA+)
r"   r#   r$   r%   r   rB   rD   r~   rv   r&   r'   s   @r    rc   rc     s#    	#"-^
Lr!   rc   c                   V     e Zd ZdZeZddddd fd
Zd Zd Zd	 Z	d
 Z
d Zd Z xZS )RequestsAdaptera  The adapter which uses `requests`_ library.

    .. _requests: https://requests.readthedocs.io

    `requests` supports keep-alives, retries, persists Cookies,
    allows response compression and uses HTTP/1.1 [currently].

    ``requests`` package must be installed in order to use this adapter.

    The requests' ``trust_env`` value is set to false, meaning that
    environment doesn't affect the requests' configuration.
    The ``ssl_context`` and ``proxies`` settings can be used for configuration.

    .. versionchanged:: 2.4
        This adapter used to use the `certifi` CA bundle by default,
        if an ``ssl_context`` wasn't provided explicitly. This has been
        changed to use the system CA store by default.
    
      Fpool_connectionspool_maxsizemax_retries
pool_blockc          
         t         st        d      t        |      }|t        j                         }t
        |   ||       t        j                         | _	        d| j                  _
        || j                  _        | j                  j                  dt        ||||             | j                  j                  dt        |||||             y )Nz`requests` must be installed in order to use RequestsAdapter. If you have installed geopy via pip, you may use this command to install requests: `pip install "geopy[requests]"`.re   Fzhttp://r   zhttps://)r=   r   r   r   r   )requests_availableImportErrorra   sslcreate_default_contextr   r   requestsSessionsession	trust_envr<   mountRequestsHTTPAdapter!RequestsHTTPWithSSLContextAdapter)r   r<   r=   r   r   r   r   r   s          r    r   zRequestsAdapter.__init__  s     "3  %W- 446KkB'')!&&!1)'%		
 	-'!1)'%		
r!   c                     | S rK   r;   rL   s    r    rM   zRequestsAdapter.__enter__  rN   r!   c                 8    | j                   j                          y rK   r   closerP   s       r    rT   zRequestsAdapter.__exit__  s    r!   c                 d    t        | dd       }|	 |j                          y y # t        $ r Y y w xY w)Nr   )getattrr   	TypeErrorr   r   s     r    __del__zRequestsAdapter.__del__  sA     $	40	    s   # 	//c                B    | j                  |||      }|j                  S Nrl   )_requestr   r   rA   r   r   resps        r    rD   zRequestsAdapter.get_text  s     }}S'7}Cyyr!   c                    | j                  |||      }	 |j                         S # t        $ r t        d|j                  z        w xY wrk   )r   rm   r,   r   r   r   s        r    rB   zRequestsAdapter.get_json  sO    }}S'7}C	99; 	$?$))K 	s	   & "Ac                f   	 | j                   j                  |||      }|j                  dk\  r:t        d|j                  z  |j                  |j                  |j
                        |S # t        $ r}t        |      }t        |t              rt        d      t        |t              rd|v rrt        d      t        |t        j                        r(d|j                         v rt        |      t!        |      t        |t        j"                        rt        d      t        |      d }~ww xY w)Nrl   rp   rq   rr   rt   rs   unauthorized)r   getr   r   r   r   ry   r|   r}   r   r   r   r   ConnectionErrorrx   r   r   Timeout)r   rA   r   r   r   r   r   s          r    r   zRequestsAdapter._request  s   	<<##C'#JD" 3&&3d6F6FF $ 0 0 LL	  1  	0%jG%/&':;;E8,')*+>??E8#;#;<!W]]_4.w77-g66E8#3#34&':;;&w//	0s   A+ +	D04B7D++D0)r"   r#   r$   r%   r   rE   r   rM   rT   r   rD   rB   r   r&   r'   s   @r    r   r   l  sA    & &L 9
v r!   r   c                   ~     e Zd ZdZeZ fdZed        Zd Z	d Z
d Zd Zd Zd	 Zej                   d
        Z xZS )AioHTTPAdaptera  The adapter which uses `aiohttp`_ library.

    .. _aiohttp: https://docs.aiohttp.org/

    `aiohttp` supports keep-alives, persists Cookies, allows response
    compression and uses HTTP/1.1 [currently].

    ``aiohttp`` package must be installed in order to use this adapter.
    c                |    t         st        d      t        |      }t        |   ||       || _        || _        y )Nz`aiohttp` must be installed in order to use AioHTTPAdapter. If you have installed geopy via pip, you may use this command to install aiohttp: `pip install "geopy[aiohttp]"`.re   )aiohttp_availabler   ra   r   r   r<   r=   )r   r<   r=   r   s      r    r   zAioHTTPAdapter.__init__  sE     2  %W-kB&r!   c                     | j                   j                  d      }|&t        j                  dd      }|| j                   d<   |S )Nr   F)r   raise_for_status)__dict__r   aiohttpClientSessionr   s     r    r   zAioHTTPAdapter.session  sH    
 --##I.?++!&G (/DMM)$r!   c                    K   | S wrK   r;   rL   s    r    rX   zAioHTTPAdapter.__aenter__'  rY   rZ   c                 T   K   | j                   j                          d {    y 7 wrK   r   rP   s       r    r\   zAioHTTPAdapter.__aexit__*  s     
 ll  """s   (&(c                b  K   | j                         5  | j                  |||      4 d {   }| j                  |       d {    |j                          d {   cd d d       d {    cd d d        S 7 O7 87 "7 # 1 d {  7  sw Y   nxY w	 d d d        y # 1 sw Y   y xY wwr   )_normalize_exceptionsr   _raise_for_statusr   r   s        r    rD   zAioHTTPAdapter.get_text1  s     '')}}S'7}KKt,,T222!YY[( LKK *)K2( LKKKK *))s   B/B#A<B#BA>BB B!B#-B.B#2
B/<B#>B BB#B	
BB	B#	B/#B,(B/c          	        K   | j                         5  | j                  |||      4 d {   }| j                  |       d {    	 	 |j                          d {   cd d d       d {    cd d d        S 7 Q7 :7 "7 # t        j
                  j                  $ rJ t        j                  |j                          d {  7        cY cd d d       d {  7   cd d d        S w xY w# t        $ r& t        d|j                          d {  7  z        w xY w# 1 d {  7  sw Y   nxY w	 d d d        y # 1 sw Y   y xY wwrk   )r   r   r   rm   r   client_exceptionsContentTypeErrorrn   r   r,   r   r   s        r    rB   zAioHTTPAdapter.get_json7  s    '')}}S'7}KKt,,T222=%)YY[0	 LKK *)K2  1	 L
 #44EE =  $zz		*;*;<< LKK *)= " ,G!%,,.  LKKK *))s   EEA>ED"B D"BB B#E/B0E4
E>E D"BE?C-C	C-C0ECE"
E,C--C00"DDDD""D4	(D+)D4	0E8	EE
Ec                    K   |j                   dk\  rFt        d|j                   z  |j                   |j                  |j                          d {         y 7 
w)Nrp   rq   rr   )statusr   r   r   )r   r   s     r    r   z AioHTTPAdapter._raise_for_statusH  sO     ;;#"/$++= KK99;&	  
 's   AAAAc                   | j                   r?t        |      j                  }| j                   j                  |j	                               }nd }t        j                  |d      }| j                  j                  ||||| j                        S )NT)encoded)r   r   proxyr   )	r<   r   r`   r   rx   yarlURLr   r=   )r   rA   r   r   r`   r   s         r    r   zAioHTTPAdapter._requestQ  sv    <<c]))FLL$$V\\^4EE hhsD)||'DDTDT   
 	
r!   c              #   ^  K   	 d  y # t         t        t        f$ r  t        $ r}t	        |      }t        |t        j                        rt        d      t        |t              rd|v r0t        d      t        |t        j                        rt        |      t        |      d }~ww xY ww)Nrt   rs   )r   r   AssertionErrorry   r|   r}   asyncioTimeoutErrorr   r   r   ClientConnectionErrorr   r   )r   r   r   s      r    r   z$AioHTTPAdapter._normalize_exceptionsa  s     	0,n= 	 		0%jG%!5!56&':;;E8,')*+>??E7#@#@A)'22&w//		0s%   B-	 B-B*A?B%%B**B-)r"   r#   r$   r%   r   rE   r   propertyr   rX   r\   rD   rB   r   r   
contextlibcontextmanagerr   r&   r'   s   @r    r   r     s`     %L'  #)"
  0 0r!   r   c                   H     e Zd Zdd fd
Z fdZ fdZd Z fdZ xZS )r   N)r=   c                @    || _         d| _        t        |   di | y )NFr;   )/_RequestsHTTPWithSSLContextAdapter__ssl_context2_RequestsHTTPWithSSLContextAdapter__urllib3_warnedr   r   )r   r=   kwargsr   s      r    r   z*RequestsHTTPWithSSLContextAdapter.__init__u  s#    ( %"6"r!   c                 x    | j                   | j                   |d<   | j                          t        |   |i |S Nr=   )r   7_RequestsHTTPWithSSLContextAdapter__warn_if_old_urllib3r   init_poolmanager)r   r{   r   r   s      r    r   z2RequestsHTTPWithSSLContextAdapter.init_poolmanagerz  sB    ) %)$6$6F=!&&(w'888r!   c                 z    | j                   | j                   |d<   | j                          t        |   |fi |S r   )r   r   r   proxy_manager_for)r   r   proxy_kwargsr   s      r    r   z3RequestsHTTPWithSSLContextAdapter.proxy_manager_for  s@    )*.*<*<L'&&(w(?,??r!   c                    | j                   ry d| _         	 dd lmc m} d t        fd|j                  j                  d      D              }|dk  rt        j                  dt               y y # t        $ r dd l}Y `w xY w)NTr   c                 8    	 t        |       S # t        $ r Y yw xY w)Nr   )r+   r,   )ss    r    
silent_intzKRequestsHTTPWithSSLContextAdapter.__warn_if_old_urllib3.<locals>.silent_int  s"    1v s   
 	c              3   .   K   | ]  } |        y wrK   r;   ).0vr   s     r    	<genexpr>zJRequestsHTTPWithSSLContextAdapter.__warn_if_old_urllib3.<locals>.<genexpr>  s     N/M!
1/Ms   .)      r   zurllib3 prior to 1.24.2 is known to have a bug with custom ssl contexts: it attempts to load system certificates to them. Please consider upgrading `requests` and `urllib3` packages. See https://github.com/urllib3/urllib3/pull/1566)r   requests.packages.urllib3packagesurllib3r   tuple__version__splitwarningswarnUserWarning)r   r   versionr   s      @r    __warn_if_old_urllib3z7RequestsHTTPWithSSLContextAdapter.__warn_if_old_urllib3  s       $	77	 Nw/B/B/H/H/MNNZMMM     		s   	A1 1B Bc                 |    t         |   ||||       | j                  d |_        d |_        d |_        d |_        y y rK   )r   cert_verifyr   ca_certsca_cert_dir	cert_filekey_file)r   connrA   verifycertr   s        r    r   z-RequestsHTTPWithSSLContextAdapter.cert_verify  sD    D#vt4) DM#D!DN DM *r!   )	r"   r#   r$   r   r   r   r   r   r&   r'   s   @r    r   r   t  s%    &* #
9@8! !r!   r   )7r%   rF   r   r   r-   rm   r   r2   r   socketr   r   r   urllib.errorr   urllib.parser   urllib.requestr   r	   r
   r   r   r   	geopy.excr   r   r   r   r   
geopy.utilr   r   requests.adaptersr   r   r   r   objectr   aiohttp.client_exceptionsr   r   IOErrorr   r7   ABCr9   rI   rV   ra   rc   r   r   r   r;   r!   r    <module>r     s!        
   +  " !   D
$
"w "4$NJ#'' JZk 
{ 
4hLO hLVOo Odr0% r0l7!(; 7!C     s$    C$ -C3 $	C0/C03C=<C=