Factor out duplicate code in api test script.