Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions docs/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ Environment Setup
The application requires some environmental arguments in order to run properly, these are illustrated in
the table below.

+-------------+-------------------------------+-------------------------------------+
| ENV | Default | Description |
+-------------+-------------------------------+-------------------------------------+
| `DB_HOST` | `postgresql://localhost:5432` | The URL for the PostgreSQL server. |
+-------------+-------------------------------+-------------------------------------+
| `DB_NAME` | `rems` | Name of the database. |
+-------------+-------------------------------+-------------------------------------+
| `DB_USER` | `rems` | Database username. |
+-------------+-------------------------------+-------------------------------------+
| `DB_PASS` | `rems` | Database password. |
+-------------+-------------------------------+-------------------------------------+
| `APP_HOST` | `0.0.0.0` | Default Host for the Web Server. |
+-------------+-------------------------------+-------------------------------------+
| `APP_PORT` | `8080` | Default port for the Web Server. |
+-------------+-------------------------------+-------------------------------------+
| `DEBUG` | `True` | If set to `True`, logs all actions. |
+-------------+-------------------------------+-------------------------------------+
+------------- +-------------------------------+-------------------------------------+
| ENV | Default | Description |
+------------- +-------------------------------+-------------------------------------+
| `DB_HOST` | `postgresql://localhost:5432` | The URL for the PostgreSQL server. |
+------------- +-------------------------------+-------------------------------------+
| `DB_NAME` | `rems` | Name of the database. |
+------------- +-------------------------------+-------------------------------------+
| `DB_USER` | `rems` | Database username. |
+------------- +-------------------------------+-------------------------------------+
| `DB_PASS` | `rems` | Database password. |
+------------- +-------------------------------+-------------------------------------+
| `APP_HOST` | `0.0.0.0` | Default Host for the Web Server. |
+------------- +-------------------------------+-------------------------------------+
| `APP_PORT` | `8080` | Default port for the Web Server. |
+------------- +-------------------------------+-------------------------------------+
| `PUBLIC_KEY` | `None` | Mandatory api key. |
+------------- +-------------------------------+-------------------------------------+
| `DEBUG` | `True` | If set to `True`, logs all actions. |
+------------- +-------------------------------+-------------------------------------+

Setting the necessary environment variables can be done e.g. via the command line:

Expand All @@ -37,6 +39,7 @@ Setting the necessary environment variables can be done e.g. via the command li
$ export DB_PASS=rems
$ export HOST=0.0.0.0
$ export PORT=8080
$ export PUBLIC_KEY=secret_string
$ export DEBUG=True

.. _app-setup:
Expand Down
15 changes: 8 additions & 7 deletions elixir_rems_proxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@

from aiohttp import web

from .schemas import load_schema
from .utils.validate import validate, api_key, check_user
from .utils.db_pool import init_db_pool
from .utils.process import process_post_request, process_get_request, process_patch_request, process_delete_request
from .utils.logging import LOG
# from .utils.validate import validate IMPLEMENT JSON SCHEMA VALIDATION


routes = web.RouteTableDef()


@routes.get('/', name='index')
async def api_get(request):
"""Return name of service, doubles as a health check function."""
LOG.debug('INDEX Request received.')
LOG.debug('INFO Request received.')
return web.Response(text='ELIXIR AAI API for REMS')


@routes.post('/user')
@validate(load_schema("post"))
async def user_post(request):
"""POST request to the /user endpoint.

Expand Down Expand Up @@ -67,6 +68,7 @@ async def user_get(request):

@routes.patch('/user/')
@routes.patch('/user/{user}')
@validate(load_schema("patch"))
async def user_patch(request):
"""PATCH request to the /user endpoint.

Expand Down Expand Up @@ -99,9 +101,8 @@ async def user_delete(request):

if 'user' in request.match_info:
user_identifier = request.match_info['user']
processed_request = await process_delete_request(user_identifier, db_pool)
if processed_request:
return web.HTTPOk(text='User was deleted')
await process_delete_request(user_identifier, db_pool)
return web.HTTPOk(text='User was deleted')
else:
raise web.HTTPBadRequest(text='Username not provided')

Expand All @@ -121,7 +122,7 @@ async def close_db(app):
def init_app():
"""Initialise the app."""
LOG.info('Initialise the server.')
app = web.Application()
app = web.Application(middlewares=[api_key(), check_user()])
app.router.add_routes(routes)
app.on_startup.append(init_db)
app.on_cleanup.append(close_db)
Expand Down
13 changes: 13 additions & 0 deletions elixir_rems_proxy/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Load JSON Schemas."""

import os
import json


def load_schema(name):
"""Load JSON schemas."""
module_path = os.path.dirname(__file__)
path = os.path.join(module_path, f'{name}.json')
with open(os.path.abspath(path), 'r') as fp:
data = fp.read()
return json.loads(data)
42 changes: 42 additions & 0 deletions elixir_rems_proxy/schemas/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"definitions": {},
"type": "object",
"required": [
"permissions"
],
"properties": {
"permissions": {
"type": "array",
"items": {
"type": "object",
"required": [
"affiliation",
"source_signature",
"url_prefix",
"datasets"
],
"properties": {
"affiliation": {
"type": "string",
"default": ""
},
"source_signature": {
"type": "string",
"default": ""
},
"url_prefix": {
"type": "string",
"default": ""
},
"datasets": {
"type": "array",
"items": {
"type": "string",
"default": ""
}
}
}
}
}
}
}
63 changes: 63 additions & 0 deletions elixir_rems_proxy/schemas/patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"definitions": {},
"type": "object",
"required": [
"user_identifier",
"affiliation",
"datasets"
],
"properties": {
"user_identifier": {
"type": "string",
"default": ""
},
"affiliation": {
"type": "string",
"default": ""
},
"datasets": {
"type": "array",
"items": {
"type": "object",
"required": [
"permissions"
],
"properties": {
"permissions": {
"type": "array",
"items": {
"type": "object",
"required": [
"affiliation",
"source_signature",
"url_prefix",
"datasets"
],
"properties": {
"affiliation": {
"type": "string",
"default": ""
},
"source_signature": {
"type": "string",
"default": ""
},
"url_prefix": {
"type": "string",
"default": ""
},
"datasets": {
"type": "array",
"items": {
"type": "string",
"default": ""
}
}
}
}
}
}
}
}
}
}
63 changes: 63 additions & 0 deletions elixir_rems_proxy/schemas/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"definitions": {},
"type": "object",
"required": [
"user_identifier",
"affiliation",
"datasets"
],
"properties": {
"user_identifier": {
"type": "string",
"default": ""
},
"affiliation": {
"type": "string",
"default": ""
},
"datasets": {
"type": "array",
"items": {
"type": "object",
"required": [
"permissions"
],
"properties": {
"permissions": {
"type": "array",
"items": {
"type": "object",
"required": [
"affiliation",
"source_signature",
"url_prefix",
"datasets"
],
"properties": {
"affiliation": {
"type": "string",
"default": ""
},
"source_signature": {
"type": "string",
"default": ""
},
"url_prefix": {
"type": "string",
"default": ""
},
"datasets": {
"type": "array",
"items": {
"type": "string",
"default": ""
}
}
}
}
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion elixir_rems_proxy/utils/db_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Database Queries."""

from aiohttp import web

from ..utils.logging import LOG


Expand Down Expand Up @@ -111,4 +113,4 @@ async def delete_user(user, connection):
return True
except Exception as e:
LOG.debug(f'An error occurred while attempting to delete user -> {e}')
return None
raise web.HTTPInternalServerError(text='Database error when attempting to remove user')
Loading