# Buggy code
def perform_request(
    self, endpoint, read_callback, data="", method="GET", capture_errors=True
):
    """
    Perform a HTTP request.
    :param endpoint: the endpoint to call (i.e. "statistics")
    :param read_callback: the callback to be called with result info when we have the data
    :param data: optional POST data to be sent with the request
    :param method: the HTTP verb (GET/POST/PUT/PATCH)
    :param capture_errors: whether errors should be handled by this class (defaults to True)
    """
    performed_requests[self.request_id] = [endpoint, method, data, time(), -1]
    performed_requests_ids.append(self.request_id)
    if len(performed_requests_ids) > 200:
        del performed_requests[performed_requests_ids.pop(0)]
    url = self.base_url + endpoint

    if method == "GET":
        buf = QBuffer()
        buf.setData(data)
        buf.open(QIODevice.ReadOnly)
        get_request = QNetworkRequest(QUrl(url))
        self.reply = self.sendCustomRequest(get_request, "GET", buf)
        buf.setParent(self.reply)
    elif method == "PATCH":
        buf = QBuffer()
        buf.setData(data)
        buf.open(QIODevice.ReadOnly)
        patch_request = QNetworkRequest(QUrl(url))
        self.reply = self.sendCustomRequest(patch_request, "PATCH", buf)
        buf.setParent(self.reply)
    elif method == "PUT":
        request = QNetworkRequest(QUrl(url))
        request.setHeader(
            QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded"
        )
        self.reply = self.put(request, data)
    elif method == "DELETE":
        buf = QBuffer()
        buf.setData(data)
        buf.open(QIODevice.ReadOnly)
        delete_request = QNetworkRequest(QUrl(url))
        self.reply = self.sendCustomRequest(delete_request, "DELETE", buf)
        buf.setParent(self.reply)
    elif method == "POST":
        request = QNetworkRequest(QUrl(url))
        request.setHeader(
            QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded"
        )
        self.reply = self.post(request, data)

    if read_callback:
        self.received_json.connect(read_callback)

    self.finished.connect(lambda reply: self.on_finished(reply, capture_errors))


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/usr/share/tribler/TriblerGUI/event_request_manager.py", line 99, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: [Failure instance: Traceback: <type 'exceptions.IOError'>: [Errno 24] Too many open files: u'/home/dsk/.Tribler/peercache.txt'
/usr/lib/python2.7/dist-packages/twisted/internet/base.py:1194:run
/usr/lib/python2.7/dist-packages/twisted/internet/base.py:1203:mainLoop
/usr/lib/python2.7/dist-packages/twisted/internet/base.py:825:runUntilCurrent
/usr/lib/python2.7/dist-packages/twisted/internet/task.py:239:__call__
--- <exception caught here> ---
/usr/lib/python2.7/dist-packages/twisted/internet/defer.py:150:maybeDeferred
/usr/share/tribler/Tribler/dispersy/discovery/community.py:801:clean_and_save
]

# Fixed code
def perform_request(
    self,
    endpoint,
    read_callback,
    data="",
    method="GET",
    capture_errors=True,
    priority=QueuePriorityEnum.CRITICAL,
    on_cancel=lambda: None,
):
    """
    Perform a HTTP request.
    :param endpoint: the endpoint to call (i.e. "statistics")
    :param read_callback: the callback to be called with result info when we have the data
    :param data: optional POST data to be sent with the request
    :param method: the HTTP verb (GET/POST/PUT/PATCH)
    :param capture_errors: whether errors should be handled by this class (defaults to True)
    """
    url = self.base_url + endpoint

    self.status_code = -1
    self.on_cancel = on_cancel
    request_queue.enqueue(
        self,
        lambda: self.dispatch_map.get(method, lambda x, y, z: None)(
            endpoint, data, url
        ),
        priority,
    )

    if read_callback:
        self.received_json.connect(read_callback)

    self.finished.connect(lambda reply: self.on_finished(reply, capture_errors))