Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Sonmur new2 #98

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 6/shumilova_sd/client.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 8 additions & 0 deletions 6/shumilova_sd/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel


class StarsTest(BaseModel):
name: str
radius: float
mass: float
distance: float
34 changes: 34 additions & 0 deletions 6/shumilova_sd/server.py
Original file line number Diff line number Diff line change
@@ -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)