From 1132713314f35591bad716b96e2d0d825b4c5dc7 Mon Sep 17 00:00:00 2001 From: Stevan Andjelkovic Date: Mon, 16 Dec 2024 14:11:45 +0100 Subject: [PATCH] feat: add python flask petstore example [skip ci] --- .gitignore | 4 ++ example/petstore-python-flask/Makefile | 2 + .../petstore-with-jwt.py | 41 +++++++++++++++++++ example/petstore-python-flask/petstore.py | 37 +++++++++++++++++ example/petstore-python-flask/shell.nix | 13 ++++++ 5 files changed, 97 insertions(+) create mode 100644 example/petstore-python-flask/Makefile create mode 100644 example/petstore-python-flask/petstore-with-jwt.py create mode 100644 example/petstore-python-flask/petstore.py create mode 100644 example/petstore-python-flask/shell.nix diff --git a/.gitignore b/.gitignore index 42c2cd1..318bf25 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Haskell dist-newstyle/ cabal.project.local* dist + +# Python +__pycache__/ diff --git a/example/petstore-python-flask/Makefile b/example/petstore-python-flask/Makefile new file mode 100644 index 0000000..8cc4845 --- /dev/null +++ b/example/petstore-python-flask/Makefile @@ -0,0 +1,2 @@ +all: + flask --app petstore run diff --git a/example/petstore-python-flask/petstore-with-jwt.py b/example/petstore-python-flask/petstore-with-jwt.py new file mode 100644 index 0000000..9a6726d --- /dev/null +++ b/example/petstore-python-flask/petstore-with-jwt.py @@ -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() diff --git a/example/petstore-python-flask/petstore.py b/example/petstore-python-flask/petstore.py new file mode 100644 index 0000000..ebd6383 --- /dev/null +++ b/example/petstore-python-flask/petstore.py @@ -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/", 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() diff --git a/example/petstore-python-flask/shell.nix b/example/petstore-python-flask/shell.nix new file mode 100644 index 0000000..b7c7142 --- /dev/null +++ b/example/petstore-python-flask/shell.nix @@ -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 + ])) + ]; +}