Refactored to move all the repeated HTTP request code into the library as a single function.