Skip to content

Commit fd5b1bd

Browse files
committed
Add make_meta util and update products API
Introduce app.utils.make_meta to centralize creation of response metadata (severity, title, version, base_url, time). Replace duplicated inline meta dicts in app/api/products/products.py and app/api/products/update.py with calls to make_meta. Simplify the update endpoint to return a placeholder indicating CSV upload handling (previous CSV-processing logic was removed). Add tests/tests_make_meta.py to validate meta fields, default BASE_URL, and timestamp format. These changes DRY up metadata creation and make it easier to test and maintain.
1 parent b8ce5e2 commit fd5b1bd

4 files changed

Lines changed: 50 additions & 59 deletions

File tree

app/api/products/products.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
from app import __version__
3+
from app.utils.make_meta import make_meta
34
from fastapi import APIRouter
45
import os, time
56
from app.api.db import get_db_connection
@@ -21,13 +22,5 @@ def root() -> dict:
2122
cur.close()
2223
conn.close()
2324

24-
base_url = os.getenv("BASE_URL", "http://localhost:8000")
25-
epoch = int(time.time() * 1000)
26-
meta = {
27-
"severity": "success",
28-
"title": "Product List",
29-
"version": __version__,
30-
"base_url": base_url,
31-
"time": epoch,
32-
}
25+
meta = make_meta("success", "Product List")
3326
return {"meta": meta, "data": products}

app/api/products/update.py

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,14 @@
22
import os, csv, time
33
from app.api.db import get_db_connection
44
from app import __version__
5+
from app.utils.make_meta import make_meta
56

67
router = APIRouter()
78

89
@router.get("/products/update", status_code=status.HTTP_202_ACCEPTED)
9-
def update_products() -> dict:
10-
"""Process the large big_data.csv file to update products."""
11-
csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/big_data.csv'))
12-
process_csv(csv_path)
13-
base_url = os.getenv("BASE_URL", "http://localhost:8000")
14-
epoch = int(time.time() * 1000)
15-
meta = {
16-
"severity": "info",
17-
"title": "Product update from big_data.csv started",
18-
"version": __version__,
19-
"base_url": base_url,
20-
"time": epoch,
21-
}
22-
return {"meta": meta, "data": {"filename": "big_data.csv"}}
2310

24-
def process_csv(csv_path: str):
25-
conn_gen = get_db_connection()
26-
conn = next(conn_gen)
27-
cur = conn.cursor()
28-
with open(csv_path, newline='') as csvfile:
29-
reader = csv.DictReader(csvfile)
30-
for row in reader:
31-
cur.execute(
32-
"""
33-
UPDATE product SET
34-
Params=%(Params)s,
35-
title=%(desc)s,
36-
UOS=%(UOS)s,
37-
Pack_Description=%(Pack_Description)s,
38-
Hierarchy1=%(Hierarchy1)s,
39-
Hierarchy2=%(Hierarchy2)s,
40-
Hierarchy3=%(Hierarchy3)s,
41-
UOP=%(UOP)s,
42-
sSell1=%(sSell1)s,
43-
sSell2=%(sSell2)s,
44-
sSell3=%(sSell3)s,
45-
sSell4=%(sSell4)s,
46-
sSell5=%(sSell5)s,
47-
pack1=%(pack1)s,
48-
pack2=%(pack2)s,
49-
pack3=%(pack3)s,
50-
pack4=%(pack4)s,
51-
pack5=%(pack5)s,
52-
EAN=%(EAN)s
53-
WHERE item=%(item)s OR EAN=%(EAN)s
54-
""",
55-
row
56-
)
57-
conn.commit()
58-
cur.close()
59-
conn.close()
11+
def update_products() -> dict:
12+
meta = make_meta("info", "Product update from big_data.csv started")
13+
return {"meta": meta, "data": {
14+
"prompt": "We POST a CSV to this endpoint"
15+
}}

app/utils/make_meta.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
import time
3+
from app import __version__
4+
5+
def make_meta(severity: str, title: str) -> dict:
6+
"""Create a standard meta dictionary for API responses."""
7+
base_url = os.getenv("BASE_URL", "http://localhost:8000")
8+
epoch = int(time.time() * 1000)
9+
return {
10+
"severity": severity,
11+
"title": title,
12+
"version": __version__,
13+
"base_url": base_url,
14+
"time": epoch,
15+
}

tests/test_make_meta.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import re
3+
import time
4+
import pytest
5+
from app.utils.make_meta import make_meta
6+
from app import __version__
7+
8+
def test_make_meta_basic(monkeypatch):
9+
monkeypatch.setenv("BASE_URL", "http://testserver:9000")
10+
before = int(time.time() * 1000)
11+
meta = make_meta("info", "Test Title")
12+
after = int(time.time() * 1000)
13+
assert meta["severity"] == "info"
14+
assert meta["title"] == "Test Title"
15+
assert meta["version"] == __version__
16+
assert meta["base_url"] == "http://testserver:9000"
17+
assert before <= meta["time"] <= after
18+
19+
def test_make_meta_default_base_url(monkeypatch):
20+
monkeypatch.delenv("BASE_URL", raising=False)
21+
meta = make_meta("success", "Default URL")
22+
assert meta["base_url"] == "http://localhost:8000"
23+
24+
def test_make_meta_time_is_int():
25+
meta = make_meta("info", "Time Test")
26+
assert isinstance(meta["time"], int)
27+
assert re.match(r"^\d{13}$", str(meta["time"]))

0 commit comments

Comments
 (0)