def list_posts():
    connection = dbConnector.connect()
    params = functions.get_json(request)
    optional = functions.get_optional(
        request=params, values=["limit", "order", "since", "sort"])
    try:
        functions.check(params, ["thread"])
        response = post.posts_list(
            connection=connection, entity="thread", params=optional, identifier=params["thread"], related=[])
    except Exception as e:
        connection.close()
        return json.dumps({"code": 1, "response": e.message})
    connection.close()
    return json.dumps({"code": 0, "response": response})
def forum_list_posts():
    connection = dbConnector.connect()
    params = functions.get_json(request)
    optional = functions.get_optional(
        params,
        ["since",
         "limit",
         "order"]
    )
    related = functions.get_related(params)
    try:
        functions.check(params, ["forum"])
        response = post.posts_list(connection, entity="forum", params=optional, identifier=params[
                                   "forum"], related=related)
    except Exception, e:
        connection.close()
        return json.dumps({"code": 3, "response": e.message})
def post_list():
    connection = dbConnector.connect()
    content = functions.get_json(request)
    try:
        identifier = content["forum"]
        entity = "forum"
    except KeyError:
        try:
            identifier = content["thread"]
            entity = "thread"
        except Exception as e:
            connection.close()
            return json.dumps({"code": 1, "response": e.message})

    optional = functions.get_optional(
        request=content, values=["limit", "order", "since"])
    try:
        p_list = post.posts_list(
            connection=connection, entity=entity, params=optional, identifier=identifier, related=[])
    except Exception as e:
        connection.close()
        return json.dumps({"code": 1, "response": e.message})
    connection.close()
    return json.dumps({"code": 0, "response": p_list})