Skip to content

Commit 87852d5

Browse files
committed
feat: init api
1 parent e34cb35 commit 87852d5

File tree

13 files changed

+162
-5
lines changed

13 files changed

+162
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ __pycache__
77

88
node_modules/
99

10-
./data
10+
/data
1111

1212
*.DS_Store

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,30 @@
22

33
This project aims to create an Web App and API to display and make available the Cycle Count Data from the West Midlands Vivacity API
44

5-
mkcert -cert-file dev.cert -key-file dev.pem "cyclecounter.localhost" "*.cyclecounter.localhost"
5+
## Development
6+
7+
### Setup
8+
9+
- Generate certificates using [mkcert](https://github.com/FiloSottile/mkcert):
10+
11+
cd traefik/certs
12+
mkcert -cert-file dev.cert -key-file dev.pem "cyclecounter.localhost" "*.cyclecounter.localhost"
13+
14+
- Create a `.env` file using the `example.env` template:
15+
16+
cp example.env .env
17+
18+
<!-- You *will* need to fill in any missing fields, such as the OAuth scope stuff
19+
for both Twitter and Discord. -->
20+
21+
- Download and build docker image dependencies:
22+
23+
INFRA=dev make pull
24+
INFRA=dev make build
25+
26+
- Launch application:
27+
28+
INFRA=dev make up
29+
30+
The infrastructure should now be running at `cyclecounter.localhost`, make sure that
31+
this resolves to `127.0.0.1`!

api/API/crud.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from datetime import datetime
2+
import uuid
3+
4+
from sqlalchemy.orm import Session
5+
from sqlalchemy import or_,and_
6+
7+
from . import config, models
8+
from typing import Any, Dict, List, Optional, Tuple, Union
9+
10+
def read_counters(db: Session, limit_offset: Tuple[int, int]):
11+
limit, offset = limit_offset
12+
counters = db.query(models.Counter).offset(offset).limit(limit).all()
13+
return counters
14+
15+
def create_counter(db:Session, name:str, lat:float,lon:float,location_desc:str)->models.Counter:
16+
print("Creating Counter")
17+
db_submission = models.Counter(
18+
name=name,
19+
lat=lat,
20+
lon=lon,
21+
location_desc=location_desc)
22+
23+
db.add(db_submission)
24+
db.commit()
25+
db.refresh(db_submission)
26+
return db_submission

api/API/db.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.ext.declarative import declarative_base
3+
from sqlalchemy.orm import sessionmaker
4+
5+
from .config import DatabaseURL
6+
7+
print("db.py init")
8+
engine = create_engine(DatabaseURL)
9+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
10+
11+
Base = declarative_base()

api/API/dependencies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .db import SessionLocal
2+
3+
4+
def get_db():
5+
db = SessionLocal()
6+
try:
7+
yield db
8+
finally:
9+
db.close()

api/API/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
from fastapi.openapi.utils import get_openapi
44

55
from . import config
6-
# from .routers import report,crossings
6+
from .routers import counts,admin
7+
from .db import engine
8+
from . import config, models
9+
10+
models.Base.metadata.create_all(bind=engine)
711

812
app = FastAPI(root_path="/api",title="WMCycleCounter")
9-
print("starting")
1013
app.add_middleware(SessionMiddleware, secret_key=config.SessionSecret)
14+
15+
app.include_router(counts.router)
16+
app.include_router(admin.router)
1117
# openapi_schema = get_openapi(title="BadlyParked",version="1.0.0",routes=app.routes,description="This is a very custom OpenAPI schema")
1218
# app.openapi_schema = openapi_schema
1319
# app.openapi = app.openapi_schema

api/API/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .db import Base, engine
2+
import uuid
3+
from sqlalchemy.dialects.postgresql import JSONB, UUID
4+
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String,Float,Integer,BigInteger
5+
6+
class Counter(Base):
7+
__tablename__ = "counters"
8+
name = Column(
9+
String, primary_key=True, index=True, default=uuid.uuid4
10+
)
11+
lat = Column(Float,nullable=False)
12+
lon = Column(Float,nullable=False)
13+
location_desc = Column(String,nullable=True)

api/API/routers/admin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
from fastapi import APIRouter, Depends
3+
from fastapi.responses import Response
4+
from typing import List, Union
5+
from sqlalchemy.orm import Session
6+
from .. import crud, schemas
7+
from ..dependencies import get_db
8+
9+
router = APIRouter()
10+
11+
@router.post("/add_counter/", response_model=schemas.Counter, tags=["admin"])
12+
def add_counter(
13+
response: Response,
14+
name: str,
15+
lat: float,
16+
lon: float,
17+
location_desc: str = "",
18+
db: Session = Depends(get_db),
19+
):
20+
# validate.check_limit(limit)
21+
response.headers["X-Total-Count"] = str(5)
22+
# res = crud.get_submissions(db, (limit, offset))
23+
# print(res[0].time)
24+
# return []
25+
return crud.create_counter(db, name,lat,lon,location_desc)

api/API/routers/counts.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fastapi import APIRouter, Depends
2+
from fastapi.responses import Response
3+
from typing import List, Union
4+
from sqlalchemy.orm import Session
5+
from .. import crud, schemas
6+
from ..dependencies import get_db
7+
8+
router = APIRouter()
9+
10+
@router.get("/counters/", response_model=List[schemas.Counter], tags=["counters"])
11+
def read_counter(
12+
response: Response,
13+
offset: int = 0,
14+
limit: int = 25,
15+
db: Session = Depends(get_db),
16+
):
17+
# validate.check_limit(limit)
18+
response.headers["X-Total-Count"] = str(5)
19+
# res = crud.get_submissions(db, (limit, offset))
20+
# print(res[0].time)
21+
# return []
22+
return crud.read_counters(db, (limit, offset))

api/API/schemas.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pydantic import BaseModel
2+
import uuid
3+
4+
class Counter(BaseModel):
5+
name:str
6+
lat:float
7+
lon:float
8+
location_desc:str

0 commit comments

Comments
 (0)