Skip to content

Added litestar API endpoints#36

Open
NirrWorks wants to merge 5 commits intoosgeonepal:masterfrom
NirrWorks:api/litestar-endpoints
Open

Added litestar API endpoints#36
NirrWorks wants to merge 5 commits intoosgeonepal:masterfrom
NirrWorks:api/litestar-endpoints

Conversation

@NirrWorks
Copy link
Copy Markdown

Summary

Adds an initial Litestar API inside the osmsg/api package.

This includes two active endpoints for now:

  • GET /health
  • GET /api/v1/users

The API uses asyncpg to query PostgreSQL and reads the database connection from the DATABASE_URL environment variable.

Changes

  • Added osmsg/api/ package with Litestar app, DB pool setup, and users query.
  • Added /health endpoint for API health checks.
  • Added /api/v1/users endpoint to return ranked user statistics from the current osmsg PostgreSQL tables.
  • Kept future stats endpoints commented out for now.
  • Added Procfile for deployment.
  • Added API dependencies: litestar[standard], asyncpg, and uv.
  • Added route tests for the active API endpoints.

Notes

The API currently expects osmsg data to already be exported into PostgreSQL using the current osmsg schema:

  • users
  • changesets
  • changeset_stats
  • state

Example local run:

uv run litestar --app osmsg.api.app:app run --host 0.0.0.0 --port 8000

Comment thread osmsg/api/app.py Outdated


@get("/api/v1/users")
async def get_users(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_user_stats
takes start end and hashtag as parameter , returns the user stats !

Comment thread tests/test_api.py
from osmsg.api.app import app


def test_api_exposes_only_active_public_routes():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert test cases with actual api expected request and response !

Comment thread pyproject.toml Outdated
license-files = ["LICENSE"]
requires-python = ">=3.11"
dependencies = [
"asyncpg>=0.30.0",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add api group and put those inside, uv is not needed in the deps !

Copy link
Copy Markdown
Collaborator

@kshitijrajsharma kshitijrajsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the slim PR , beautiful work !
few points ;
add .env example !
add instructions to quickly test the api and the readme , to insert the data and get the api up , usually should be two commands ! expand test cases for real test assertion

@NirrWorks
Copy link
Copy Markdown
Author

Updated. I added:

  • .env.example with DATABASE_URL
  • README instructions for inserting data into Postgres and starting the API
  • two-command quick test flow:
    • uv run osmsg --last day --format psql --psql-dsn "$DATABASE_URL" --name api_last_day
    • uv run --group api litestar --app osmsg.api.app:app run --host 0.0.0.0 --port 8000
  • expanded API tests with real TestClient requests and expected JSON response assertions

@NirrWorks
Copy link
Copy Markdown
Author

The API is now hosted on Render and connected to a Neon Postgres database.

Health endpoint:
https://osmsg-1.onrender.com/health

User stats endpoint:
https://osmsg-1.onrender.com/api/v1/user-stats?start=2026-05-02T00:00:00Z&end=2026-05-03T00:00:00Z&limit=10

The database was populated using:
uv run osmsg --last day --summary --format psql --psql-dsn "$DATABASE_URL" --name api_last_day

Comment thread osmsg/api/app.py
@@ -0,0 +1,71 @@
from __future__ import annotations
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed , this was only needed for the tools that supports <=3.11

Comment thread osmsg/api/app.py
async def get_user_stats(
start: datetime,
end: datetime,
hashtag: str | None = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support list[str]

Comment thread osmsg/api/db.py
@@ -0,0 +1,36 @@
from __future__ import annotations
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here , remove

Comment thread osmsg/api/queries.py
limit: int = 100,
offset: int = 0,
) -> list[dict[str, Any]]:
sql = """
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to see if sql can be parametrized safely with the sql formatting !

Comment thread pyproject.toml
"litestar[standard]>=2.18.0",
]
dev = [
{include-group = "api"},
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev doesn't need api ! api needs to be explicit

Copy link
Copy Markdown
Collaborator

@kshitijrajsharma kshitijrajsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few minor changes !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Under Review

Development

Successfully merging this pull request may close these issues.

2 participants