-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (53 loc) · 1.81 KB
/
main.py
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
# Запуск: python3 -m uvicorn main:app --reload
# ngrok http 8000
from fastapi import FastAPI, Request, Body, Depends
from fastapi.responses import HTMLResponse
import schemas
import models
from database import *
from sqlalchemy.orm import Session
import myservice
app = FastAPI()
Base.metadata.create_all(engine)
def get_session():
session = SessionLocal()
try:
yield session
finally:
session.close()
@app.get("/")
def help():
return "Docs: https://github.com/Victor2004/gocloudcamp-test-assignment/blob/main/README.md "
@app.get("/id")
def get_item(id: str, session: Session = Depends(get_session)):
item = session.query(models.Item).get(id)
return item
@app.post("/id")
def add_item(item: schemas.Item, session = Depends(get_session)):
item = models.Item(service = item.service, data = item.data)
session.add(item)
session.commit()
session.refresh(item)
return item
@app.put("/id/{id}")
def update_item(id:int, item:schemas.Item, session = Depends(get_session)):
itemObject = session.query(models.Item).get(id)
session.commit()
return itemObject
@app.delete("/id/{id}")
def delete_item(id:int, session = Depends(get_session)):
if myservice.get_congif() == id:
return "You can not delete the config used by the service."
itemObject = session.query(models.Item).get(id)
session.delete(itemObject)
session.commit()
session.close()
return f"Config id={id} delete."
@app.get("/service")
def get_service_configs(session: Session = Depends(get_session)):
all_configs = session.query(models.Item).all()
return all_configs
@app.put("/service/{id}")
def update_service_config(id:int, session: Session = Depends(get_session)):
item = session.query(models.Item).get(myservice.get_congif(id))
return "Config update!", item