-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev service endpoint #24
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from flask import Blueprint, Flask, request, Response, Request | ||
from ..database import db | ||
from ..models.service import Service | ||
from ..models.user import User | ||
from sqlalchemy_helpers import get_or_create | ||
from .util import not_found, success, bad_request, created, conflict, validate_request, exclude_from_val, unprocessable_entity | ||
|
||
|
||
app = Flask(__name__) | ||
service_endpoint = Blueprint("service_endpoint", __name__) | ||
|
||
|
||
@service_endpoint.route("/service", methods=["POST"]) | ||
def create_service(): | ||
""" | ||
Used for creating a new service by sending a post request to /service/ path. | ||
""" | ||
if not validate_request(request.json, fields=['username', 'type', 'desc', 'name']): | ||
return unprocessable_entity() | ||
|
||
session = db.Session() | ||
body = request.json | ||
|
||
|
||
user = session.query(User).filter(User.username == body['username']).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if user is None: | ||
return not_found() | ||
|
||
service, is_created = get_or_create(session, Service, name=body['name'], type=body['type'], desc=body['desc'], user_id=user.id) | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not is_created: | ||
return conflict({'message': 'Service Already Exists'}) | ||
else: | ||
return created({'message': 'Created', 'uuid': service.id}) | ||
|
||
|
||
@service_endpoint.route("/service/search", methods=["GET"]) | ||
def list_services(): | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Used for listing all services belong to a user by sending a get request to /service/search path | ||
""" | ||
if not validate_request(request): | ||
return unprocessable_entity() | ||
|
||
session = db.Session() | ||
user = session.query(User).filter(User.username.like(request.json['username'])).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if user is None: | ||
return not_found() | ||
|
||
services = session.query(Service).filter(Service.user_id == user.id).all() | ||
|
||
return success({'service_list': services}) | ||
|
||
|
||
@service_endpoint.route("/service", methods=["GET"]) | ||
def lookup_service(): | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Used for retrieving a service by it's uuid by sending a get request | ||
to the /service path. | ||
|
||
Request Body: | ||
service_uuid: Service UUID | ||
""" | ||
if not validate_request(request, ['service_uuid']): | ||
return unprocessable_entity() | ||
|
||
session = db.Session() | ||
service = session.query(Service).filter(Service.id == request.json['service_uuid']).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if service is None: | ||
return not_found() | ||
else: | ||
return success({'uuid': service.id, 'name': service.name, 'type': service.type, 'desc': service.desc}) | ||
|
||
|
||
@service_endpoint.route("/service/revoke", methods=["PUT"]) | ||
def revoke_service(): | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Used for revoking a service by sending a PUT request to /service/revoke path. | ||
|
||
Request Body: | ||
service_uuid: Service UUID | ||
username: Username of the user that servicce belongs to. | ||
""" | ||
if not validate_request(request, fields=['username', 'service_uuid']): | ||
return unprocessable_entity() | ||
|
||
session = db.Session() | ||
user = session.query(User).filter(User.username == request.json['username']).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if user is None: | ||
return not_found() | ||
|
||
service = session.query(Service).filter(Service.user_id == user.id).filter(Service.id == request.json['service_uuid']).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if service is None: | ||
return not_found() | ||
|
||
service.disabled = True | ||
session.commit() | ||
|
||
return success({'uuid': service.id, 'is_valid': not service.disabled}) | ||
|
||
|
||
@service_endpoint.route("/service", methods=["PUT"]) | ||
def update_service(): | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Used for updating a service by sending a PUT request to /service path. | ||
|
||
Request Body: | ||
uuid: UUID of the service | ||
name: Updated name (optional) | ||
mesg_body: Updated message body (optional) | ||
|
||
""" | ||
if not validate_request(request, fields=['uuid', 'name', 'mesg_body']): | ||
return unprocessable_entity() | ||
|
||
session = db.Session() | ||
service = session.query(Service).filter(Service.id == request.json['uuid']).first() | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if service is None: | ||
return not_found() | ||
|
||
service.name = request.json['name'] if request.json['name'] is not None and request.json['name'] != "" else service.name | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
service.desc = request.json['mesg_body'] if request.json['mesg_body'] is not None and request.json['mesg_body'] != "" else service.desc | ||
brngylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
session.commit() | ||
return success({'uuid': service.id, 'name': service.name, 'mesg_body': service.desc, 'is_valid': not service.disabled}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abompard Do we want to stick to relative or absolute imports for this one?
@brngylni Whatever it might be - Let us stick to just one for consistency.