bookmark_borderFastAPI Stripe Webhook Template

A FastAPI equivalent for verify-events-came-from-stripe.

import os

from http import HTTPStatus
from typing import Annotated

import stripe
from fastapi import Depends, FastAPI, Header, HTTPException, Request


app = FastAPI()


async def get_body(request: Request) -> bytes:
    return await request.body()


@app.post("/webhook", status_code=HTTPStatus.NO_CONTENT)
def post_report(
    stripe_signature: Annotated[str, Header(alias="stripe-signature")],
    body: bytes = Depends(get_body),
) -> None:
    endpoint_secret = os.environ["ENDPOINT_SECRET"]

    try:
        # signature validation
        event = stripe.Webhook.construct_event(body, stripe_signature, endpoint_secret)
    except ValueError as e:
        # Invalid payload
        raise HTTPException(status_code=HTTPStatus.BAD_REQUEST) from e
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        raise HTTPException(status_code=HTTPStatus.UNPROCESSABLE_ENTITY) from e

    print(event)

    return

A possible catch, one that I ran into, is that I had FastAPI convert the request.body to a dictionary in the function parameters (so body: dict). Then I serialized it to a string for the validation step… and it failed because it was no longer identical to what came in.

bookmark_borderOpset – a python configuration library

Last week, my team open sourced the configuration library we have streamlined for the software components that we work on.

Opset – A library for simplifying the configuration of Python applications at all stages of deployment. [github] [pypi]

Continue reading “Opset – a python configuration library”

bookmark_borderA chrome extension to navigate Github files differently

Last week, I was somewhat complaining that Github’s file navigator was not to my taste. When moving across files and directory, you have to go back and forth a lot. If you quickly want to switch between 2 files not in the same directory, it’s painful. Of course, one could open another tab, but tabbing doesn’t cut it. I wanted something more like a files navigator that we can find in any OS.

Then I though, hey, surely I can simply manipulate the HTML to make it work the way I want. I started coding… 45 minutes later, I had a working prototype of JavaScript code that I could simply paste in the browser console and bang; a nice file tree navigator-like experience right in Github.

Continue reading “A chrome extension to navigate Github files differently”