While there are various functions which similar usage in all the examples, I'd prefer to keep each example to be self-containing most of the things. However some clearly repeated action can be moved to a single codebase.
Move some common functions to an utilities file.