U
    
ڲg                  	   @   s   d Z ddlmZ ddlZddlmZ eejjdr<ddiZ	nddiZ	ejf dddd	d
gddde	Z
dZG dd dZG dd deZdS )zqInternal HTTP client module.

 This module provides utilities for making HTTP calls using the requests library.
     )	transportN)retryZallowed_methodsmethod_whitelist      i  i  Fg      ?)connectreadstatusstatus_forcelistraise_on_statusbackoff_factorx   c                   @   s   e Zd ZdZddddeefddZedd Zedd	 Z	ed
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )
HttpClientzBase HTTP client used to make HTTP calls.

    HttpClient maintains an HTTP session, and handles request authentication and retries if
    necessary.
    N c                 C   s   |rt j|| _n|r || _n
t | _|r<| jj| |rp| jdtjj	|d | jdtjj	|d || _
|| _dS )a  Creates a new HttpClient instance from the provided arguments.

        If a credential is provided, initializes a new HTTP session authorized with it. If neither
        a credential nor a session is provided, initializes a new unauthorized session.

        Args:
          credential: A Google credential that can be used to authenticate requests (optional).
          session: A custom HTTP session (optional).
          base_url: A URL prefix to be added to all outgoing requests (optional).
          headers: A map of headers to be added to all outgoing requests (optional).
          retries: A urllib retry configuration. Default settings would retry once for low-level
              connection and socket read errors, and up to 4 times for HTTP 500 and 503 errors.
              Pass a False value to disable retries (optional).
          timeout: HTTP timeout in seconds. Defaults to 120 seconds when not specified. Set to
              None to disable timeouts (optional).
        zhttp://)max_retrieszhttps://N)r   requestsZAuthorizedSession_sessionSessionheadersupdatemountadaptersHTTPAdapter	_base_url_timeout)selfZ
credentialsessionbase_urlr   retriestimeout r    ?/tmp/pip-unpacked-wheel-p0r7i5ii/firebase_admin/_http_client.py__init__/   s    
zHttpClient.__init__c                 C   s   | j S N)r   r   r    r    r!   r   Q   s    zHttpClient.sessionc                 C   s   | j S r#   )r   r$   r    r    r!   r   U   s    zHttpClient.base_urlc                 C   s   | j S r#   )r   r$   r    r    r!   r   Y   s    zHttpClient.timeoutc                 C   s   t d S r#   )NotImplementedErrorr   respr    r    r!   
parse_body]   s    zHttpClient.parse_bodyc                 K   s6   d|kr| j |d< | jj|| j| f|}|  |S )a	  Makes an HTTP call using the Python requests library.

        This is the sole entry point to the requests library. All other helper methods in this
        class call this method to send HTTP requests out. Refer to
        http://docs.python-requests.org/en/master/api/ for more information on supported options
        and features.

        Args:
          method: HTTP method name as a string (e.g. get, post).
          url: URL of the remote endpoint.
          **kwargs: An additional set of keyword arguments to be passed into the requests API
              (e.g. json, params, timeout).

        Returns:
          Response: An HTTP response object.

        Raises:
          RequestException: Any requests exceptions encountered while making the HTTP call.
        r   )r   r   requestr   raise_for_statusr   methodurlkwargsr'   r    r    r!   r)   `   s
    
zHttpClient.requestc                 K   s   | j ||f|}|jS r#   )r)   r   r+   r    r    r!   r   z   s    zHttpClient.headersc                 K   s   | j ||f|}| ||fS r#   r)   r(   r+   r    r    r!   body_and_response~   s    zHttpClient.body_and_responsec                 K   s   | j ||f|}| |S r#   r/   r+   r    r    r!   body   s    zHttpClient.bodyc                 K   s    | j ||f|}|j| |fS r#   )r)   r   r(   r+   r    r    r!   headers_and_body   s    zHttpClient.headers_and_bodyc                 C   s   | j   d | _ d S r#   )r   closer$   r    r    r!   r3      s    
zHttpClient.close)__name__
__module____qualname____doc__DEFAULT_RETRY_CONFIGDEFAULT_TIMEOUT_SECONDSr"   propertyr   r   r   r(   r)   r   r0   r1   r2   r3   r    r    r    r!   r   (   s*       
"


r   c                   @   s    e Zd ZdZdd Zdd ZdS )JsonHttpClientz5An HTTP client that parses response messages as JSON.c                 K   s   t j| f| d S r#   )r   r"   )r   r.   r    r    r!   r"      s    zJsonHttpClient.__init__c                 C   s   |  S r#   )jsonr&   r    r    r!   r(      s    zJsonHttpClient.parse_bodyN)r4   r5   r6   r7   r"   r(   r    r    r    r!   r;      s   r;   )r7   Zgoogle.authr   r   Zrequests.packages.urllib3.utilr   hasattrRetryDEFAULTZ_ANY_METHODr8   r9   r   r;   r    r    r    r!   <module>   s&   
    g