diff --git a/6/shumilova_sd/client.py b/6/shumilova_sd/client.py new file mode 100644 index 00000000..d78851c4 --- /dev/null +++ b/6/shumilova_sd/client.py @@ -0,0 +1,26 @@ +import requests +from random import randrange +from schemas import StarsTest + + +s = requests.Session() +api_base = 'http://127.0.0.1:8000' + + +def main(): + names = ["Sun", "Alpha Centauri", "Proxima Centauri", "Sirius"] + stars = [] + for i in names: + cls = StarsTest(name=i, radius=randrange(1000, 5000), mass=randrange(5000, 100000), + distance=randrange(10000, 1000000)) + stars.append(cls) + for i in stars: + s.put(f"{api_base}/add", i.model_dump_json()) + + for i in names: + res = s.get(f"{api_base}/stars/{i}/dis") + print(f"Your star is {i}, and it's distance from the Earth is {res.json()}") + + +if __name__ == '__main__': + main() diff --git a/6/shumilova_sd/schemas.py b/6/shumilova_sd/schemas.py new file mode 100644 index 00000000..13240034 --- /dev/null +++ b/6/shumilova_sd/schemas.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + + +class StarsTest(BaseModel): + name: str + radius: float + mass: float + distance: float diff --git a/6/shumilova_sd/server.py b/6/shumilova_sd/server.py new file mode 100644 index 00000000..0e645d67 --- /dev/null +++ b/6/shumilova_sd/server.py @@ -0,0 +1,34 @@ +from fastapi import FastAPI +from schemas import StarsTest +import uvicorn + +api = FastAPI() +stars = [] + + +@api.put('/add') +async def add_star(star: StarsTest): + stars.append(star) + + +@api.get("/") +async def get_stars(): + return stars + + +@api.get("/stars/{star_name}") +async def get_star_by_name(star_name: str): + for i in stars: + if i.name == star_name: + return f"{i.name}" + + +@api.get("/stars/{star_name}/dis") +async def get_star_distance(star_name: str): + for i in stars: + if i.name == star_name: + return f"{i.distance}" + + +if __name__ == '__main__': + uvicorn.run('server:api', reload=True)