# Buggy code
def setup_api():
    session = requests.Session()
    session.mount(
        API_HOST,
        host_header_ssl.HostHeaderSSLAdapter(
            max_retries=3,
        ),
    )
    session.headers.update({"Host": PRODUCTION_DOMAIN})
    api_config = {
        "base_url": "%s/api/v2/" % API_HOST,
        "serializer": serialize.Serializer(
            default="json-drf",
            serializers=[
                serialize.JsonSerializer(),
                DrfJsonSerializer(),
            ],
        ),
        "session": session,
    }
    if USER and PASS:
        log.debug(
            "Using slumber v2 with user %s, pointed at %s",
            USER,
            API_HOST,
        )
        session.auth = (USER, PASS)
    else:
        log.warning("SLUMBER_USERNAME/PASSWORD settings are not set")
    return API(**api_config)


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/humitos/rtfd/code/readthedocs.org/readthedocs/projects/tasks.py", line 333, in run
self.project = self.get_project(pk)
File "/home/humitos/rtfd/code/readthedocs.org/readthedocs/projects/tasks.py", line 512, in get_project
project_data = api_v2.project(project_pk).get()
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/slumber/__init__.py", line 155, in get
resp = self._request("GET", params=kwargs)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/slumber/__init__.py", line 97, in _request
resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/requests_toolbelt/adapters/host_header_ssl.py", line 43, in send
return super(HostHeaderSSLAdapter, self).send(request, **kwargs)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
timeout=timeout
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/urllib3/connectionpool.py", line 589, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/urllib3/connectionpool.py", line 251, in _get_conn
return conn or self._new_conn()
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/urllib3/connectionpool.py", line 212, in _new_conn
strict=self.strict, **self.conn_kw)
File "/home/humitos/.pyenv/versions/3.6.6/envs/readthedocs.org/lib/python3.6/site-packages/urllib3/connection.py", line 125, in __init__
_HTTPConnection.__init__(self, *args, **kw)
TypeError: __init__() got an unexpected keyword argument 'assert_hostname'

# Fixed code
def setup_api():
    session = requests.Session()
    if API_HOST.startswith("https"):
        # Only use the HostHeaderSSLAdapter for HTTPS connections
        adapter_class = host_header_ssl.HostHeaderSSLAdapter
    else:
        adapter_class = requests.adapters.HTTPAdapter

    session.mount(
        API_HOST,
        adapter_class(max_retries=3),
    )
    session.headers.update({"Host": PRODUCTION_DOMAIN})
    api_config = {
        "base_url": "%s/api/v2/" % API_HOST,
        "serializer": serialize.Serializer(
            default="json-drf",
            serializers=[
                serialize.JsonSerializer(),
                DrfJsonSerializer(),
            ],
        ),
        "session": session,
    }
    if USER and PASS:
        log.debug(
            "Using slumber v2 with user %s, pointed at %s",
            USER,
            API_HOST,
        )
        session.auth = (USER, PASS)
    else:
        log.warning("SLUMBER_USERNAME/PASSWORD settings are not set")
    return API(**api_config)