See Ash Code logo See Ash Code

My Flask endpoint needed somewhere else to put its work

Ashlynn Antrobus
• • 3 min read

Creating a blog draft involves more than accepting a title from an HTTP request. My application needs to check the slug, create a post, save a Markdown file, record it in GitHub, and persist its database record.

I don’t want all of that hidden in a Flask route.

In the February post-management snapshot, the route builds a CreateDraftCommand and passes it to a handler. The route still handles HTTP. The handler coordinates the draft operation.

Follow one draft

The authenticated POST route checks for a JSON body, reads the slug and title, and uses the authenticated user’s ID as the author. A successful handler result becomes a JSON response with status 201.

The dependencies for this operation look like:

POST request -> Flask route -> CreateDraftCommand
                                  |
                                  v
                         create_draft_handler
                           |      |       |
                           v      v       v
                       draft     post    GitHub
                     repository repository service

The draft repository works with Markdown files. The post repository works with database records. The GitHub service handles the remote content commit.

One dragon passes a request to another, who directs work to a file, a database, and a cloud.

The handler checks for an existing slug in both repositories before creating the domain post and its draft file. That sequence belongs to the create-draft operation even if I later invoke it through something other than this HTTP route.

The failure behavior belongs there too

The handler saves the draft file, attempts the GitHub commit, and then saves the database post.

If the GitHub service returns no commit SHA, it logs a warning and continues with the local save. If the database save raises an exception, it deletes the local draft file and re-raises the failure.

Those are different policies for different failures. Keeping them together makes the ordering visible. It also exposes a question: deleting the local file doesn’t undo a GitHub commit that may already have succeeded. There isn’t one transaction covering all three systems.

A small fake makes the boundary concrete

For a teaching example, this fake implements only the draft-storage methods the handler uses:

class FakeDraftRepository:
    def __init__(self):
        self.drafts = {}

    def find_by_slug(self, slug):
        return self.drafts.get(slug)

    def save(self, draft):
        self.drafts[draft.slug] = draft

    def delete(self, slug):
        self.drafts.pop(slug, None)

A handler test can supply that object alongside separate database and GitHub doubles. It can inspect whether a failed database save removed the local draft without writing a real file or making a GitHub request.

This isn’t a test of filesystem permissions or database transactions. Those belong to the real adapters’ checks. The fake helps test the coordination rule.

The separation is useful without pretending it is complete

The handler takes its collaborators as arguments, but its annotations still name concrete infrastructure classes. The route also constructs those classes through helper functions. This revision hasn’t expressed every boundary as an abstract protocol.

I can now read the order of saves and failures in one function, without working through Flask’s request handling at the same time.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.