Skip to content

Improved api #2

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

Merged
merged 24 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .devcontainer/Dockerfile_dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mcr.microsoft.com/devcontainers/python:3.12-bullseye

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*


COPY requirements-dev.txt requirements-dev.txt
COPY src/requirements.txt src/requirements.txt
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements-dev.txt
51 changes: 51 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
"name": "flask_api_sqlite_db",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": "docker-compose_dev.yml",
// The 'service' property is the name of the service for the container that VS Code should
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
"service": "app",
"shutdownAction": "stopCompose",
"workspaceFolder": "/workspace",
"forwardPorts": [5000, 3306],
"portsAttributes": {
"5000": {"label": "Web port", "onAutoForward": "notify"}
},
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-bicep",
"charliermarsh.ruff",
"ms-python.python",
"bierner.github-markdown-preview"
],
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"files.exclude": {
".coverage": true,
".pytest_cache": true,
"__pycache__": true,
".ruff_cache": true
},
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
}
}
}
}
},
"features": {
"ghcr.io/azure/azure-dev/azd:latest": {}
},
"postCreateCommand": "pip install -e src && python3 -m flask --app src/flaskapp db upgrade --directory src/flaskapp/migrations"
}
8 changes: 8 additions & 0 deletions .devcontainer/docker-compose_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
app:
build:
context: ..
dockerfile: ./.devcontainer/Dockerfile_dev

command: sleep infinity
30 changes: 30 additions & 0 deletions .github/workflows/devcontaienr-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Check Dev Container

on:
push:
branches: [ 'main', 'improved-api']
paths:
- ".devcontainer/**"
- ".github/workflows/devcontainer-ci.yaml"
pull_request:
branches: [ 'main', 'improved-api' ]
paths:
- ".devcontainer/**"
- ".github/workflows/devcontainer-ci.yaml"

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 20.x
- run: npm install -g @devcontainers/cli
- run: devcontainer build --config ./.devcontainer/devcontainer.json --workspace-folder "$(pwd)"
32 changes: 32 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Run Python linter and formatter

on:
push:
branches: [ 'main' ]
paths:
- '**.py'

pull_request:
branches: [ 'main' ]
paths:
- '**.py'

jobs:
checks-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Check linting with ruff
run: |
ruff check .
- name: Check formatting with black
run: |
black . --verbose
45 changes: 45 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Run Python tests

on:
push:
branches: [ 'main' ]
paths:
- '**.py'

pull_request:
branches: [ 'main' ]
paths:
- '**.py'

jobs:
test_package:

name: Test ${{ matrix.os }} Python ${{ matrix.python_version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-20.04"]
python_version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Setup python
uses: actions/setup-python@v2
with:

python-version: ${{ matrix.python_version }}
architecture: x64
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-dev.txt
python3 -m pip install -e src
- name: Run the migrations
run: |
python3 -m flask --app src.flaskapp db upgrade --directory src/flaskapp/migrations
env:
DATABASE_FILENAME: testdb.db
- name: Run tests
run: python3 -m pytest
env:
DATABASE_FILENAME: testdb.db
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exclude: '^tests/snapshots/'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 24.1.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
types_or: [css, javascript, ts, tsx, html]
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,40 +12,58 @@ To run the Flask application, follow these steps:
cd flask-api-sqlite-db
```

1. **Install, initialize and activate a virtualenv using:**
1. **Initialize and activate a virtualenv using:**

```bash
pip install virtualenv
python -m virtualenv venv
source venv/bin/activate
python3 -m venv .venv
source .venv/bin/activate
```

>**Note** - In Windows, the `venv` does not have a `bin` directory. Therefore, you'd use the analogous command shown below:
```bash
source venv\Scripts\activate
source .venv\Scripts\activate
```
1. **Install the dependencies:**
1. **Install the app as an editable package:**
```bash
pip install -r requirements.txt
python3 -m pip install -e src
```
1. **Execute the following command in your terminal to start the flask app**
1. **Execute the following command to add the database name and apply the migrations:**
```bash
export DATABASE_FILENAME=testdb.db
export FLASK_APP=src.app
export FLASK_ENV=development
flask run --reload
python3 -m flask --app src.flaskapp db upgrade --directory src/flaskapp/migrations
```
### Run the tests
1. **Inside your virtual environment, execute the following command to run the tests**
1. **Execute the following command to run the flask application:**
```bash
python flask_test.py
python3 -m flask --app src.flaskapp run --reload
```
### Development
1. **Inside your virtual environment, execute the following command to install the development requirements:**
```bash
pip install -r requirements-dev.txt
```
1. **Execute the following command to install the pre commit hooks:**
```bash
pre-commit install
```
### Testing
1. **Execute the following command to run the tests**
```bash
pytest
```
## API Documentation
@@ -208,5 +226,3 @@ The API will return these error types when the request fails:
"total_executions": 10
}
```
Loading