Optional tweak that you might find helpful/cleaner: You could replace ``if resp is None`` with ``else``; then it will only hit this if you don't explicitly break out of the loop (which you always will if the query succeeds).