From a683fceb10a0eda73b5d28415cb7a9bd96dbd0b0 Mon Sep 17 00:00:00 2001
From: Anson
Date: Fri, 23 May 2025 13:53:40 -0600
Subject: [PATCH] get everything working together kinda
---
pyproject.toml | 17 +++--------------
src/test/test_api.py | 17 ++++++++++-------
src/two_chainz/__init__.py | 25 ++++++++++++++++++++++---
website/index.html | 16 ++++++++--------
4 files changed, 43 insertions(+), 32 deletions(-)
diff --git a/pyproject.toml b/pyproject.toml
index e3672f6..3f2c409 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -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"]
diff --git a/src/test/test_api.py b/src/test/test_api.py
index 043c2c8..6eed839 100644
--- a/src/test/test_api.py
+++ b/src/test/test_api.py
@@ -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
diff --git a/src/two_chainz/__init__.py b/src/two_chainz/__init__.py
index 57b7b9d..f5392e6 100644
--- a/src/two_chainz/__init__.py
+++ b/src/two_chainz/__init__.py
@@ -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")
diff --git a/website/index.html b/website/index.html
index 9a92b08..6d445be 100644
--- a/website/index.html
+++ b/website/index.html
@@ -109,8 +109,8 @@
send a get
request to
https://chainz-rest.azurewebsites.net/quotehttps://chainz.ansonbiggs.com/api/quote
@@ -132,8 +132,8 @@
>
and is subject to change. An example return from
- https://chainz-rest.azurewebsites.net/quote?batch=2https://chainz.ansonbiggs.com/api/quote?batch=2
@@ -160,8 +160,8 @@
send a get
request to
https://chainz-rest.azurewebsites.net/aliashttps://chainz.ansonbiggs.com/api/alias
@@ -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;