Refactored api_request to have a simpler flow logic and accept a function as a parameter, in addition to a permission string