示例#1
0
文件: utils.py 项目: mode/plotly.py
def get_headers():
    """
    Using session credentials/config, get headers for a V2 API request.

    Users may have their own proxy layer and so we free up the `authorization`
    header for this purpose (instead adding the user authorization in a new
    `plotly-authorization` header). See pull #239.

    :returns: (dict) Headers to add to a requests.request call.

    """
    creds = config.get_credentials()

    headers = {
        'plotly-client-platform': 'python {}'.format(version.stable_semver()),
        'content-type': 'application/json'
    }

    plotly_auth = basic_auth(creds['username'], creds['api_key'])
    proxy_auth = basic_auth(creds['proxy_username'], creds['proxy_password'])

    if config.get_config()['plotly_proxy_authorization']:
        headers['authorization'] = proxy_auth
        if creds['username'] and creds['api_key']:
            headers['plotly-authorization'] = plotly_auth
    else:
        if creds['username'] and creds['api_key']:
            headers['authorization'] = plotly_auth

    return headers
示例#2
0
def get_headers():
    """
    Using session credentials/config, get headers for a V2 API request.

    Users may have their own proxy layer and so we free up the `authorization`
    header for this purpose (instead adding the user authorization in a new
    `plotly-authorization` header). See pull #239.

    :returns: (dict) Headers to add to a requests.request call.

    """
    creds = config.get_credentials()

    headers = {
        'plotly-client-platform': 'python {}'.format(version.stable_semver()),
        'content-type': 'application/json'
    }

    plotly_auth = basic_auth(creds['username'], creds['api_key'])
    proxy_auth = basic_auth(creds['proxy_username'], creds['proxy_password'])

    if config.get_config()['plotly_proxy_authorization']:
        headers['authorization'] = proxy_auth
        if creds['username'] and creds['api_key']:
            headers['plotly-authorization'] = plotly_auth
    else:
        if creds['username'] and creds['api_key']:
            headers['authorization'] = plotly_auth

    return headers
示例#3
0
def get_headers():
    """
    Using session credentials/config, get headers for a v1 API request.

    Users may have their own proxy layer and so we free up the `authorization`
    header for this purpose (instead adding the user authorization in a new
    `plotly-authorization` header). See pull #239.

    :returns: (dict) Headers to add to a requests.request call.

    """
    headers = {}
    creds = config.get_credentials()
    proxy_auth = basic_auth(creds['proxy_username'], creds['proxy_password'])

    if config.get_config()['plotly_proxy_authorization']:
        headers['authorization'] = proxy_auth

    return headers
示例#4
0
def get_headers():
    """
    Using session credentials/config, get headers for a v1 API request.

    Users may have their own proxy layer and so we free up the `authorization`
    header for this purpose (instead adding the user authorization in a new
    `plotly-authorization` header). See pull #239.

    :returns: (dict) Headers to add to a requests.request call.

    """
    headers = {}
    creds = config.get_credentials()
    proxy_auth = basic_auth(creds['proxy_username'], creds['proxy_password'])

    if config.get_config()['plotly_proxy_authorization']:
        headers['authorization'] = proxy_auth

    return headers
示例#5
0
    def _get_proxy_config(self):
        """
        Determine if self._url should be passed through a proxy. If so, return
        the appropriate proxy_server and proxy_port. Assumes https_proxy is used
        when ssl_enabled=True.

        """

        proxy_server = None
        proxy_port = None
        proxy_username = None
        proxy_password = None
        proxy_auth = None
        ssl_enabled = self._ssl_enabled

        if ssl_enabled:
            proxy = (os.environ.get("https_proxy") or
                     os.environ.get("HTTPS_PROXY"))
        else:
            proxy = (os.environ.get("http_proxy") or
                     os.environ.get("HTTP_PROXY"))

        no_proxy = os.environ.get("no_proxy") or os.environ.get("NO_PROXY")
        no_proxy_url = no_proxy and self._server in no_proxy

        if proxy and not no_proxy_url:
            p = urlparse(proxy)
            proxy_server = p.hostname
            proxy_port = p.port
            proxy_username = p.username
            proxy_password = p.password

        if proxy_username and proxy_password:
            username = unquote(proxy_username)
            password = unquote(proxy_password)
            proxy_auth = utils.basic_auth(username, password)

        return proxy_server, proxy_port, proxy_auth
示例#6
0
    def _get_proxy_config(self):
        """
        Determine if self._url should be passed through a proxy. If so, return
        the appropriate proxy_server and proxy_port. Assumes https_proxy is used
        when ssl_enabled=True.

        """

        proxy_server = None
        proxy_port = None
        proxy_username = None
        proxy_password = None
        proxy_auth = None
        ssl_enabled = self._ssl_enabled

        if ssl_enabled:
            proxy = (os.environ.get("https_proxy")
                     or os.environ.get("HTTPS_PROXY"))
        else:
            proxy = (os.environ.get("http_proxy")
                     or os.environ.get("HTTP_PROXY"))

        no_proxy = os.environ.get("no_proxy") or os.environ.get("NO_PROXY")
        no_proxy_url = no_proxy and self._server in no_proxy

        if proxy and not no_proxy_url:
            p = urlparse(proxy)
            proxy_server = p.hostname
            proxy_port = p.port
            proxy_username = p.username
            proxy_password = p.password

        if proxy_username and proxy_password:
            username = unquote(proxy_username)
            password = unquote(proxy_password)
            proxy_auth = utils.basic_auth(username, password)

        return proxy_server, proxy_port, proxy_auth