Initial Commit
This commit is contained in:
commit
14d041c332
10 changed files with 1973 additions and 0 deletions
198
.gitignore
vendored
Normal file
198
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,198 @@
|
||||||
|
# Database files
|
||||||
|
*.db
|
||||||
|
|
||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode
|
||||||
|
|
||||||
|
### Python ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
#pdm.lock
|
||||||
|
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||||
|
# in version control.
|
||||||
|
# https://pdm.fming.dev/#use-with-ide
|
||||||
|
.pdm.toml
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
### Python Patch ###
|
||||||
|
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
||||||
|
poetry.toml
|
||||||
|
|
||||||
|
# ruff
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
|
# LSP config files
|
||||||
|
pyrightconfig.json
|
||||||
|
|
||||||
|
### VisualStudioCode ###
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
||||||
|
!.vscode/*.code-snippets
|
||||||
|
|
||||||
|
# Local History for Visual Studio Code
|
||||||
|
.history/
|
||||||
|
|
||||||
|
# Built Visual Studio Code Extensions
|
||||||
|
*.vsix
|
||||||
|
|
||||||
|
### VisualStudioCode Patch ###
|
||||||
|
# Ignore all local history of files
|
||||||
|
.history
|
||||||
|
.ionide
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 Emma Shoup
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
0
README.md
Normal file
0
README.md
Normal file
1482
poetry.lock
generated
Normal file
1482
poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
28
pyproject.toml
Normal file
28
pyproject.toml
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
[project]
|
||||||
|
name = "uptime-pie-api"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = [
|
||||||
|
{name = "Emma Shoup",email = "emma@shoup.io"}
|
||||||
|
]
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
dependencies = [
|
||||||
|
"fastapi[standard] (>=0.115.12,<0.116.0)",
|
||||||
|
"apscheduler (>=3.11.0,<4.0.0)",
|
||||||
|
"sqlmodel (>=0.0.24,<0.0.25)"
|
||||||
|
]
|
||||||
|
license = "MIT"
|
||||||
|
|
||||||
|
[tool.poetry]
|
||||||
|
packages = [{include = "uptime_pie_api", from = "src"}]
|
||||||
|
|
||||||
|
|
||||||
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
pytest = "^8.3.5"
|
||||||
|
black = "^25.1.0"
|
||||||
|
httpx = "^0.28.1"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
||||||
0
src/uptime_py_api/__init__.py
Normal file
0
src/uptime_py_api/__init__.py
Normal file
123
src/uptime_py_api/main.py
Normal file
123
src/uptime_py_api/main.py
Normal file
|
|
@ -0,0 +1,123 @@
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from typing import Annotated
|
||||||
|
from secrets import token_urlsafe
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Depends, HTTPException
|
||||||
|
from fastapi.responses import PlainTextResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlmodel import Field, SQLModel, create_engine, Session, select
|
||||||
|
|
||||||
|
|
||||||
|
description = """
|
||||||
|
Uptime Pie API does awesome stuff.
|
||||||
|
"""
|
||||||
|
|
||||||
|
tags_metadata = [{"name": "agents", "description": "Operations with agents."}]
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
create_db_and_tables()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title="Uptime Pie API",
|
||||||
|
description=description,
|
||||||
|
license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"},
|
||||||
|
openapi_tags=tags_metadata,
|
||||||
|
lifespan=lifespan,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
sqlite_file_name = "database.db"
|
||||||
|
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||||
|
|
||||||
|
connect_args = {"check_same_thread": False}
|
||||||
|
engine = create_engine(sqlite_url, connect_args=connect_args)
|
||||||
|
|
||||||
|
|
||||||
|
def create_db_and_tables():
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
|
|
||||||
|
def get_session():
|
||||||
|
with Session(engine) as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
SessionDep = Annotated[Session, Depends(get_session)]
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/ping",
|
||||||
|
response_class=PlainTextResponse,
|
||||||
|
summary="Ping the API",
|
||||||
|
responses={
|
||||||
|
200: {
|
||||||
|
"content": {"text/plain": {"schema": {"type": "string", "example": "OK"}}}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def read_main():
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
class Agent(SQLModel, table=True):
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
name: str = Field(unique=True)
|
||||||
|
api_key: str = Field()
|
||||||
|
|
||||||
|
|
||||||
|
class AgentCreateModel(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(
|
||||||
|
"/agents",
|
||||||
|
tags=["agents"],
|
||||||
|
responses={400: {"description": "Agent already exists"}},
|
||||||
|
)
|
||||||
|
async def create_agent(agent: AgentCreateModel, session: SessionDep) -> Agent:
|
||||||
|
agent_exists = session.exec(select(Agent).where(Agent.name == agent.name)).first()
|
||||||
|
if agent_exists:
|
||||||
|
raise HTTPException(status_code=400, detail="Agent already exists")
|
||||||
|
|
||||||
|
new_agent = Agent(name=agent.name, api_key=token_urlsafe(32))
|
||||||
|
session.add(new_agent)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(new_agent)
|
||||||
|
return new_agent
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/agents", tags=["agents"])
|
||||||
|
async def get_agents(session: SessionDep) -> list[Agent]:
|
||||||
|
agents = session.exec(select(Agent)).all()
|
||||||
|
return agents # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/agents/{agent_id}",
|
||||||
|
tags=["agents"],
|
||||||
|
responses={404: {"description": "Agent not found"}},
|
||||||
|
)
|
||||||
|
async def get_agent(agent_id: UUID, session: SessionDep):
|
||||||
|
agent = session.get(Agent, agent_id)
|
||||||
|
if not agent:
|
||||||
|
raise HTTPException(status_code=404, detail="Agent not found")
|
||||||
|
return agent
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete(
|
||||||
|
"/agents/{agent_id}",
|
||||||
|
tags=["agents"],
|
||||||
|
responses={404: {"description": "Agent not found"}},
|
||||||
|
)
|
||||||
|
async def delete_agent(agent_id: UUID, session: SessionDep):
|
||||||
|
agent = session.get(Agent, agent_id)
|
||||||
|
if not agent:
|
||||||
|
raise HTTPException(status_code=404, detail="Agent not found")
|
||||||
|
session.delete(agent)
|
||||||
|
session.commit()
|
||||||
|
return {"message": "Agent deleted"}
|
||||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
27
tests/conftest.py
Normal file
27
tests/conftest.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
|
from sqlmodel.pool import StaticPool
|
||||||
|
|
||||||
|
from uptime_pie_api.main import app, get_session
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="session")
|
||||||
|
def session_fixture():
|
||||||
|
engine = create_engine(
|
||||||
|
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||||
|
)
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
with Session(engine) as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="client")
|
||||||
|
def client_fixture(session: Session):
|
||||||
|
def get_session_override():
|
||||||
|
return session
|
||||||
|
|
||||||
|
app.dependency_overrides[get_session] = get_session_override
|
||||||
|
client = TestClient(app)
|
||||||
|
yield client
|
||||||
|
app.dependency_overrides.clear()
|
||||||
94
tests/test_main.py
Normal file
94
tests/test_main.py
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from sqlmodel import Session
|
||||||
|
|
||||||
|
from uptime_pie_api.main import Agent
|
||||||
|
|
||||||
|
|
||||||
|
uuid_re = re.compile(
|
||||||
|
r"^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$",
|
||||||
|
flags=re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ping(client: TestClient):
|
||||||
|
response = client.get("/ping")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.text == "OK"
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_agents(client: TestClient):
|
||||||
|
payload = {"name": "test_agent"}
|
||||||
|
response = client.post("/agents", content=json.dumps(payload))
|
||||||
|
json_response = response.json()
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json_response["name"] == "test_agent"
|
||||||
|
assert uuid_re.match(json_response["id"])
|
||||||
|
assert json_response["api_key"] is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_create_agent(client: TestClient):
|
||||||
|
payload = {"name": "test_agent", "api_key": "test_api_key"}
|
||||||
|
response = client.post("/agents", content=json.dumps(payload))
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
response = client.post("/agents", content=json.dumps(payload))
|
||||||
|
json_response = response.json()
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert json_response["detail"] == "Agent already exists"
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_agents(client: TestClient, session: Session):
|
||||||
|
session.add(Agent(name="test_agent", api_key="test_api_key"))
|
||||||
|
session.add(Agent(name="test_agent2", api_key="test_api_key2"))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
response = client.get("/agents")
|
||||||
|
json_response = response.json()
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert len(json_response) == 2
|
||||||
|
assert json_response[0]["name"] == "test_agent"
|
||||||
|
assert json_response[1]["name"] == "test_agent2"
|
||||||
|
assert uuid_re.match(json_response[0]["id"])
|
||||||
|
assert uuid_re.match(json_response[1]["id"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_agent(client: TestClient, session: Session):
|
||||||
|
test_agent = Agent(name="test_agent", api_key="test_api_key")
|
||||||
|
session.add(test_agent)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(test_agent)
|
||||||
|
|
||||||
|
response = client.get(f"/agents/{test_agent.id}")
|
||||||
|
json_response = response.json()
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json_response["name"] == "test_agent"
|
||||||
|
assert json_response["id"] == str(test_agent.id)
|
||||||
|
assert json_response["api_key"] == "test_api_key"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_agent_not_found(client: TestClient):
|
||||||
|
response = client.get("/agents/00000000-aaaa-1111-bbbb-222222222222")
|
||||||
|
json_response = response.json()
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
assert json_response["detail"] == "Agent not found"
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_agent(client: TestClient, session: Session):
|
||||||
|
test_agent = Agent(name="test_agent", api_key="test_api_key")
|
||||||
|
session.add(test_agent)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(test_agent)
|
||||||
|
|
||||||
|
response = client.delete(f"/agents/{test_agent.id}")
|
||||||
|
json_response = response.json()
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json_response["message"] == "Agent deleted"
|
||||||
|
assert session.get(Agent, test_agent.id) is None
|
||||||
Loading…
Add table
Add a link
Reference in a new issue