From 9964c363269e925978f3b37ef56e205bae6b920d Mon Sep 17 00:00:00 2001 From: DF5HSE Date: Sun, 19 Sep 2021 18:15:38 +0300 Subject: [PATCH] Add GET and POST endpoints --- .gitignore | 3 +++ main.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb80269 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +/__pycache__ +/venv \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..11a52e4 --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel, Field + +wsd_app = FastAPI() + +user_list = [ + { + 'mail': 'abc@yandex.ru', + 'password': 'abc_pwd' + }, + { + 'mail': 'qwerty@gmail.com', + 'password': 'qwerty' + } +] + + +@wsd_app.get("/statistics/") +async def get_statistics(indicator: str): + if indicator == "num_of_users": + return {'number of users': len(user_list)} + raise HTTPException(status_code=404, detail="Don't know this indicator") + + +class User(BaseModel): + mail: str = Field(..., regex=r".+@.+\..+") + password1: str + password2: str + + +@wsd_app.post("/registration/", status_code=201) +async def create_account(user: User): + if user.mail in [d['mail'] for d in user_list]: + raise HTTPException(status_code=403, detail="This mail has been already registered") + if user.password1 != user.password2: + raise HTTPException(status_code=403, detail="Passwords don't match") + user_list.append({'mail': user.mail, 'password': user.password1}) + return "Account is created"