Refactor requests logic for mocking in tests