Skip to content

Commit

Permalink
feat: add python flask petstore example [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
stevana committed Dec 16, 2024
1 parent 1de9cd6 commit 1132713
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Haskell
dist-newstyle/
cabal.project.local*
dist

# Python
__pycache__/
2 changes: 2 additions & 0 deletions example/petstore-python-flask/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
flask --app petstore run
41 changes: 41 additions & 0 deletions example/petstore-python-flask/petstore-with-jwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from flask import Flask
from flask import jsonify
from flask import request

from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import jwt_required
from flask_jwt_extended import JWTManager

app = Flask(__name__)

# Setup the Flask-JWT-Extended extension
app.config["JWT_SECRET_KEY"] = "super-secret" # Change this!
jwt = JWTManager(app)


# Create a route to authenticate your users and return JWTs. The
# create_access_token() function is used to actually generate the JWT.
@app.route("/login", methods=["POST"])
def login():
username = request.json.get("username", None)
password = request.json.get("password", None)
if username != "test" or password != "test":
return jsonify({"msg": "Bad username or password"}), 401

access_token = create_access_token(identity=username)
return jsonify(access_token=access_token)


# Protect a route with jwt_required, which will kick out requests
# without a valid JWT present.
@app.route("/protected", methods=["GET"])
@jwt_required()
def protected():
# Access the identity of the current user with get_jwt_identity
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200


if __name__ == "__main__":
app.run()
37 changes: 37 additions & 0 deletions example/petstore-python-flask/petstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from flask import Flask
from flask import jsonify
from flask import request

app = Flask(__name__)

# NOTE: Not thread-safe and won't work if clients connect concurrently.
petstore = {}

@app.route("/pet/<pet_id>", methods=["GET"])
def get_pet(pet_id):
if pet_id in petstore:
pet_name = petstore[pet_id]
return jsonify(petId=pet_id, petName=pet_name)
else:
return "Pet not found", 404

@app.route("/pet", methods=["POST"])
def add_pet():
pet_id = request.json.get("petId", None)
pet_name = request.json.get("petName", None)
if pet_id in petstore:
return jsonify(error="Pet already exists"), 409
petstore[pet_id] = pet_name
return "[]", 201

@app.route("/health", methods=["GET"])
def health():
return "[]", 200

@app.route("/_reset", methods=["DELETE"])
def _reset():
petstore.clear()
return "[]", 200

if __name__ == "__main__":
app.run()
13 changes: 13 additions & 0 deletions example/petstore-python-flask/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/24.05.tar.gz";
pkgs = import nixpkgs { config = {}; overlays = []; };
in

pkgs.mkShell {
packages = with pkgs; [
(pkgs.python3.withPackages (python-pkgs: with python-pkgs; [
flask
flask-jwt-extended
]))
];
}

0 comments on commit 1132713

Please sign in to comment.