Move API recording/playback code out of tests/ directory.

This stuff is unrelated to unit-testing the core code. Having it as part of the
test suite makes it hard to develop, and we'll soon be adding local tests for
the API in tests/api. Also separate tests from bootstrapping code, and add
minimal retry logic for API timeouts.