Generalize HTTP request and move to function