Refactoring of api handling