'Process the results of a command request.
Will execute the \'next\' handler stored in the session
data, or the \'error\' handler depending on the Iq\'s type.
Arguments:
iq -- The command response.'

def _handle_command_result(self, iq):

    sessionid = ('client:' + iq['command']['sessionid'])
    pending = False
    if (sessionid not in self.sessions):
        pending = True
        pendingid = ('client:pending_' + iq['id'])
        if (pendingid not in self.sessions):
            return
        sessionid = pendingid
    session = self.sessions[sessionid]
    sessionid = ('client:' + iq['command']['sessionid'])
    session['id'] = iq['command']['sessionid']
    self.sessions[sessionid] = session
    if pending:
        del self.sessions[pendingid]
    handler_type = 'next'
    if (iq['type'] == 'error'):
        handler_type = 'error'
    handler = session.get(handler_type, None)
    if handler:
        handler(iq, session)
    elif (iq['type'] == 'error'):
        self.terminate_command(session)
    if (iq['command']['status'] == 'completed'):
        self.terminate_command(session)
