generated from worldbank/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to better support usage as module (#61)
* Refactor to better support usage as module * Bump version * Ensure only 1 connection during lambda execution * Fix typing * Rework to lazy-app to make for easier testing * Update path * Cleanup helper for flexibility * Use context manager to close connections * Add tests * Pre-commit * Rework settings * Simplify db setup * Add missing quote and change steps to load data * Fix settings import * Update for fast reloading and how to run API locally * Update README.md Co-authored-by: Anthony Lukach <anthonylukach@gmail.com> --------- Co-authored-by: Zachary Deziel <zachary.deziel@gmail.com>
- Loading branch information
Showing
19 changed files
with
355 additions
and
241 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,3 +1,7 @@ | ||
"""space2stats.""" | ||
|
||
__version__ = "0.1.0" | ||
from .lib import StatsTable | ||
from .settings import Settings | ||
|
||
__all__ = ["StatsTable", "Settings"] | ||
__version__ = "1.0.0" |
This file contains 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
This file contains 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,3 @@ | ||
from .app import build_app | ||
|
||
__all__ = ["build_app"] |
This file contains 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,77 @@ | ||
from contextlib import asynccontextmanager | ||
from typing import Any, Dict, List, Optional | ||
|
||
import boto3 | ||
from asgi_s3_response_middleware import S3ResponseMiddleware | ||
from fastapi import Depends, FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.responses import ORJSONResponse | ||
from starlette.requests import Request | ||
from starlette_cramjam.middleware import CompressionMiddleware | ||
|
||
from ..lib import StatsTable | ||
from .db import close_db_connection, connect_to_db | ||
from .errors import add_exception_handlers | ||
from .schemas import SummaryRequest | ||
from .settings import Settings | ||
|
||
s3_client = boto3.client("s3") | ||
|
||
|
||
def build_app(settings: Optional[Settings] = None) -> FastAPI: | ||
settings = settings or Settings() | ||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
await connect_to_db(app, settings=settings) | ||
yield | ||
await close_db_connection(app) | ||
|
||
app = FastAPI( | ||
default_response_class=ORJSONResponse, | ||
lifespan=lifespan, | ||
) | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
app.add_middleware(CompressionMiddleware) | ||
app.add_middleware( | ||
S3ResponseMiddleware, | ||
s3_bucket_name=settings.S3_BUCKET_NAME, | ||
s3_client=s3_client, | ||
) | ||
|
||
add_exception_handlers(app) | ||
|
||
def stats_table(request: Request): | ||
"""Dependency to generate a per-request connection to stats table""" | ||
with request.app.state.pool.connection() as conn: | ||
yield StatsTable(conn=conn, table_name=settings.PGTABLENAME) | ||
|
||
@app.post("/summary", response_model=List[Dict[str, Any]]) | ||
def get_summary(body: SummaryRequest, table: StatsTable = Depends(stats_table)): | ||
return table.summaries( | ||
body.aoi, | ||
body.spatial_join_method, | ||
body.fields, | ||
body.geometry, | ||
) | ||
|
||
@app.get("/fields", response_model=List[str]) | ||
def fields(table: StatsTable = Depends(stats_table)): | ||
return table.fields() | ||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"message": "Welcome to Space2Stats!"} | ||
|
||
@app.get("/health") | ||
def health(): | ||
return {"status": "ok"} | ||
|
||
return app |
This file contains 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
File renamed without changes.
This file contains 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
This file contains 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,12 @@ | ||
from typing import List, Literal, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from ..types import AoiModel | ||
|
||
|
||
class SummaryRequest(BaseModel): | ||
aoi: AoiModel | ||
spatial_join_method: Literal["touches", "centroid", "within"] | ||
fields: List[str] | ||
geometry: Optional[Literal["polygon", "point"]] = None |
This file contains 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,6 @@ | ||
from ..settings import Settings as DbSettings | ||
|
||
|
||
class Settings(DbSettings): | ||
# Bucket for large responses | ||
S3_BUCKET_NAME: str |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.