Skip to content

API

We have set up Django Ninja for fast api creations, locallibrary/api.py.

We can send payloads via a GET querystring - http://localhost:8000/api/genres?JWT=123456 and extract JWT to then authenticate:

@api.get("/genres", response=List[GenreOut])
def list_genres(request):
    # http://localhost:8000/api/genres?JWT=123456
    JWT = request.GET.get("JWT", None)

    if JWT is None:
        print(f"UNAUTHORISED {JWT}")
    else:
        print(f"AUTHORISED {JWT}")
    qs = Genre.objects.all()
    return qs
or we can send this over in headers as we do with a post request to add an employee:

POST

@api.post("/employees")
def create_employee(request, payload: EmployeeIn):
    # http://localhost:8000/api/employees
    # with Headers(JWT=123456)
    # if JWT is valid then we can do create etc
    first_name = payload.dict()["first_name"]
    last_name = payload.dict()["last_name"]
    birthdate = payload.dict()["birthdate"]
    JWT = payload.dict()["JWT"]
    print(f"PAYLOAD: {first_name} {last_name} {birthdate} {JWT}")
    employee = Employee.objects.create(
        first_name=first_name, last_name=last_name, birthdate=birthdate
    )
    return {"id": employee.id}