From fd1b008ab263eaab08cc879c3bbf32ed19bb4cc9 Mon Sep 17 00:00:00 2001 From: Michel Romero Date: Thu, 24 Jul 2025 18:09:22 -0300 Subject: [PATCH 1/2] Move the basic example into a new directory --- examples/README.md | 3 +++ examples/{ => basic}/verify-token.py | 0 2 files changed, 3 insertions(+) create mode 100644 examples/README.md rename examples/{ => basic}/verify-token.py (100%) diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..0f49df1 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,3 @@ +# Virtual Queue Python SDK Examples + +Here you can find examples on how to use this SDK diff --git a/examples/verify-token.py b/examples/basic/verify-token.py similarity index 100% rename from examples/verify-token.py rename to examples/basic/verify-token.py From 45101cde8da3d4ded939d186cf82da6f6effdc27 Mon Sep 17 00:00:00 2001 From: Michel Romero Date: Thu, 24 Jul 2025 18:10:07 -0300 Subject: [PATCH 2/2] Add an example with Flask --- examples/flask/README.md | 31 +++++++++++++++++++++++++ examples/flask/app.py | 40 +++++++++++++++++++++++++++++++++ examples/flask/requirements.txt | 2 ++ 3 files changed, 73 insertions(+) create mode 100644 examples/flask/README.md create mode 100644 examples/flask/app.py create mode 100644 examples/flask/requirements.txt diff --git a/examples/flask/README.md b/examples/flask/README.md new file mode 100644 index 0000000..8e2d086 --- /dev/null +++ b/examples/flask/README.md @@ -0,0 +1,31 @@ +# Flask Example for VQueue's Python SDK + +This is an example of how to use `vqueue-sdk` within a Flask app. + +## Setup + +1. Create a virtual environment + +```sh +python -m venv .venv +``` + +2. Activate the environment + +```sh +source .venv/bin/activate +``` + +3. Install the required packages from `requirements.txt` + +```sh +pip install -r requirements.txt +``` + +## Running + +To run this example use + +```sh +flask run +``` diff --git a/examples/flask/app.py b/examples/flask/app.py new file mode 100644 index 0000000..627dc09 --- /dev/null +++ b/examples/flask/app.py @@ -0,0 +1,40 @@ +from flask import Flask, g, jsonify, request + +from vqueue import TokenVerifier +from vqueue.exceptions import VQueueError + +app = Flask(__name__) + + +def get_token_verifier() -> TokenVerifier: + """ + Gets the TokenVerifier from the global context of the app. + It's a good practice in order to reuse connections. + """ + if "VQueueTokenVerifier" not in g: + g.VQueueTokenVerifier = TokenVerifier() + + return g.VQueueTokenVerifier + + +# Verify the token at root path, this expects a `token` query param +@app.route("/") +def hello(): + token = request.args.get("token") + + # Get the token verifier from the global context + token_verifier = get_token_verifier() + if token: + try: + # Call the `TokenVerifier.verify_token` method + verification_result = token_verifier.verify_token(token) + + # If the verification is successful you can continue with your app's logic + return jsonify(verification_result) + except VQueueError as e: + # In case of an un successful verification, there will be an + # exception for you to handle. You can be more specific with the + # handling of the distinct exceptions that the method can raise + return jsonify({"success": False, "message": e.__dict__}) + + return jsonify({"success": False, "message": "No token parameter was given"}), 404 diff --git a/examples/flask/requirements.txt b/examples/flask/requirements.txt new file mode 100644 index 0000000..df7c546 --- /dev/null +++ b/examples/flask/requirements.txt @@ -0,0 +1,2 @@ +vqueue-sdk +Flask