Esempio n. 1
0
def test_get_user_agent_returns_correct_value_after_setting_suffix(default_user_agent):
    settings.set_user_agent_suffix("example-suffix")
    assert settings.get_user_agent_string() == "{} {}".format(
        default_user_agent, "example-suffix"
    )
    # reset settings to default
    settings.set_user_agent_suffix("")
Esempio n. 2
0
    def __init__(self, session, host_address, auth_handler=None):
        self._initialized = False
        self._needs_auth_renewal_check = False
        self._auth_lock = Lock()
        self._session = session
        adapter = requests.adapters.HTTPAdapter(pool_connections=20, pool_maxsize=20)
        if not host_address.startswith(u"http://") and not host_address.startswith(u"https://"):
            host_address = u"https://{0}".format(host_address)

        self._host_address = host_address
        self._auth_handler = auth_handler

        self._session.proxies = settings.proxies
        self._session.verify = settings.verify_ssl_certs
        self._session.mount(u"https://", adapter)
        self._session.mount(u"http://", adapter)

        self._host_address = host_address
        parsed_host = urlparse(self._host_address)
        host = parsed_host.netloc

        self._session.headers = {
            u"Accept": u"application/json",
            u"Content-Type": u"application/json",
            u"Host": host,
            u"User-Agent": settings.get_user_agent_string(),
            u"Accept-Encoding": u"gzip, deflate",
            u"Connection": u"keep-alive",
        }
Esempio n. 3
0
def test_get_user_agent_returns_correct_value_after_setting_suffix(
        default_user_agent):
    settings.set_user_agent_suffix("example-suffix")
    assert settings.get_user_agent_string(
    ) == f"{default_user_agent} example-suffix"
    # reset settings to default
    settings.set_user_agent_suffix("")
Esempio n. 4
0
    def _prepare_request(
        self,
        method,
        url,
        params=None,
        data=None,
        json=None,
        headers=None,
        cookies=None,
        files=None,
        auth=None,
        hooks=None,
    ):
        url = urljoin(self.host_address, url)
        self._session.proxies = settings.proxies
        self._session.verify = settings.verify_ssl_certs

        if json is not None:
            data = json_lib.dumps(json)

        user_headers = {u"User-Agent": settings.get_user_agent_string()}
        if headers:
            user_headers.update(headers)
        self._headers.update(user_headers)
        request = Request(
            method=method,
            url=url,
            headers=self._headers,
            files=files,
            data=data,
            params=params,
            auth=auth or self._auth,
            cookies=cookies,
            hooks=hooks,
        )

        _print_request(method, url, params=params, data=data)
        return self._session.prepare_request(request)
Esempio n. 5
0
def test_get_user_agent_returns_correct_default_value(default_user_agent):
    assert settings.get_user_agent_string() == default_user_agent
    # reset settings to default
    settings.set_user_agent_suffix("")
Esempio n. 6
0
def _create_user_headers(headers):
    user_headers = {u"User-Agent": settings.get_user_agent_string()}
    if headers:
        user_headers.update(headers)
    return user_headers