1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-21 20:44:18 +00:00

Updated how POST works, changed http response codes

This commit is contained in:
Lorenzo Balugani 2021-05-11 19:08:14 +02:00
parent cc788ef936
commit 351a8ba0ab
8 changed files with 45 additions and 29 deletions

View file

@ -50,7 +50,7 @@ def page_alert(aid):
security:
- jwt: []
responses:
'200':
'204':
description: The repository has been deleted successfully.
'404':
description: Could not find the requested repository.
@ -88,7 +88,7 @@ def page_alert(aid):
schema: AlertParameterSchema
responses:
'200':
'204':
description: The alert has been updated successfully.
content:
application/json:
@ -131,14 +131,14 @@ def page_alert(aid):
if 'window_size' in request.json:
alert.window_size = request.json['window_size']
ext.session.commit()
return json_success(alert.to_json()), 200
return json_success(alert.to_json()), 204
elif request.method == "DELETE":
try:
ext.session.delete(alert)
ext.session.commit()
except Exception:
return json_error("Something went wrong while deleting alert."), 500
return json_success("Deletion completed."), 200
return json_success("Deletion completed."), 204
elif request.method == "PUT":
if not json_request_authorizer(request.json, alert):
return json_error("Missing one or more parameters in repository json."), 400
@ -185,6 +185,7 @@ def page_alert(aid):
return json_error("One of the provided IDs is incorrect"), 404
except KeyError:
return json_error("Unknown field specified."), 400
return json_success(alert.to_json()), 200
def create_node(node, ext, json, id):

View file

@ -48,7 +48,7 @@ def page_repository_alerts(rid):
application/json:
schema: CreateAlert
responses:
'200':
'201':
description: The alert has been created successfully.
content:
application/json:
@ -89,4 +89,4 @@ def page_repository_alerts(rid):
ext.session.add(alert)
ext.session.commit()
return json_success(alert.to_json()), 200
return json_success(alert.to_json()), 201

View file

@ -48,7 +48,7 @@ def page_condition(cid):
security:
- jwt: []
responses:
'200':
'204':
description: The deletion was successful.
'401':
description: The user is not logged in.
@ -80,7 +80,7 @@ def page_condition(cid):
application/json:
schema: CreateCondition
responses:
'200':
'204':
description: The user is not logged in.
content:
application/json:
@ -124,8 +124,8 @@ def page_condition(cid):
if content := request.json.get("content"):
condition.content = content
ext.session.commit()
return json_success(condition.to_json()), 200
return json_success(condition.to_json()), 204
if request.method == "DELETE":
ext.session.delete(condition)
ext.session.commit()
return json_success("Deleted."), 200
return json_success("Deleted."), 204

View file

@ -49,7 +49,7 @@ def page_repository_conditions(rid):
application/json:
schema: CreateCondition
responses:
'200':
'201':
description: The user has been created successfully.
content:
application/json:
@ -95,4 +95,4 @@ def page_repository_conditions(rid):
extension_sqlalchemy.session.add(condition)
extension_sqlalchemy.session.commit()
return json_success(condition.to_json()), 200
return json_success(condition.to_json()), 201

View file

@ -38,9 +38,9 @@ def page_repositories():
required: true
content:
application/json:
schema: CreateRepository
schema: Repository
responses:
'200':
'201':
description: The user has been created successfully.
content:
application/json:
@ -73,10 +73,25 @@ def page_repositories():
return json_success({"owner": [r.to_json() for r in owner],
"spectator": [r.repository.to_json() for r in spectator]})
elif request.method == "POST":
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
if not request.json.get("name") or not request.json.get("conditions") or not request.json.get("evaluation_mode"):
return json_error("Missing arguments."), 400
name = request.json.get("name")
if not name:
return json_error("Missing one or more parameters"), 400
repository = Repository(name=name, owner_id=user.email)
try:
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError:
return json_error("Unknown `type` specified."), 400
repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode)
ext.session.add(repository)
ext.session.commit()
return json_success(repository.to_json()), 200
ids = [c['id'] for c in request.json['conditions'] if c['id']]
# Create brand new conditions
for c in request.json['conditions']:
if not c['id']:
try:
type_ = ConditionType(c['type'])
except KeyError:
return json_error("Unknown `type` specified."), 400
ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id))
ext.session.commit()
return json_success(repository.to_json()), 201

View file

@ -50,7 +50,7 @@ def page_repository(rid):
security:
- jwt: []
responses:
'200':
'204':
description: The repository has been deleted successfully.
'404':
description: Could not find the requested repository.
@ -88,7 +88,7 @@ def page_repository(rid):
schema: IntegerParameterSchema
responses:
'200':
'204':
description: The repository has been updated successfully.
content:
application/json:
@ -175,7 +175,7 @@ def page_repository(rid):
return json_error("Unknown `type` specified."), 400
repository.evaluation_mode = evaluation_mode
ext.session.commit()
return json_success(repository.to_json()), 200
return json_success(repository.to_json()), 204
elif request.method == "DELETE":
if repository.owner_id != user.email and not user.isAdmin:
return json_error("You are not the owner of this repository."), 403
@ -185,7 +185,7 @@ def page_repository(rid):
except Exception as e:
ext.session.rollback()
return json_error("Cant delete repository because of dependencies."), 500
return json_success("Success"), 200
return json_success("Success"), 204
elif request.method == "PUT":
if not json_request_authorizer(request.json, repository):
return json_error("Missing one or more parameters in repository json."), 400

View file

@ -48,7 +48,7 @@ def page_user(email):
security:
- jwt: []
responses:
'200':
'204':
description: The user has been deleted successfully.
'404':
description: Could not find the requested user.
@ -86,7 +86,7 @@ def page_user(email):
security:
- jwt: []
responses:
'200':
'204':
description: The user has been updated successfully.
content:
application/json:
@ -133,7 +133,7 @@ def page_user(email):
except Exception:
ext.session.rollback()
return json_error("Could not delete the user."), 500
return json_success("The user has been deleted."), 200
return json_success("The user has been deleted."), 204
elif request.method == "PATCH":
if not email == user.email and not user.isAdmin:
return json_error("Thou art not authorized."), 403
@ -143,4 +143,4 @@ def page_user(email):
if request.json.get("password"):
target.password = gen_password(request.json.get("password"))
ext.session.commit()
return json_success(target.to_json()), 200
return json_success(target.to_json()), 204

View file

@ -39,7 +39,7 @@ def page_users():
application/json:
schema: CreateUser
responses:
'200':
'201':
description: The user has been created successfully.
content:
application/json:
@ -62,7 +62,7 @@ def page_users():
if not user.isAdmin:
return json_error("User is not admin. Thou art not authorized"), 403
users = User.query.all()
return json_success([user.to_json() for user in users])
return json_success([user.to_json() for user in users]), 200
if request.method == "POST":
if not user.isAdmin:
return json_error("User is not admin. Thou art not authorized."), 403
@ -70,4 +70,4 @@ def page_users():
username=request.json.get("username"))
ext.session.add(new_user)
ext.session.commit()
return json_success(new_user.to_json())
return json_success(new_user.to_json()), 201