forked from pnp/copilot-pro-dev-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
120 lines (100 loc) · 3.82 KB
/
Copy pathfunction_app.py
File metadata and controls
120 lines (100 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
import logging
import os
from pathlib import Path
import azure.functions as func
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
DATA_FILE = Path(__file__).parent / "src" / "repairsData.json"
def _is_api_key_valid(req: func.HttpRequest) -> bool:
expected_key = os.environ.get("API_KEY", "").strip()
if not expected_key:
return False
api_key = (req.headers.get("X-API-Key") or "").strip()
return api_key == expected_key
@app.route(route="repairs", methods=["GET"])
def repairs(req: func.HttpRequest) -> func.HttpResponse:
if not _is_api_key_valid(req):
return func.HttpResponse(
json.dumps({"error": "Unauthorized"}),
status_code=401,
mimetype="application/json",
)
try:
repair_records = json.loads(DATA_FILE.read_text(encoding="utf-8"))
except Exception:
logging.exception("Error reading repairs data")
return func.HttpResponse(
json.dumps({"error": "Failed to retrieve repair records"}),
status_code=500,
mimetype="application/json",
)
assigned_to = req.params.get("assignedTo")
if assigned_to:
query = assigned_to.strip().lower()
repair_records = [
r for r in repair_records
if r["assignedTo"].lower() == query
or query in r["assignedTo"].lower().split()
]
return func.HttpResponse(
json.dumps({"results": repair_records}),
status_code=200,
mimetype="application/json",
)
@app.route(route="repairs/{id}", methods=["PATCH"])
def update_repair(req: func.HttpRequest) -> func.HttpResponse:
if not _is_api_key_valid(req):
return func.HttpResponse(
json.dumps({"error": "Unauthorized"}),
status_code=401,
mimetype="application/json",
)
repair_id = req.route_params.get("id")
try:
body = req.get_json()
except ValueError:
return func.HttpResponse(
json.dumps({"error": "Invalid JSON body"}),
status_code=400,
mimetype="application/json",
)
new_title = body.get("title")
new_assignee = body.get("assignedTo")
if new_title is not None and (not isinstance(new_title, str) or not new_title.strip()):
return func.HttpResponse(
json.dumps({"error": "Title must be a non-empty string"}),
status_code=400,
mimetype="application/json",
)
if new_assignee is not None and (not isinstance(new_assignee, str) or not new_assignee.strip()):
return func.HttpResponse(
json.dumps({"error": "AssignedTo must be a non-empty string"}),
status_code=400,
mimetype="application/json",
)
try:
repair_records = json.loads(DATA_FILE.read_text(encoding="utf-8"))
idx = next((i for i, r in enumerate(repair_records) if r["id"] == repair_id), -1)
if idx < 0:
return func.HttpResponse(
json.dumps({"error": "Repair not found"}),
status_code=404,
mimetype="application/json",
)
if new_title is not None:
repair_records[idx]["title"] = new_title
if new_assignee is not None:
repair_records[idx]["assignedTo"] = new_assignee
DATA_FILE.write_text(json.dumps(repair_records, indent=2), encoding="utf-8")
return func.HttpResponse(
json.dumps({"updatedRepair": repair_records[idx]}),
status_code=200,
mimetype="application/json",
)
except Exception:
logging.exception("Error updating repair record")
return func.HttpResponse(
json.dumps({"error": "Failed to update repair record"}),
status_code=500,
mimetype="application/json",
)