-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
160 lines (124 loc) · 3.93 KB
/
api.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
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
from src.main import Simulation
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
sim = Simulation()
class SimulationData(BaseModel):
name: str
class PrimaryData(BaseModel):
hash: str
m: float
class OrbitalElementsData(BaseModel):
hash: str
m: float
P: float
e: float
M: float
a: Optional[float] = None
inc: Optional[float] = None
omega: Optional[float] = None
Omega: Optional[float] = None
class BodyData(BaseModel):
hash: str
m: float
x: Optional[float] = None
y: Optional[float] = None
z: Optional[float] = None
vx: Optional[float] = None
vy: Optional[float] = None
vz: Optional[float] = None
class BodyUpdateData(BaseModel):
hash: str
m: Optional[float] = None
x: Optional[float] = None
y: Optional[float] = None
z: Optional[float] = None
vx: Optional[float] = None
vy: Optional[float] = None
vz: Optional[float] = None
class TimeData(BaseModel):
time: float
class TrajectoryData(BaseModel):
start_time: float
end_time: float
time_step: float
target: Optional[str] = None
@app.route('/')
def home():
"""
Home route for the API. Returns a message indicating the API should be used via endpoints.
Returns:
str: A message indicating the API should be used via endpoints.
"""
return 'This API should not be used in regular browser mode. Please use it as an API endpoint.'
@app.post('/create')
def create_route(data: SimulationData):
global sim
sim = Simulation(data.name)
sim.save()
return {"status": "success", "message": f"Simulation {data.name} created"}
@app.post('/load')
def load_route(data: SimulationData):
global sim
try:
sim = Simulation.load(data.name)
return {"status": "success", "message": f"Simulation {data.name} loaded"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/add_primary')
def add_primary_route(data: PrimaryData):
global sim
try:
sim.add_primary(**data.model_dump())
sim.save()
return {"status": "success", "message": f"{data.hash} added"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/add_object')
def add_object_route(data: BodyData):
global sim
try:
sim.add_object(**data.model_dump())
sim.save()
return {"status": "success", "message": f"{data.hash} added"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/add_from_orbital_elements')
def add_from_orbital_elements_route(data: OrbitalElementsData):
global sim
try:
sim.add_from_orbital_elements(**data.model_dump())
sim.save()
return {"status": "success", "message": f"{data.hash} added"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/integrate')
def integrate_route(data: TimeData):
global sim
try:
sim.integrate(data.time)
sim.save()
return {"status": "success", "message": f"Simulation {sim.name} integrated to {data.time}"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/update_object')
def update_object_route(data: BodyUpdateData):
global sim
try:
sim.update_object(**data.model_dump())
sim.save()
return {"status": "success", "message": f"{data.hash} updated"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post('/get_trajectory')
def get_trajectory_route(data: TrajectoryData):
global sim
try:
result = sim.get_trajectory(**data.model_dump())
return result
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=4242)