Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (166 loc) · 6.1 KB
/
Copy pathmain.py
File metadata and controls
198 lines (166 loc) · 6.1 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from fastapi import FastAPI, HTTPException, Response, status, Request
from models.models import Employee, Client, Contract, History
from fastapi.middleware.cors import CORSMiddleware
import json
import os
import logging
# from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
logging.basicConfig(level=logging.DEBUG)
app = FastAPI(port=8081)
DATA='./dummy-data.json'
# TODO: add status_codes 404, 400, 201
# TODO: Database agnostic methods
# Middleware
# Change this to your own origins
origins = [
"http://localhost",
"http://localhost:8000",
"http://127.0.0.1:8000",
"http://127.0.0.1:8081"
]
# HTTPS only
# app.add_middleware(HTTPSRedirectMiddleware)
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def read_in_json():
with open(DATA) as f:
data = json.load(f)
return data
data=read_in_json()
# Employees
@app.get("/employees")
async def root():
return data["employees"]
def _employee_exists(employee_id) -> bool:
for employee in data["employees"]:
if employee["id"] == employee_id:
return True
return False
@app.post("/employees", status_code=201)
async def create_employee(employee: Employee) -> Employee:
data["employees"].append(employee)
return employee
@app.get("/employees/{employee_id}")
async def get_employee(employee_id: int):
if not _employee_exists(employee_id):
raise HTTPException(status_code=404, detail="Employee not found")
for employee in data["employees"]:
if employee["id"] == employee_id:
return employee
@app.put("/employees/{employee_id}")
async def update_employee(employee_id: int, employee: Employee):
if not _employee_exists(employee_id):
raise HTTPException(status_code=404, detail="Employee not found")
return {"employee_id": employee_id, **employee.dict()}
@app.delete("/employees/{employee_id}")
async def delete_employee(employee_id: int):
for employee in data["employees"]:
if employee["id"] == employee_id:
data["employees"].remove(employee)
return {f"Employee with ID: {employee_id}": "Deleted"}
# Clients
@app.get("/clients")
async def root():
return data["clients"]
def _client_exists(client_id):
for client in data["clients"]:
if client["id"] == client_id:
return True
return False
@app.post("/clients", status_code=200)
async def create_client(client: Client, response: Response):
if client != None:
data["clients"].append(client)
return client
else:
response.status_code=status.HTTP_400_CLIENT_ERROR
raise HTTPException(status_code=400, detail="Client Error")
@app.get("/clients/{client_id}", status_code=200)
async def get_client(client_id: int):
if not _client_exists(client_id):
raise HTTPException(status_code=404, detail="Client not found")
else:
return data['clients'][client_id]
@app.put("/clients/{client_id}", status_code=200)
async def update_client(client_id: int, client: Client, request: Request):
inbound=await request.json()
# Problem here as it fetches client automaitcally which includes ID.
if not _client_exists(client_id):
raise HTTPException(status_code=404, detail="Client not found.")
# Using privateAttrs to prevent updating ID but request allows meeting spec.
elif inbound.get("_id") != None:
raise HTTPException(status_code=400, detail="Cannot update primary id.")
else:
return {"client_id": client_id, **client.dict()}
@app.delete("/clients/{client_id}")
async def delete_client(client_id: int):
for client in data["clients"]:
if client["id"] == client_id:
data["clients"].remove(client)
return {f"Client with ID: {client_id}": "Deleted"}
raise HTTPException(status_code=404, detail="Client not found")
# Contracts
@app.get("/contracts")
async def root():
return data["contracts"]
def _contract_exists(contract_id):
for contract in data["contracts"]:
if contract["id"] == contract_id:
return True
return False
@app.post("/contracts", status_code=201)
async def create_contract(contract: Contract):
return contract
@app.get("/contracts/{contract_id}")
async def get_contract(contract_id: int):
if not _contract_exists(contract_id):
raise HTTPException(status_code=404, detail="Contract not found")
for contract in data["contracts"]:
if contract["id"] == contract_id:
return {"contract_id": contract}
@app.put("/contracts/{contract_id}")
async def update_contract(contract_id: int, contract: Contract):
if _contract_exists(contract_id):
return {"contract_id": contract_id, **contract.dict()}
return {"contract_id": "Not found"}
@app.delete("/contracts/{contract_id}")
async def delete_contract(contract_id: int):
for contract in data["contracts"]:
if contract["id"] == contract_id:
data["contracts"].remove(contract)
return {f"Contract with ID: {contract_id}": "Deleted"}
# History
@app.get("/history")
async def root():
return data["history"]
def _history_exists(history_id):
for history in data["history"]:
if history["id"] == history_id:
return True
return False
@app.post("/history", status_code=201)
async def create_history(history: History):
return history
@app.get("/history/{history_id}")
async def get_history(history_id: int):
if not _history_exists(history_id):
raise HTTPException(status_code=404, detail="History not found")
for history in data["history"]:
if history["id"] == history_id:
return history
@app.put("/history/{history_id}")
async def update_history(history_id: int, history: History):
if _history_exists(history_id):
return {"history_id": history_id, **history.dict()}
return {"history_id": "Not found"}
@app.delete("/history/{history_id}")
async def delete_history(history_id: int):
for history in data["history"]:
if history["id"] == history_id:
data["history"].remove(history)
return {f"History with ID: {history_id}": "Deleted"}