-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add python flask petstore example [skip ci]
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# Haskell | ||
dist-newstyle/ | ||
cabal.project.local* | ||
dist | ||
|
||
# Python | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
flask --app petstore run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
])) | ||
]; | ||
} |