-
Notifications
You must be signed in to change notification settings - Fork 379
Implement Flask backend API with progress tracking endpoints #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/implement-backend-api-with-flask
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| # Python | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| *.so | ||
| .Python | ||
| *.egg | ||
| *.egg-info/ | ||
| dist/ | ||
| build/ | ||
|
|
||
| # Virtual Environment | ||
| venv/ | ||
| env/ | ||
| ENV/ | ||
|
|
||
| # Testing | ||
| .pytest_cache/ | ||
| .coverage | ||
| htmlcov/ | ||
|
|
||
| # Flask | ||
| instance/ | ||
| .webassets-cache | ||
| flask.log | ||
|
|
||
| # Progress data | ||
| progress.json | ||
|
|
||
| # IDE | ||
| .vscode/ | ||
| .idea/ | ||
| *.swp | ||
| *.swo | ||
| *~ | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db |
This file contains hidden or 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,0 +1,87 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from flask import Flask, jsonify, request | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import json | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import threading | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app = Flask(__name__) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # In-memory storage for progress data | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progress_data = [] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Thread lock for synchronization | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progress_lock = threading.Lock() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # File path for persistence | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PROGRESS_FILE = 'progress.json' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Load progress from file if it exists | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def load_progress(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| global progress_data | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if os.path.exists(PROGRESS_FILE): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(PROGRESS_FILE, 'r', encoding='utf-8') as f: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progress_data = json.load(f) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Error loading progress: {e}") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progress_data = [] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Save progress to file | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def save_progress(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(PROGRESS_FILE, 'w', encoding='utf-8') as f: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json.dump(progress_data, f, ensure_ascii=False, indent=2) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Error saving progress: {e}") | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Load progress on startup | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| load_progress() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @app.route('/') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def index(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Top page route""" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonify({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": "Welcome to the Progress API", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "endpoints": { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "/": "This page", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "/api/progress": "GET - Retrieve progress data, POST - Save progress data" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @app.route('/api/progress', methods=['GET']) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_progress(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Get progress data""" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with progress_lock: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonify({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": True, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "data": progress_data.copy() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @app.route('/api/progress', methods=['POST']) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def post_progress(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Save progress data""" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data = request.get_json() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not data: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonify({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": False, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "error": "No data provided" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with progress_lock: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progress_data.append(data) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| save_progress() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonify({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": True, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "message": "Progress saved successfully", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "data": data | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonify({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "success": False, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "error": str(e) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), 500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+81
to
+84
Check warningCode scanning / CodeQL Information exposure through an exception Medium Stack trace information Error loading related location Loading
Copilot AutofixAI 9 months ago To fix the issue, we should avoid returning the actual exception message
Suggested changeset
1
main.py
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.run(debug=False, host='0.0.0.0', port=5000) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or 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 @@ | ||
| Flask==3.0.0 | ||
| pytest==7.4.3 |
This file contains hidden or 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,134 @@ | ||
| import pytest | ||
| import json | ||
| import os | ||
| from main import app, progress_data, PROGRESS_FILE | ||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| """Create a test client for the Flask app""" | ||
| app.config['TESTING'] = True | ||
| with app.test_client() as client: | ||
| yield client | ||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def cleanup(): | ||
| """Clean up progress data before and after each test""" | ||
| # Clear in-memory data | ||
| progress_data.clear() | ||
|
|
||
| # Remove progress file if it exists | ||
| if os.path.exists(PROGRESS_FILE): | ||
| os.remove(PROGRESS_FILE) | ||
|
|
||
| yield | ||
|
|
||
| # Clean up after test | ||
| progress_data.clear() | ||
| if os.path.exists(PROGRESS_FILE): | ||
| os.remove(PROGRESS_FILE) | ||
|
|
||
| def test_index_route(client): | ||
| """Test the index (/) route""" | ||
| response = client.get('/') | ||
| assert response.status_code == 200 | ||
| data = json.loads(response.data) | ||
| assert 'message' in data | ||
| assert 'endpoints' in data | ||
| assert data['message'] == "Welcome to the Progress API" | ||
|
|
||
| def test_get_progress_empty(client): | ||
| """Test GET /api/progress with no data""" | ||
| response = client.get('/api/progress') | ||
| assert response.status_code == 200 | ||
| data = json.loads(response.data) | ||
| assert data['success'] is True | ||
| assert data['data'] == [] | ||
|
|
||
| def test_post_progress_valid(client): | ||
| """Test POST /api/progress with valid data""" | ||
| test_data = { | ||
| "task": "Implement Flask API", | ||
| "status": "completed", | ||
| "percentage": 100 | ||
| } | ||
|
|
||
| response = client.post('/api/progress', | ||
| data=json.dumps(test_data), | ||
| content_type='application/json') | ||
|
|
||
| assert response.status_code == 201 | ||
| data = json.loads(response.data) | ||
| assert data['success'] is True | ||
| assert data['message'] == "Progress saved successfully" | ||
| assert data['data'] == test_data | ||
|
|
||
| def test_post_progress_no_data(client): | ||
| """Test POST /api/progress with no data""" | ||
| response = client.post('/api/progress', | ||
| data=json.dumps(None), | ||
| content_type='application/json') | ||
|
|
||
| assert response.status_code == 400 | ||
| data = json.loads(response.data) | ||
| assert data['success'] is False | ||
| assert 'error' in data | ||
|
|
||
| def test_get_progress_after_post(client): | ||
| """Test GET /api/progress after posting data""" | ||
| test_data = { | ||
| "task": "Write tests", | ||
| "status": "in progress", | ||
| "percentage": 50 | ||
| } | ||
|
|
||
| # Post data | ||
| client.post('/api/progress', | ||
| data=json.dumps(test_data), | ||
| content_type='application/json') | ||
|
|
||
| # Get data | ||
| response = client.get('/api/progress') | ||
| assert response.status_code == 200 | ||
| data = json.loads(response.data) | ||
| assert data['success'] is True | ||
| assert len(data['data']) == 1 | ||
| assert data['data'][0] == test_data | ||
|
|
||
| def test_multiple_progress_entries(client): | ||
| """Test posting multiple progress entries""" | ||
| entries = [ | ||
| {"task": "Task 1", "status": "completed"}, | ||
| {"task": "Task 2", "status": "in progress"}, | ||
| {"task": "Task 3", "status": "pending"} | ||
| ] | ||
|
|
||
| for entry in entries: | ||
| response = client.post('/api/progress', | ||
| data=json.dumps(entry), | ||
| content_type='application/json') | ||
| assert response.status_code == 201 | ||
|
|
||
| # Verify all entries are stored | ||
| response = client.get('/api/progress') | ||
| data = json.loads(response.data) | ||
| assert len(data['data']) == 3 | ||
| assert data['data'] == entries | ||
|
|
||
| def test_progress_persistence(client): | ||
| """Test that progress data persists to file""" | ||
| test_data = {"task": "Test persistence", "status": "saved"} | ||
|
|
||
| # Post data | ||
| client.post('/api/progress', | ||
| data=json.dumps(test_data), | ||
| content_type='application/json') | ||
|
|
||
| # Check that file was created | ||
| assert os.path.exists(PROGRESS_FILE) | ||
|
|
||
| # Read file and verify content | ||
| with open(PROGRESS_FILE, 'r', encoding='utf-8') as f: | ||
| file_data = json.load(f) | ||
|
|
||
| assert len(file_data) == 1 | ||
| assert file_data[0] == test_data |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.