1
0
mirror of https://gitlab.com/2-chainz/2chainz.git synced 2025-06-15 17:36:39 +00:00

get everything working together kinda

This commit is contained in:
Anson 2025-05-23 13:53:40 -06:00
parent 5687871b86
commit a683fceb10
4 changed files with 43 additions and 32 deletions

View File

@ -3,30 +3,19 @@ name = "2chainz"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Anson", email = "anson@ansonbiggs.com" }
]
authors = [{ name = "Anson", email = "anson@ansonbiggs.com" }]
requires-python = ">=3.13"
dependencies = [
"fastapi[standard]>=0.115.12",
]
dependencies = ["fastapi[standard]>=0.115.12"]
[project.scripts]
two_chainz = "two_chainz:main"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/two_chainz"]
[dependency-groups]
dev = [
"httpx>=0.28.1",
"pytest>=8.3.5",
"ruff>=0.11.11",
]
dev = ["httpx>=0.28.1", "pytest>=8.3.5", "ruff>=0.11.11"]

View File

@ -8,8 +8,8 @@ from two_chainz import app
client = TestClient(app)
class TestApi:
class TestApi:
def test_api_endpoint(self):
response = client.get("/api/")
assert response.status_code == 200
@ -24,12 +24,15 @@ class TestApi:
# Validate timestamp format (ISO format)
assert datetime.fromisoformat(data["timestamp"])
@pytest.mark.parametrize("mocked_time,start_time,expected", [
(100, 50, 50), # 100 - 50 = 50 seconds uptime
(200, 100, 100), # 200 - 100 = 100 seconds uptime
])
@pytest.mark.parametrize(
"mocked_time,start_time,expected",
[
(100, 50, 50), # 100 - 50 = 50 seconds uptime
(200, 100, 100), # 200 - 100 = 100 seconds uptime
],
)
def test_api_uptime_calculation(self, mocked_time, start_time, expected):
with patch('time.time', return_value=mocked_time):
with patch('two_chainz.start_time', start_time):
with patch("time.time", return_value=mocked_time):
with patch("two_chainz.start_time", start_time):
response = client.get("/api/")
assert response.json()["uptime_seconds"] == expected

View File

@ -2,13 +2,22 @@ import time
from datetime import datetime
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import tomllib
from pathlib import Path
import random
app = FastAPI()
start_time = time.time()
def read_data() -> dict[str, str]:
raw_data = tomllib.loads(Path("data.toml").read_text())
raw_data['aliases'] = [alias['name'] for alias in raw_data['aliases'] for _ in range(alias['weight'])]
return raw_data
data = read_data()
@app.get("/api/")
async def ping():
return {
@ -18,5 +27,15 @@ async def ping():
}
@app.get("/api/quote")
async def quote():
return {"quote": random.choice(data['quotes'])}
@app.get("/api/alias")
async def alias():
return {"alias": random.choice(data['aliases'])}
# Mount static files
app.mount("/", StaticFiles(directory="website", html=True), name="static")

View File

@ -109,8 +109,8 @@
<p>
send a <code>get</code> request to
<code
><a href="https://chainz-rest.azurewebsites.net/quote"
>https://chainz-rest.azurewebsites.net/quote</a
><a href="https://chainz.ansonbiggs.com/api/quote"
>https://chainz.ansonbiggs.com/api/quote</a
></code
>
</p>
@ -132,8 +132,8 @@
>
and is subject to change. An example return from
<code>
<a href="https://chainz-rest.azurewebsites.net/quote?batch=2"
>https://chainz-rest.azurewebsites.net/quote?batch=2</a
<a href="https://chainz.ansonbiggs.com/api/quote?batch=2"
>https://chainz.ansonbiggs.com/api/quote?batch=2</a
></code
>
</p>
@ -160,8 +160,8 @@
<p>
send a <code>get</code> request to
<code
><a href="https://chainz-rest.azurewebsites.net/alias"
>https://chainz-rest.azurewebsites.net/alias</a
><a href="https://chainz.ansonbiggs.com/api/alias"
>https://chainz.ansonbiggs.com/api/alias</a
></code
>
</p>
@ -275,7 +275,7 @@
getQuote();
function getQuote() {
fetch("https://chainz-rest.azurewebsites.net/quote", { method: "GET" })
fetch("/api/quote", { method: "GET" })
.then((resp) => resp.json())
.then(function (data) {
document.getElementById("quote").innerHTML = data.quote;
@ -288,7 +288,7 @@
).href = `https://twitter.com/intent/tweet?text=${tweet}`;
});
fetch("https://chainz-rest.azurewebsites.net/alias", { method: "GET" })
fetch("/api/alias", { method: "GET" })
.then((resp) => resp.json())
.then(function (data) {
document.getElementById("alias").innerHTML = "- " + data.alias;