fix server
This commit is contained in:
parent
1b3af70082
commit
973f432b53
36
server.py
36
server.py
@ -5,7 +5,7 @@ from functools import wraps
|
|||||||
|
|
||||||
import dotenv
|
import dotenv
|
||||||
import jwt
|
import jwt
|
||||||
from sanic import Sanic, Unauthorized
|
from sanic import Sanic, Unauthorized, json as jsonr
|
||||||
from sanic_ext import validate
|
from sanic_ext import validate
|
||||||
from sanic_ext.extensions.openapi import openapi
|
from sanic_ext.extensions.openapi import openapi
|
||||||
from sanic_ext.extensions.openapi.definitions import RequestBody
|
from sanic_ext.extensions.openapi.definitions import RequestBody
|
||||||
@ -65,21 +65,21 @@ def protected(wrapped):
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
@validate(json=LoginRequest)
|
@validate(json=LoginRequest)
|
||||||
async def login(request):
|
async def login(_, body: LoginRequest):
|
||||||
if not pbkdf2_sha256(10000, salt=b'salt').verify(request.json['password'], api_auth.get(request.json['username'])):
|
if not pbkdf2_sha256(10000, salt=b'salt').verify(body.password, api_auth.get(body.username)):
|
||||||
return {'status': 'error', 'message': 'Invalid username or password'}
|
return {'status': 'error', 'message': 'Invalid username or password'}
|
||||||
return {
|
return jsonr({
|
||||||
'token': jwt.encode({}, api_secret, algorithm='HS256'),
|
'token': jwt.encode({}, api_secret, algorithm='HS256'),
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
@app.get('/api/subscriptions')
|
@app.get('/api/subscriptions')
|
||||||
@protected
|
@protected
|
||||||
async def get_subscriptions(request):
|
async def get_subscriptions(_):
|
||||||
async with redis.conn as r:
|
async with redis.conn as r:
|
||||||
return {
|
return jsonr({
|
||||||
'subscriptions': await r.smembers(REDIS_SUBS_KEY),
|
'subscriptions': await r.smembers(REDIS_SUBS_KEY),
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -95,18 +95,17 @@ class UpdateSubscriptionRequest:
|
|||||||
)
|
)
|
||||||
@validate(json=UpdateSubscriptionRequest)
|
@validate(json=UpdateSubscriptionRequest)
|
||||||
@protected
|
@protected
|
||||||
async def delete_subscriptions(request):
|
async def delete_subscriptions(_, body: UpdateSubscriptionRequest):
|
||||||
data = request.json
|
requested_subs = {' '.join(sorted(sub.lower().split())) for sub in body.subs}
|
||||||
requested_subs = {' '.join(sorted(sub.lower().split())) for sub in data['subs']}
|
|
||||||
|
|
||||||
async with redis.conn as r:
|
async with redis.conn as r:
|
||||||
subs = await get_subs(r)
|
subs = await get_subs(r)
|
||||||
skipped = requested_subs - subs
|
skipped = requested_subs - subs
|
||||||
if skipped:
|
if skipped:
|
||||||
return {'status': 'error', 'message': 'Some subscriptions were not found', 'skipped': sorted(skipped)}
|
return jsonr({'status': 'error', 'message': 'Some subscriptions were not found', 'skipped': sorted(skipped)})
|
||||||
await r.srem(REDIS_SUBS_KEY, *requested_subs)
|
await r.srem(REDIS_SUBS_KEY, *requested_subs)
|
||||||
|
|
||||||
return {'status': 'ok', 'removed': sorted(requested_subs)}
|
return jsonr({'status': 'ok', 'removed': sorted(requested_subs)})
|
||||||
|
|
||||||
|
|
||||||
@app.post('/api/subscriptions')
|
@app.post('/api/subscriptions')
|
||||||
@ -117,18 +116,17 @@ async def delete_subscriptions(request):
|
|||||||
)
|
)
|
||||||
@validate(json=UpdateSubscriptionRequest)
|
@validate(json=UpdateSubscriptionRequest)
|
||||||
@protected
|
@protected
|
||||||
async def add_subscriptions(request):
|
async def add_subscriptions(_, body: UpdateSubscriptionRequest):
|
||||||
data = request.json
|
requested_subs = {' '.join(sorted(sub.lower().split())) for sub in body.subs}
|
||||||
requested_subs = {' '.join(sorted(sub.lower().split())) for sub in data['subs']}
|
|
||||||
|
|
||||||
async with redis.conn as r:
|
async with redis.conn as r:
|
||||||
subs = await get_subs(r)
|
subs = await get_subs(r)
|
||||||
conflicts = requested_subs & subs
|
conflicts = requested_subs & subs
|
||||||
if conflicts:
|
if conflicts:
|
||||||
return {'status': 'error', 'message': 'Some subscriptions already exist', 'conflicts': sorted(conflicts)}
|
return jsonr({'status': 'error', 'message': 'Some subscriptions already exist', 'conflicts': sorted(conflicts)})
|
||||||
await r.sadd(REDIS_SUBS_KEY, *data['subs'])
|
await r.sadd(REDIS_SUBS_KEY, *body.subs)
|
||||||
|
|
||||||
return {'status': 'ok', 'added': sorted(requested_subs)}
|
return jsonr({'status': 'ok', 'added': sorted(requested_subs)})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user