Skip to content

Commit

Permalink
#150: Fix openapi YAML creation
Browse files Browse the repository at this point in the history
- I've reverted the setup for the ISIS and session endpoints back to way it was done at the start of this branch, there's no need to parse backend now the test backend is set in the config object, just create it locally on each endpoint file
  • Loading branch information
MRichards99 committed Dec 7, 2020
1 parent 1b91217 commit 8c1941d
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 438 deletions.
42 changes: 15 additions & 27 deletions datagateway_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
get_id_endpoint,
)
from datagateway_api.src.resources.entities.entity_map import endpoints
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.resources.non_entities.sessions_endpoints import Sessions
from datagateway_api.src.resources.table_endpoints.table_endpoints import (
count_instrument_facility_cycles_endpoint,
count_instrument_investigation_endpoint,
instrument_facility_cycles_endpoint,
instrument_investigation_endpoint,
InstrumentsFacilityCycles,
InstrumentsFacilityCyclesCount,
InstrumentsFacilityCyclesInvestigations,
InstrumentsFacilityCyclesInvestigationsCount,
)
from datagateway_api.src.swagger.apispec_flask_restful import RestfulPlugin
from datagateway_api.src.swagger.initialise_spec import initialise_spec
Expand Down Expand Up @@ -103,42 +101,32 @@ def create_api_endpoints(flask_app, api, spec):
spec.path(resource=get_find_one_endpoint_resource, api=api)

# Session endpoint
session_endpoint_resource = session_endpoints(backend)
api.add_resource(session_endpoint_resource, "/sessions")
# spec.path(resource=session_endpoint_resource, api=api)
api.add_resource(Sessions, "/sessions")
spec.path(resource=Sessions, api=api)

# Table specific endpoints
instrument_facility_cycle_resource = instrument_facility_cycles_endpoint(backend)
api.add_resource(
instrument_facility_cycle_resource, "/instruments/<int:id_>/facilitycycles",
InstrumentsFacilityCycles, "/instruments/<int:id_>/facilitycycles",
)
# spec.path(resource=instrument_facility_cycle_resource, api=api)
spec.path(resource=InstrumentsFacilityCycles, api=api)

count_instrument_facility_cycle_res = count_instrument_facility_cycles_endpoint(
backend,
)
api.add_resource(
count_instrument_facility_cycle_res,
"/instruments/<int:id_>/facilitycycles/count",
InstrumentsFacilityCyclesCount, "/instruments/<int:id_>/facilitycycles/count",
)
# spec.path(resource=count_instrument_facility_cycle_resource, api=api)
spec.path(resource=InstrumentsFacilityCyclesCount, api=api)

instrument_investigation_resource = instrument_investigation_endpoint(backend)
api.add_resource(
instrument_investigation_resource,
InstrumentsFacilityCyclesInvestigations,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations",
)
# spec.path(resource=instrument_investigation_resource, api=api)
spec.path(resource=InstrumentsFacilityCyclesInvestigations, api=api)

count_instrument_investigation_resource = count_instrument_investigation_endpoint(
backend,
)
api.add_resource(
count_instrument_investigation_resource,
InstrumentsFacilityCyclesInvestigationsCount,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations"
"/count",
)
# spec.path(resource=count_instrument_investigation_resource, api=api)
spec.path(resource=InstrumentsFacilityCyclesInvestigationsCount, api=api)


def openapi_config(spec):
Expand Down
281 changes: 135 additions & 146 deletions datagateway_api/src/resources/non_entities/sessions_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,163 +3,152 @@
from flask import request
from flask_restful import Resource

from datagateway_api.common.config import config
from datagateway_api.common.exceptions import AuthenticationError
from datagateway_api.common.helpers import get_session_id_from_auth_header


log = logging.getLogger()

backend = config.get_backend_type()

def session_endpoints(backend):
"""
Generate a flask_restful Resource class using the configured backend. In main.py
these generated classes are registered with the api e.g.
`api.add_resource(get_endpoint("Datafiles", DATAFILE), "/datafiles")`

:param backend: The backend instance used for processing requests
:type backend: :class:`DatabaseBackend` or :class:`PythonICATBackend`
:return: The generated session endpoint class
"""
class Sessions(Resource):
def post(self):
"""
Generates a sessionID if the user has correct credentials
:return: String - SessionID
class Sessions(Resource):
def post(self):
"""
Generates a sessionID if the user has correct credentials
:return: String - SessionID
---
summary: Login
description: Generates a sessionID if the user has correct credentials
tags:
- Sessions
security: []
requestBody:
description: User credentials to login with
required: true
---
summary: Login
description: Generates a sessionID if the user has correct credentials
tags:
- Sessions
security: []
requestBody:
description: User credentials to login with
required: true
content:
application/json:
schema:
type: object
properties:
username:
type: string
password:
type: string
mechanism:
type: string
responses:
201:
description: Success - returns a session ID
content:
application/json:
application/json:
schema:
type: object
properties:
username:
type: string
password:
type: string
mechanism:
type: string
responses:
201:
description: Success - returns a session ID
content:
application/json:
schema:
type: object
properties:
sessionID:
type: string
description: Session ID
example: xxxxxx-yyyyyyy-zzzzzz
400:
description: Bad request. User credentials not provided in request body
403:
description: Forbidden. User credentials were invalid
"""
if not (
request.data
and "username" in request.json
and "password" in request.json
):
return "Bad request", 400
# If no mechanism is present in request body, default to simple
if not ("mechanism" in request.json):
request.json["mechanism"] = "simple"
try:
return ({"sessionID": backend.login(request.json)}, 201)
except AuthenticationError:
return ("Forbidden", 403)
type: object
properties:
sessionID:
type: string
description: Session ID
example: xxxxxx-yyyyyyy-zzzzzz
400:
description: Bad request. User credentials not provided in request body
403:
description: Forbidden. User credentials were invalid
"""
if not (
request.data and "username" in request.json and "password" in request.json
):
return "Bad request", 400
# If no mechanism is present in request body, default to simple
if not ("mechanism" in request.json):
request.json["mechanism"] = "simple"
try:
return {"sessionID": backend.login(request.json)}, 201
except AuthenticationError:
return "Forbidden", 403

def delete(self):
"""
Deletes a users sessionID when they logout
:return: Blank response, 200
---
summary: Delete session
description: Deletes a users sessionID when they logout
tags:
- Sessions
responses:
200:
description: Success - User's session was successfully deleted
400:
description: Bad request - something was wrong with the request
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
404:
description: Not Found - Unable to find session ID
"""
backend.logout(get_session_id_from_auth_header())
return ("", 200)
def delete(self):
"""
Deletes a users sessionID when they logout
:return: Blank response, 200
---
summary: Delete session
description: Deletes a users sessionID when they logout
tags:
- Sessions
responses:
200:
description: Success - User's session was successfully deleted
400:
description: Bad request - something was wrong with the request
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
404:
description: Not Found - Unable to find session ID
"""
backend.logout(get_session_id_from_auth_header())
return "", 200

def get(self):
"""
Gives details of a users session
:return: String: Details of the session, 200
---
summary: Get session details
description: Gives details of a user's session
tags:
- Sessions
responses:
200:
description: Success - a user's session details
content:
application/json:
schema:
type: object
properties:
ID:
type: string
description: The session ID
example: xxxxxx-yyyyyyy-zzzzzz
EXPIREDATETIME:
type: string
format: datetime
description: When this session expires
example: "2017-07-21T17:32:28Z"
USERNAME:
type: string
description: Username associated with this session
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
"""
return (backend.get_session_details(get_session_id_from_auth_header()), 200)

def put(self):
"""
Refreshes a users session
:return: String: The session ID that has been refreshed, 200
---
summary: Refresh session
description: Refreshes a users session
tags:
- Sessions
responses:
200:
description: Success - the user's session ID that has been refreshed
content:
application/json:
schema:
type: string
description: Session ID
example: xxxxxx-yyyyyyy-zzzzzz
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
"""
return (backend.refresh(get_session_id_from_auth_header()), 200)
def get(self):
"""
Gives details of a users session
:return: String: Details of the session, 200
---
summary: Get session details
description: Gives details of a user's session
tags:
- Sessions
responses:
200:
description: Success - a user's session details
content:
application/json:
schema:
type: object
properties:
ID:
type: string
description: The session ID
example: xxxxxx-yyyyyyy-zzzzzz
EXPIREDATETIME:
type: string
format: datetime
description: When this session expires
example: "2017-07-21T17:32:28Z"
USERNAME:
type: string
description: Username associated with this session
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
"""
return backend.get_session_details(get_session_id_from_auth_header()), 200

return Sessions
def put(self):
"""
Refreshes a users session
:return: String: The session ID that has been refreshed, 200
---
summary: Refresh session
description: Refreshes a users session
tags:
- Sessions
responses:
200:
description: Success - the user's session ID that has been refreshed
content:
application/json:
schema:
type: string
description: Session ID
example: xxxxxx-yyyyyyy-zzzzzz
401:
description: Unauthorized - No session ID found in HTTP Auth. header
403:
description: Forbidden - The session ID provided is invalid
"""
return backend.refresh(get_session_id_from_auth_header()), 200
Loading

0 comments on commit 8c1941d

Please sign in to comment.