Skip to content
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

[NEW] Pre commit #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
files: src

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: check-ast
- id: check-yaml
- id: check-json
- id: check-case-conflict
- id: detect-private-key
- id: fix-encoding-pragma
args: [--remove]
- id: forbid-submodules
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-byte-order-marker
- id: check-symlinks
- id: check-merge-conflict
- id: check-added-large-files
args: [--maxkb=1024]
- id: end-of-file-fixer
- id: double-quote-string-fixer

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.11.1
hooks:
- id: isort

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: [--prose-wrap=always, --write]
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ Test out the following routes:
1. [http://localhost:8002/ping](http://localhost:8002/ping)
1. [http://localhost:8002/docs](http://localhost:8002/docs)
1. [http://localhost:8002/notes](http://localhost:8002/notes)


## Want to use pre-commit?
For using the pre-commit, you should go inside of the `src` folder:
```bash
# Create the environment
python3 -m venv venv

# Activate the environment
source env/bin/activate

# Install pre-commit
pip3 install pre-commit
```
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 126
extend-ignore = E203, W503, E501, E741, F841, F401, F821
2 changes: 1 addition & 1 deletion src/app/api/crud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.api.models import NoteSchema
from app.db import notes, database
from app.db import database, notes


async def post(payload: NoteSchema):
Expand Down
28 changes: 14 additions & 14 deletions src/app/api/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,52 @@
router = APIRouter()


@router.post("/", response_model=NoteDB, status_code=201)
@router.post('/', response_model=NoteDB, status_code=201)
async def create_note(payload: NoteSchema):
note_id = await crud.post(payload)

response_object = {
"id": note_id,
"title": payload.title,
"description": payload.description,
'id': note_id,
'title': payload.title,
'description': payload.description,
}
return response_object


@router.get("/{id}/", response_model=NoteDB)
@router.get('/{id}/', response_model=NoteDB)
async def read_note(id: int = Path(..., gt=0),):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail="Note not found")
raise HTTPException(status_code=404, detail='Note not found')
return note


@router.get("/", response_model=List[NoteDB])
@router.get('/', response_model=List[NoteDB])
async def read_all_notes():
return await crud.get_all()


@router.put("/{id}/", response_model=NoteDB)
@router.put('/{id}/', response_model=NoteDB)
async def update_note(payload: NoteSchema, id: int = Path(..., gt=0),):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail="Note not found")
raise HTTPException(status_code=404, detail='Note not found')

note_id = await crud.put(id, payload)

response_object = {
"id": note_id,
"title": payload.title,
"description": payload.description,
'id': note_id,
'title': payload.title,
'description': payload.description,
}
return response_object


@router.delete("/{id}/", response_model=NoteDB)
@router.delete('/{id}/', response_model=NoteDB)
async def delete_note(id: int = Path(..., gt=0)):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail="Note not found")
raise HTTPException(status_code=404, detail='Note not found')

await crud.delete(id)

Expand Down
4 changes: 2 additions & 2 deletions src/app/api/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
router = APIRouter()


@router.get("/ping")
@router.get('/ping')
async def pong():
# some async operation could happen here
# example: `notes = await get_all_notes()`
return {"ping": "pong!"}
return {'ping': 'pong!'}
26 changes: 9 additions & 17 deletions src/app/db.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import os

from sqlalchemy import (
Column,
DateTime,
Integer,
MetaData,
String,
Table,
create_engine
)
from sqlalchemy.sql import func

from databases import Database
from sqlalchemy import (Column, DateTime, Integer, MetaData, String, Table,
create_engine)
from sqlalchemy.sql import func

DATABASE_URL = os.getenv("DATABASE_URL")
DATABASE_URL = os.getenv('DATABASE_URL')

# SQLAlchemy
engine = create_engine(DATABASE_URL)
metadata = MetaData()
notes = Table(
"notes",
'notes',
metadata,
Column("id", Integer, primary_key=True),
Column("title", String(50)),
Column("description", String(50)),
Column("created_date", DateTime, default=func.now(), nullable=False),
Column('id', Integer, primary_key=True),
Column('title', String(50)),
Column('description', String(50)),
Column('created_date', DateTime, default=func.now(), nullable=False),
)

# databases query builder
Expand Down
6 changes: 3 additions & 3 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
app = FastAPI()


@app.on_event("startup")
@app.on_event('startup')
async def startup():
await database.connect()


@app.on_event("shutdown")
@app.on_event('shutdown')
async def shutdown():
await database.disconnect()


app.include_router(ping.router)
app.include_router(notes.router, prefix="/notes", tags=["notes"])
app.include_router(notes.router, prefix='/notes', tags=['notes'])
10 changes: 5 additions & 5 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
databases[postgresql]==0.6.2
psycopg2-binary==2.9.5
SQLAlchemy==1.4.41
asyncpg==0.27.0
databases[postgresql]==0.6.2
fastapi==0.87.0
uvicorn==0.20.0
httpx==0.23.1
psycopg2-binary==2.9.5

# dev
pytest==7.2.0
httpx==0.23.1
SQLAlchemy==1.4.41
uvicorn==0.20.0
2 changes: 1 addition & 1 deletion src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app.main import app


@pytest.fixture(scope="module")
@pytest.fixture(scope='module')
def test_app():
client = TestClient(app)
yield client # testing happens here
Loading