U
    [eC                     @   s  d Z ddlZddlZddlmZmZmZmZmZm	Z	m
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mZmZmZmZmZmZ ddlmZmZmZ ddlmZ dej dejj  Z!dej dej"#  Z$G d	d
 d
Z%G dd de%Z&G dd de%Z'dS )aV  
============================
WebServices Client API
============================

This class provides a client API for all the GeoIP2 web services. The web
services are Country, City Plus, and Insights. Each service returns a
different set of data about an IP address, with Country returning the least
data and Insights the most.

Each service is represented by a different model class, and these model
classes in turn contain multiple record classes. The record classes have
attributes which contain data about the IP address.

If the service does not return a particular piece of data for an IP address,
the associated attribute is not populated.

The service may not return any information for an entire record, in which
case all of the attributes for that record class will be empty.

SSL
---

Requests to the web service are always made with SSL.

    N)AnyDictcastListOptionalTypeUnion)AddressNotFoundErrorAuthenticationErrorGeoIP2Error	HTTPErrorInvalidRequestErrorOutOfQueriesErrorPermissionRequiredError)CityCountryInsights)	IPAddresszGeoIP2-Python-Client/ c                   @   s  e Zd ZU eed< eed< eed< ee ed< eed< eeeeee  edddd	Z	ee
ed
ddZeeeedddZeeeeedddZeeeeedddZeeeeeeeeeeef dddZeeeee edddZeeeee edddZdS )
BaseClient_account_idZ_host_license_key_locales_timeoutN)
account_idlicense_keyhostlocalestimeoutreturnc                 C   sJ   |dkrdg}|| _ t|tr"|nt|| _|| _d| d| _|| _dS )zConstruct a Client.Nenzhttps://z/geoip/v2.1)r   
isinstancebytesstrr   r   	_base_urir   )selfr   r   r   r   r    r&   S/var/www/html/services/stratfitenv/lib/python3.8/site-packages/geoip2/webservice.py__init__C   s    
zBaseClient.__init__)path
ip_addressr   c                 C   s(   |dkrt |}d| j|t|gS )Nme/)	ipaddressr*   joinr$   r#   )r%   r)   r*   r&   r&   r'   _uriZ   s    
zBaseClient._uri)bodyurir   c              
   C   sV   zt | W S  tk
rP } z&td| dd|j d||W 5 d }~X Y nX d S )NzReceived a 200 response for z, but could not decode the response as JSON: ,    )jsonloads
ValueErrorr   r.   args)r0   r1   exr&   r&   r'   _handle_success_   s    

zBaseClient._handle_success)statuscontent_typer0   r1   r   c                 C   s\   d|  krdk r(n n|  ||||S d|  kr<dk rNn n| |||S | |||S )Ni  i  iX  )_exception_for_4xx_status_exception_for_5xx_status_exception_for_non_200_status)r%   r:   r;   r0   r1   r&   r&   r'   _exception_for_errorl   s
    zBaseClient._exception_for_errorc              
   C   s   |s t d| d| d|||S |ddkrNt d| d| d| |||S zt|}W nR tk
r } z4t d| d| dd	 d
|j ||| W Y S d }~X Y n<X d|krd|kr| |d|d||S t d|||S d S )NzReceived a z error for z with no body.r4   z for z with the following body: z but it did not include zthe expected JSON body: r2   codeerrorzAResponse contains JSON but it does not specify code or error keys)	r   findr4   r5   r6   r.   r7    _exception_for_web_service_errorget)r%   r:   r;   r0   r1   decoded_bodyr8   r&   r&   r'   r<   u   sP    
	   z$BaseClient._exception_for_4xx_status)messagerA   r:   r1   r   c                 C   sN   |dkrt | S |dkr t| S |dkr0t| S |dkr@t| S t| |||S )N)ZIP_ADDRESS_NOT_FOUNDZIP_ADDRESS_RESERVED)ZACCOUNT_ID_REQUIREDZACCOUNT_ID_UNKNOWNZAUTHORIZATION_INVALIDZLICENSE_KEY_REQUIREDZUSER_ID_REQUIREDZUSER_ID_UNKNOWN)ZINSUFFICIENT_FUNDSZOUT_OF_QUERIESZPERMISSION_REQUIRED)r	   r
   r   r   r   )rG   rA   r:   r1   r&   r&   r'   rD      s    
z+BaseClient._exception_for_web_service_error)r:   r1   r0   r   c                 C   s   t d|  d| | ||S )NzReceived a server error () for r   r:   r1   r0   r&   r&   r'   r=      s    z$BaseClient._exception_for_5xx_statusc                 C   s   t d|  d| | ||S )Nz(Received a very surprising HTTP status (rH   rI   rJ   r&   r&   r'   r>      s    z(BaseClient._exception_for_non_200_status)__name__
__module____qualname__r#   __annotations__r   floatintr   r(   r   r/   staticmethodr   r9   r   r?   r<   r   r
   r	   r   r   r   rD   r   r=   r>   r&   r&   r&   r'   r   <   sj   

   
   (     
  r   c                	       s   e Zd ZU dZejed< ee ed< de	eeee
e  eee dd fdd	Zd eedddZd!eedddZd"eedddZejdddZeeee ee ee f eeeeef dddZdd Zd dddZdddddddZ  ZS )#AsyncClienta[  An async GeoIP2 client.

    It accepts the following required arguments:

    :param account_id: Your MaxMind account ID.
    :param license_key: Your MaxMind license key.

    Go to https://www.maxmind.com/en/my_license_key to see your MaxMind
    account ID and license key.

    The following keyword arguments are also accepted:

    :param host: The hostname to make a request against. This defaults to
      "geoip.maxmind.com". To use the GeoLite2 web service instead of the
      GeoIP2 web service, set this to "geolite.info".
    :param locales: This is list of locale codes. This argument will be
      passed on to record classes to use when their name properties are
      called. The default value is ['en'].

      The order of the locales is significant. When a record class has
      multiple names (country, city, etc.), its name property will return
      the name in the first locale that has one.

      Note that the only locale which is always present in the GeoIP2
      data is "en". If you do not include this locale, the name property
      may end up returning None even when the record has an English name.

      Currently, the valid locale codes are:

      * de -- German
      * en -- English names may still include accented characters if that is
        the accepted spelling in English. In other words, English does not
        mean ASCII.
      * es -- Spanish
      * fr -- French
      * ja -- Japanese
      * pt-BR -- Brazilian Portuguese
      * ru -- Russian
      * zh-CN -- Simplified Chinese.
    :param timeout: The timeout in seconds to use when waiting on the request.
      This sets both the connect timeout and the read timeout. The default is
      60.
    :param proxy: The URL of an HTTP proxy to use. It may optionally include
      a basic auth username and password, e.g.,
      ``http://username:password@host:port``.

    _existing_session_proxygeoip.maxmind.comN<   r   r   r   r   r   proxyr   c                    s   t  ||||| || _d S N)superr(   rT   r%   r   r   r   r   r   rX   	__class__r&   r'   r(     s    	zAsyncClient.__init__r+   r*   r   c                    s   t t| dtjj|I dH S )  Call City Plus endpoint with the specified IP.

        :param ip_address: IPv4 or IPv6 address as a string. If no
           address is provided, the address that the web service is
           called from will be used.

        :returns: :py:class:`geoip2.models.City` object

        cityNr   r   _response_forgeoip2modelsr%   r*   r&   r&   r'   r`     s    
 zAsyncClient.cityc                    s   t t| dtjj|I dH S )(  Call the GeoIP2 Country endpoint with the specified IP.

        :param ip_address: IPv4 or IPv6 address as a string. If no address
          is provided, the address that the web service is called from will
          be used.

        :returns: :py:class:`geoip2.models.Country` object

        countryNr   r   rb   rc   rd   re   r&   r&   r'   rg   $  s    
zAsyncClient.countryc                    s   t t| dtjj|I dH S )  Call the Insights endpoint with the specified IP.

        Insights is only supported by the GeoIP2 web service. The GeoLite2 web
        service does not support it.

        :param ip_address: IPv4 or IPv6 address as a string. If no address
          is provided, the address that the web service is called from will
          be used.

        :returns: :py:class:`geoip2.models.Insights` object

        insightsNr   r   rb   rc   rd   re   r&   r&   r'   rj   3  s    zAsyncClient.insightsr   c                    s>   t | ds8tjt| j| jdtdtj| jdd| _	| j	S )NrS   application/json)Accept
User-Agent)total)authheadersr   )
hasattraiohttpClientSessionZ	BasicAuthr   r   _AIOHTTP_UAZClientTimeoutr   rS   r%   r&   r&   r'   _sessionE  s    
zAsyncClient._sessionr)   model_classr*   r   c                    s   |  ||}|  I d H }|j|| jdI d H 4 I d H `}|j}|j}| I d H }	|dkrl| |||	|| |	|}
||
| j	dW  5 Q I d H R  S Q I d H R X d S )N)rX   r3   r   )
r/   rx   rE   rT   r:   r;   textr?   r9   r   )r%   r)   rz   r*   r1   sessionresponser:   r;   r0   rF   r&   r&   r'   rb   O  s     zAsyncClient._response_forc                    s   t | dr| j I dH  dS )fClose underlying session

        This will close the session and any associated connections.
        rS   N)rs   rS   closerw   r&   r&   r'   r   `  s    
zAsyncClient.closec                    s   | S rY   r&   rw   r&   r&   r'   
__aenter__h  s    zAsyncClient.__aenter__exc_type	exc_value	tracebackr   c                    s   |   I d H  d S rY   r   r%   r   r   r   r&   r&   r'   	__aexit__k  s    zAsyncClient.__aexit__)rU   NrV   N)r+   )r+   )r+   )rK   rL   rM   __doc__rt   ru   rN   r   r#   rP   r   rO   r(   r   r   r`   r   rg   r   rj   rx   r   r   rb   r   r   r   __classcell__r&   r&   r\   r'   rR      s8   
0
    
rR   c                	       s   e Zd ZU dZejed< eee	e	f  ed< de
e	e	eee	  eee	 dd fdd	ZdeedddZdeedddZd eedddZe	eee ee ee f eeeeef dddZdd Zd dddZdddddddZ  ZS )!Clientaa  A synchronous GeoIP2 client.

    It accepts the following required arguments:

    :param account_id: Your MaxMind account ID.
    :param license_key: Your MaxMind license key.

    Go to https://www.maxmind.com/en/my_license_key to see your MaxMind
    account ID and license key.

    The following keyword arguments are also accepted:

    :param host: The hostname to make a request against. This defaults to
      "geoip.maxmind.com". To use the GeoLite2 web service instead of the
      GeoIP2 web service, set this to "geolite.info".
    :param locales: This is list of locale codes. This argument will be
      passed on to record classes to use when their name properties are
      called. The default value is ['en'].

      The order of the locales is significant. When a record class has
      multiple names (country, city, etc.), its name property will return
      the name in the first locale that has one.

      Note that the only locale which is always present in the GeoIP2
      data is "en". If you do not include this locale, the name property
      may end up returning None even when the record has an English name.

      Currently, the valid locale codes are:

      * de -- German
      * en -- English names may still include accented characters if that is
        the accepted spelling in English. In other words, English does not
        mean ASCII.
      * es -- Spanish
      * fr -- French
      * ja -- Japanese
      * pt-BR -- Brazilian Portuguese
      * ru -- Russian
      * zh-CN -- Simplified Chinese.
    :param timeout: The timeout in seconds to use when waiting on the request.
      This sets both the connect timeout and the read timeout. The default is
      60.
    :param proxy: The URL of an HTTP proxy to use. It may optionally include
      a basic auth username and password, e.g.,
      ``http://username:password@host:port``.


    rx   _proxiesrU   NrV   rW   c                    sd   t  ||||| t | _| j| jf| j_d| jjd< t	| jjd< |d krVd | _
n
d|i| _
d S )Nrm   rn   ro   https)rZ   r(   requestsSessionrx   r   r   rq   rr   _REQUEST_UAr   r[   r\   r&   r'   r(     s    	
zClient.__init__r+   r^   c                 C   s   t t| dtjj|S )r_   r`   ra   re   r&   r&   r'   r`     s    
zClient.cityc                 C   s   t t| dtjj|S )rf   rg   rh   re   r&   r&   r'   rg     s    
 zClient.countryc                 C   s   t t| dtjj|S )ri   rj   rk   re   r&   r&   r'   rj     s     zClient.insightsry   c           
      C   sj   |  ||}| jj|| j| jd}|j}|jd }|j}|dkrP| ||||| 	||}	||	| j
dS )N)proxiesr   zContent-Typer3   r{   )r/   rx   rE   r   r   status_coderr   r|   r?   r9   r   )
r%   r)   rz   r*   r1   r~   r:   r;   r0   rF   r&   r&   r'   rb     s    
zClient._response_forc                 C   s   | j   dS )r   N)rx   r   rw   r&   r&   r'   r     s    zClient.closerl   c                 C   s   | S rY   r&   rw   r&   r&   r'   	__enter__  s    zClient.__enter__r   c                 C   s   |    d S rY   r   r   r&   r&   r'   __exit__  s    zClient.__exit__)rU   NrV   N)r+   )r+   )r+   )rK   rL   rM   r   r   r   rN   r   r   r#   rP   r   rO   r(   r   r   r`   r   rg   r   rj   r   r   rb   r   r   r   r   r&   r&   r\   r'   r   o  s6   
1
    
r   )(r   r-   r4   typingr   r   r   r   r   r   r   rt   Zaiohttp.httpr   Zrequests.utilsrc   Zgeoip2.modelsZgeoip2.errorsr	   r
   r   r   r   r   r   r   r   r   Zgeoip2.typesr   __version__httpSERVER_SOFTWARErv   utilsdefault_user_agentr   r   rR   r   r&   r&   r&   r'   <module>   s*   $$	   