# Buggy code
def http_get(uri):
    """
    Performs a GET request
    :param uri: The URL to perform a GET request to
    :return: A deferred firing the body of the response.
    :raises HttpError: When the HTTP response code is not OK (i.e. not the HTTP Code 200)
    """

    def _on_response(response):
        if response.code == http.OK:
            return readBody(response)
        if response.code == http.FOUND:
            # Check if location header contains magnet link
            location_headers = response.headers.getRawHeaders("location")
            if not location_headers:
                return fail(
                    Failure(
                        RuntimeError(
                            "HTTP redirect response does not contain location header"
                        )
                    )
                )
            new_uri = location_headers[0]
            if new_uri.startswith("magnet"):
                _, infohash, _ = parse_magnetlink(new_uri)
                if infohash:
                    return succeed(new_uri)
            return http_get(new_uri)
        raise HttpError(response)

    try:
        uri = six.ensure_binary(uri)
    except AttributeError:
        pass
    try:
        contextFactory = WebClientContextFactory()
        agent = Agent(reactor, contextFactory)
        headers = Headers({"User-Agent": ["Tribler " + version_id]})
        deferred = agent.request(b"GET", uri, headers, None)
        deferred.addCallback(_on_response)
        return deferred
    except:
        return fail()


# Traceback (ImportError)
Traceback (most recent call last):
File "run_tribler.py", line 95, in <module>
start_tribler_core(base_path, api_port)
File "run_tribler.py", line 43, in start_tribler_core
from Tribler.Core.Session import Session
File "/home/tribler/tudelft/tribler/Tribler/Core/Session.py", line 21, in <module>
from Tribler.Core.APIImplementation.LaunchManyCore import TriblerLaunchMany
File "/home/tribler/tudelft/tribler/Tribler/Core/APIImplementation/LaunchManyCore.py", line 40, in <module>
from Tribler.Core.Modules.MetadataStore.store import MetadataStore
File "/home/tribler/tudelft/tribler/Tribler/Core/Modules/MetadataStore/store.py", line 16, in <module>
from Tribler.Core.Modules.MetadataStore.OrmBindings import (
File "/home/tribler/tudelft/tribler/Tribler/Core/Modules/MetadataStore/OrmBindings/channel_metadata.py", line 19, in <module>
from Tribler.Core.TorrentDef import TorrentDef
File "/home/tribler/tudelft/tribler/Tribler/Core/TorrentDef.py", line 13, in <module>
from six import binary_type, ensure_binary, integer_types, text_type
ImportError: cannot import name 'ensure_binary'

# Fixed code
def http_get(uri):
    """
    Performs a GET request
    :param uri: The URL to perform a GET request to
    :return: A deferred firing the body of the response.
    :raises HttpError: When the HTTP response code is not OK (i.e. not the HTTP Code 200)
    """

    def _on_response(response):
        if response.code == http.OK:
            return readBody(response)
        if response.code == http.FOUND:
            # Check if location header contains magnet link
            location_headers = response.headers.getRawHeaders("location")
            if not location_headers:
                return fail(
                    Failure(
                        RuntimeError(
                            "HTTP redirect response does not contain location header"
                        )
                    )
                )
            new_uri = location_headers[0]
            if new_uri.startswith("magnet"):
                _, infohash, _ = parse_magnetlink(new_uri)
                if infohash:
                    return succeed(new_uri)
            return http_get(new_uri)
        raise HttpError(response)

    try:
        uri = ensure_binary(uri)
    except AttributeError:
        pass
    try:
        contextFactory = WebClientContextFactory()
        agent = Agent(reactor, contextFactory)
        headers = Headers({"User-Agent": ["Tribler " + version_id]})
        deferred = agent.request(b"GET", uri, headers, None)
        deferred.addCallback(_on_response)
        return deferred
    except:
        return fail()