# Buggy code
def _load_config(
    self,
    host,
    client_id,
    namespace,
    other_client_id,
    other_client_secret,
    existing_token,
):
    config = kfp_server_api.configuration.Configuration()

    host = host or ""
    # Preprocess the host endpoint to prevent some common user mistakes.
    # This should only be done for non-IAP cases (when client_id is None). IAP requires preserving the protocol.
    if not client_id:
        host = re.sub(r"^(http|https)://", "", host).rstrip("/")

    if host:
        config.host = host

    token = None

    # "existing_token" is designed to accept token generated outside of SDK. Here is an example.
    #
    # https://cloud.google.com/functions/docs/securing/function-identity
    # https://cloud.google.com/endpoints/docs/grpc/service-account-authentication
    #
    # import requests
    # import kfp
    #
    # def get_access_token():
    #     url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token'
    #     r = requests.get(url, headers={'Metadata-Flavor': 'Google'})
    #     r.raise_for_status()
    #     access_token = r.json()['access_token']
    #     return access_token
    #
    # client = kfp.Client(host='<KFPHost>', existing_token=get_access_token())
    #
    if existing_token:
        token = existing_token
    elif client_id:
        token = get_auth_token(client_id, other_client_id, other_client_secret)
    elif self._is_inverse_proxy_host(host):
        token = get_gcp_access_token()

    if token:
        config.api_key["authorization"] = token
        config.api_key_prefix["authorization"] = "Bearer"
        return config

    if host:
        # if host is explicitly set with auth token, it's probably a port forward address.
        return config

    import kubernetes as k8s

    in_cluster = True
    try:
        k8s.config.load_incluster_config()
    except:
        in_cluster = False
        pass

    if in_cluster:
        config.host = Client.IN_CLUSTER_DNS_NAME.format(namespace)
        return config

    try:
        k8s.config.load_kube_config(client_configuration=config)
    except:
        print("Failed to load kube config.")
        return config

    if config.host:
        config.host = config.host + "/" + Client.KUBE_PROXY_PATH.format(namespace)
    return config


# Traceback (urllib3.exceptions.LocationValueError)
Traceback (most recent call last):
File "../kubeflow-apply/kubeflow-apply.py", line 73, in <module>
args.func()
File "../kubeflow-apply/kubeflow-apply.py", line 33, in pipelines
self.client.upload_pipeline(filename, name, description)
File "/usr/local/lib/python3.8/site-packages/kfp/_client.py", line 723, in upload_pipeline
response = self._upload_api.upload_pipeline(pipeline_package_path, name=pipeline_name, description=description)
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/api/pipeline_upload_service_api.py", line 83, in upload_pipeline
return self.upload_pipeline_with_http_info(uploadfile, **kwargs)  # noqa: E501
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/api/pipeline_upload_service_api.py", line 177, in upload_pipeline_with_http_info
return self.api_client.call_api(
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/api_client.py", line 379, in call_api
return self.__call_api(resource_path, method,
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/api_client.py", line 196, in __call_api
response_data = self.request(
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/api_client.py", line 422, in request
return self.rest_client.POST(url,
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/rest.py", line 279, in POST
return self.request("POST", url,
File "/usr/local/lib/python3.8/site-packages/kfp_server_api/rest.py", line 196, in request
r = self.pool_manager.request(
File "/usr/local/lib/python3.8/site-packages/urllib3/request.py", line 79, in request
return self.request_encode_body(
File "/usr/local/lib/python3.8/site-packages/urllib3/request.py", line 172, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/usr/local/lib/python3.8/site-packages/urllib3/poolmanager.py", line 326, in urlopen
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
File "/usr/local/lib/python3.8/site-packages/urllib3/poolmanager.py", line 231, in connection_from_host
raise LocationValueError("No host specified.")
urllib3.exceptions.LocationValueError: No host specified.

# Fixed code
def _load_config(
    self,
    host,
    client_id,
    namespace,
    other_client_id,
    other_client_secret,
    existing_token,
):
    config = kfp_server_api.configuration.Configuration()

    host = host or ""
    # Preprocess the host endpoint to prevent some common user mistakes.
    if not client_id:
        # always preserving the protocol (http://localhost requires it)
        host = host.rstrip("/")

    if host:
        config.host = host

    token = None

    # "existing_token" is designed to accept token generated outside of SDK. Here is an example.
    #
    # https://cloud.google.com/functions/docs/securing/function-identity
    # https://cloud.google.com/endpoints/docs/grpc/service-account-authentication
    #
    # import requests
    # import kfp
    #
    # def get_access_token():
    #     url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token'
    #     r = requests.get(url, headers={'Metadata-Flavor': 'Google'})
    #     r.raise_for_status()
    #     access_token = r.json()['access_token']
    #     return access_token
    #
    # client = kfp.Client(host='<KFPHost>', existing_token=get_access_token())
    #
    if existing_token:
        token = existing_token
    elif client_id:
        token = get_auth_token(client_id, other_client_id, other_client_secret)
    elif self._is_inverse_proxy_host(host):
        token = get_gcp_access_token()

    if token:
        config.api_key["authorization"] = token
        config.api_key_prefix["authorization"] = "Bearer"
        return config

    if host:
        # if host is explicitly set with auth token, it's probably a port forward address.
        return config

    import kubernetes as k8s

    in_cluster = True
    try:
        k8s.config.load_incluster_config()
    except:
        in_cluster = False
        pass

    if in_cluster:
        config.host = Client.IN_CLUSTER_DNS_NAME.format(namespace)
        return config

    try:
        k8s.config.load_kube_config(client_configuration=config)
    except:
        print("Failed to load kube config.")
        return config

    if config.host:
        config.host = config.host + "/" + Client.KUBE_PROXY_PATH.format(namespace)
    return config