-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
217 lines (177 loc) · 8.28 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'''
Структура сервиса
Есть файлы users.csv и tokens.csv (имитация 2-х таблиц БД).
В users есть поля login, password, salary, salary_date,
в tokens - login, token, token_date.
Файл users должен быть заранее заполнен какими-то данными.
Users.csv считывается функцией csv_reader
и пишется в переменную users. В переменной лежат словари вида
{
"john": {
"login": "john",
"password": "pass",
"salary": "100",
"salary_date": "2023-06-04",
"token": "passjohn111",
"token_date": "2023-06-05 02:05:33.218894"
},
}
Затем создаётся экземпляр FastAPI, который слушает эндпоинты.
На эндпоинте '/' стоит заглушка, можно отправить GET-запрос,
но ничего не придёт в ответ.
На эндпоинте '/login' сервис ожидает POST-запрос JSON'a с параметрами вида
{
"login": "",
"password": ""
}
Функция проверяет правильность логина и пароля, обновляет
список токенов в паямти (считывает из словаря), проверяет
актуальность токена, при необходимости - обновляет,
записывает обновленные токены в базу (в csv-файл).
Актуальный токен появляется в памяти сервиса и в базе.
Токен актуален в течение суток.
На эндпоинт '/salary' нужно отправить
GET-запрос с query-параметрами 'login' и 'token'.
Функция обновляет список токенов в паямти (считывает из словаря),
проверяет правильность логина и токена, в случае ошибки - возвращает None.
При выполнение всех условий возвращает JSON с искомыми значениями:
{
"login": "john",
"salary": "100",
"salary_date": "2023-06-04"
}
Описание работы функций есть в docstring самих функций.
Описание полей объекта user, с которыми работает программа:
login: str = ''
password: str = ''
salary: float = 0.0
salary_date: str = ''
token: str = ''
token_date: str = ''
Изначально палнировалось сделать pydantic-модель,
но не понадобилось, внутренние функции работают
с обычным списком словарей,
модель одного пользователя - словарь.
ТЗ и реализация простые, pydantic здесь избыточен.
Для запуска потребуются FastAPI и uvicorn.
'''
import csv
from datetime import datetime
from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel
USERS = 'users.csv'
TOKENS = 'tokens.csv'
class LoginModel(BaseModel):
login: str = ''
password: str = ''
def csv_reader(file, users):
'''
Функция получения/обновления данных пользователей из CSV.
По идее, список пользователей обычно получают из БД,
но в данном случае, по ТЗ этого не требовалось,
сделал максимально просто.
Выбирал между JSON и CSV, взял CSV, потому что
его удобнее отлаживать и тестировать.
'''
with open(file, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
if row['login'] in users:
users[row['login']].update(row)
else:
users[row['login']] = row
def csv_writer(file, users):
'''
Простая функция записи в CSV.
Переделано строго для сохранения токенов в отдельную таблицу.
Если потребуется сохранять все данные, нужно раскомментировать
первую и вторую строки и закомментировать третью.
'''
with open(file, 'w', newline='') as file:
# anykey = [*users.keys()][0]
# headers = users[anykey].keys()
headers = ['login', 'token', 'token_date']
writer = csv.DictWriter(file, headers,
restval='Unknown',
extrasaction='ignore')
writer.writeheader()
for user in users.values():
writer.writerow(user)
def make_token(user):
'''
Механизм создания токена
Сейчас он максимально простой,
но технически здесь может быть
любая сложная логика,
либо сторонний модуль, заработает.
'''
password = user['password']
login = user['login']
secret_text = password + login + '111'
return secret_text
def update_token(user):
'''Простая функция обновления токена в объекте пользователя'''
user['token'] = make_token(user)
user['token_date'] = str(datetime.now())
def token_is_actual(user):
'''
Функция проверки токена на актуальность:
1. Проверяется, создавался ли токен вообще (проверка поля с датой)
2. Проверяется, актуален ли токен (дата создания - не позже прошлых суток)
3. Проверяется, совпадает ли механизм шифрования
'''
if not user['token_date']:
return False
got_date = datetime.strptime(user['token_date'], '%Y-%m-%d %H:%M:%S.%f')
if (datetime.now() - got_date).days > 1:
return False
if user['token'] != make_token(user):
return False
return True
def check_or_update_token(user):
'''Собрал три предыдущие функции в одну для удобства'''
if not token_is_actual(user):
update_token(user)
app = FastAPI()
# Правильно было бы задать параметры
# docs_url=None, redoc_url=None, openapi_url=None
# но я оставил их для более удобного теста запросов
users = dict()
csv_reader(USERS, users) # на старте программы
@app.get('/')
def index():
'''Главная страница ничего не возвращает'''
return None
@app.post('/login')
def login(current_login: LoginModel):
'''
Функция получает логин и пароль POST-запросом в форме JSON,
проверяет на актуальность, возвращает актуальный токен.
'''
if current_login.login not in users:
return None
if users[current_login.login]['password'] != current_login.password:
return None
csv_reader(TOKENS, users)
check_or_update_token(users[current_login.login])
csv_writer(TOKENS, users)
return users[current_login.login]['token']
@app.get('/salary')
def salary(login: str, token: str):
'''
Функция сделана как GET + Query-параметры, для разнообразия.
Проверяет на актуальность токен, возвращает искомые значения.
'''
csv_reader(TOKENS, users)
if login not in users:
return None
if users[login]['token'] != token:
return None
return {
'login': users[login]['login'],
'salary': users[login]['salary'],
'salary_date': users[login]['salary_date'],
}
if __name__ == '__main__':
uvicorn.run('main:app', reload=True)