Skip to content

Commit

Permalink
#150: Actually fix openapi YAML creation
Browse files Browse the repository at this point in the history
- This is essentially an undo of 8c1941d as I've realised the backend must get parsed into the endpoint classes for testing reasons
  • Loading branch information
MRichards99 committed Dec 7, 2020
1 parent 3eec346 commit c5a867c
Show file tree
Hide file tree
Showing 3 changed files with 444 additions and 357 deletions.
42 changes: 27 additions & 15 deletions datagateway_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
get_id_endpoint,
)
from datagateway_api.src.resources.entities.entity_map import endpoints
from datagateway_api.src.resources.non_entities.sessions_endpoints import Sessions
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.resources.table_endpoints.table_endpoints import (
InstrumentsFacilityCycles,
InstrumentsFacilityCyclesCount,
InstrumentsFacilityCyclesInvestigations,
InstrumentsFacilityCyclesInvestigationsCount,
count_instrument_facility_cycles_endpoint,
count_instrument_investigation_endpoint,
instrument_facility_cycles_endpoint,
instrument_investigation_endpoint,
)
from datagateway_api.src.swagger.apispec_flask_restful import RestfulPlugin
from datagateway_api.src.swagger.initialise_spec import initialise_spec
Expand Down Expand Up @@ -101,32 +103,42 @@ def create_api_endpoints(flask_app, api, spec):
spec.path(resource=get_find_one_endpoint_resource, api=api)

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

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

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

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

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


def openapi_config(spec):
Expand Down
282 changes: 147 additions & 135 deletions datagateway_api/src/resources/non_entities/sessions_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,164 @@
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")`
class Sessions(Resource):
def post(self):
"""
Generates a sessionID if the user has correct credentials
:return: String - SessionID
:param backend: The backend instance used for processing requests
:type backend: :class:`DatabaseBackend` or :class:`PythonICATBackend`
:return: The generated session endpoint class
"""

---
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:
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
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
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:
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

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:
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: The session ID
description: 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
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 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
return Sessions
Loading

0 comments on commit c5a867c

Please sign in to comment.