Skip to content
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

created delete component for jupyterhub api #148

Merged
merged 12 commits into from
Jan 5, 2023
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Changelog

Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.
## [0.7.0] - 2023-01-05

### Added

- Added delete component PR [#148](https://github.com/datajoint/pharus/pull/148)

### Bugfix

- Public deploy of dynamic API uses incorrect credential keywords PR [#148](https://github.com/datajoint/pharus/pull/148)

## [0.6.4] - 2022-12-07

Expand Down Expand Up @@ -233,6 +242,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
- Support for DataJoint attribute types: `varchar`, `int`, `float`, `datetime`, `date`, `time`, `decimal`, `uuid`.
- Check dependency utility to determine child table references.

[0.7.0]: https://github.com/datajoint/pharus/compare/0.6.4...0.7.0
[0.6.4]: https://github.com/datajoint/pharus/compare/0.6.3...0.6.4
[0.6.3]: https://github.com/datajoint/pharus/compare/0.6.2...0.6.3
[0.6.2]: https://github.com/datajoint/pharus/compare/0.6.1...0.6.2
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ To start the API server, use the command:

.. code-block:: bash

PHARUS_VERSION=0.6.4 docker-compose -f docker-compose-deploy.yaml up -d
PHARUS_VERSION=0.7.0 docker-compose -f docker-compose-deploy.yaml up -d

To stop the API server, use the command:

.. code-block:: bash

PHARUS_VERSION=0.6.4 docker-compose -f docker-compose-deploy.yaml down
PHARUS_VERSION=0.7.0docker-compose -f docker-compose-deploy.yaml down

References
----------
Expand Down
4 changes: 2 additions & 2 deletions docker-compose-deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PHARUS_VERSION=0.6.4 docker-compose -f docker-compose-deploy.yaml pull
# PHARUS_VERSION=0.6.4 docker-compose -f docker-compose-deploy.yaml up -d
# PHARUS_VERSION=0.7.0 docker-compose -f docker-compose-deploy.yaml pull
# PHARUS_VERSION=0.7.0 docker-compose -f docker-compose-deploy.yaml up -d
#
# Intended for production deployment.
# Note: You must run both commands above for minimal outage
Expand Down
14 changes: 14 additions & 0 deletions pharus/component_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(


class FetchComponent(Component):
rest_verb = ["GET"]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
component_config = kwargs.get("component_config", args[1] if args else None)
Expand Down Expand Up @@ -142,7 +144,18 @@ def dj_query_route(self):
)


class DeleteComponent(Component):
rest_verb = ["DELETE"]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def dj_query_route(self):
return


class InsertComponent(Component):
rest_verb = ["POST", "GET"]
fields_route_format = "{route}/fields"

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -453,4 +466,5 @@ def dj_query_route(self):
"slider": FetchComponent,
"dropdown-query": FetchComponent,
"form": InsertComponent,
"delete": DeleteComponent,
}
39 changes: 14 additions & 25 deletions pharus/dynamic_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import re
import warnings
from pharus.component_interface import InsertComponent, TableComponent
from pharus.component_interface import TableComponent, InsertComponent, FetchComponent


def populate_api():
Expand All @@ -27,11 +27,11 @@ def populate_api():
"""
route_template = """

@app.route('{route}', methods=['{rest_verb}'])
@app.route('{route}', methods={rest_verb})
@protected_route
def {method_name}(connection: dj.Connection) -> dict:

if request.method in ['{rest_verb}']:
if request.method in {rest_verb}:
try:
component_instance = type_map['{component_type}'](name='{component_name}',
component_config={component},
Expand All @@ -44,12 +44,12 @@ def {method_name}(connection: dj.Connection) -> dict:
"""
route_template_nologin = """

@app.route('{route}', methods=['{rest_verb}'])
@app.route('{route}', methods={rest_verb})
def {method_name}() -> dict:
if request.method in ['{rest_verb}']:
if request.method in {rest_verb}:
connection = dj.Connection(
databaseAddress=os.environ["PHARUS_HOST"],
username=os.environ["PHARUS_USER"],
host=os.environ["PHARUS_HOST"],
user=os.environ["PHARUS_USER"],
password=os.environ["PHARUS_PASSWORD"],
)
try:
Expand Down Expand Up @@ -104,7 +104,7 @@ def {method_name}() -> dict:
f.write(
(active_route_template).format(
route=grid["route"],
rest_verb="GET",
rest_verb=[FetchComponent.rest_verb[0]],
method_name=grid["route"].replace("/", ""),
component_type="basicquery",
component_name="dynamicgrid",
Expand All @@ -130,27 +130,16 @@ def {method_name}() -> dict:
stacklevel=2,
)
if re.match(
r"""^(
table|
antd-table|
metadata|
plot|
file|
slider|
dropdown-query|
form|
basicquery|
external
).*$""",
r"""
^(table|metadata|plot|file|slider|
dropdown-query|form|basicquery|external|delete).*$""",
comp["type"],
flags=re.VERBOSE,
):
f.write(
(active_route_template).format(
route=comp["route"],
rest_verb="POST"
if comp["type"].split(":", 1)[0] == "form"
else "GET",
rest_verb=[type_map[comp["type"]].rest_verb[0]],
method_name=comp["route"].replace("/", ""),
component_type=comp["type"],
component_name=comp_name,
Expand All @@ -169,7 +158,7 @@ def {method_name}() -> dict:
f.write(
(active_route_template).format(
route=fields_route,
rest_verb="GET",
rest_verb=[InsertComponent.rest_verb[1]],
method_name=fields_route.replace("/", ""),
component_type=comp["type"],
component_name=comp_name,
Expand All @@ -186,7 +175,7 @@ def {method_name}() -> dict:
f.write(
(active_route_template).format(
route=attributes_route,
rest_verb="GET",
rest_verb=[TableComponent.rest_verb[0]],
method_name=attributes_route.replace("/", ""),
component_type=comp["type"],
component_name=comp_name,
Expand Down
28 changes: 14 additions & 14 deletions pharus/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def wrapper(**kwargs):
if "database_host" in request.args:
encoded_jwt = request.headers.get("Authorization").split()[1]
connect_creds = {
"databaseAddress": request.args["database_host"],
"username": jwt.decode(
"host": request.args["database_host"],
"user": jwt.decode(
encoded_jwt,
crypto_serialization.load_der_public_key(
b64decode(environ.get("PHARUS_OIDC_PUBLIC_KEY").encode())
Expand All @@ -86,8 +86,8 @@ def wrapper(**kwargs):
algorithms="RS256",
)
connection = dj.Connection(
host=connect_creds["databaseAddress"],
user=connect_creds["username"],
host=connect_creds["host"],
user=connect_creds["user"],
password=connect_creds["password"],
)
return function(connection, **kwargs)
Expand Down Expand Up @@ -126,7 +126,7 @@ def api_version() -> str:
Content-Type: application/json

{
"version": "0.6.4"
"version": "0.7.0"
}

:statuscode 200: No error.
Expand Down Expand Up @@ -173,8 +173,8 @@ def login() -> dict:
Accept: application/json

{
"databaseAddress": "tutorial-db.datajoint.io",
"username": "user1",
"host": "tutorial-db.datajoint.io",
"user": "user1",
"password": "password1"
}

Expand Down Expand Up @@ -237,8 +237,8 @@ def login() -> dict:
)
time.sleep(1)
connect_creds = {
"databaseAddress": request.args["database_host"],
"username": jwt.decode(
"host": request.args["database_host"],
"user": jwt.decode(
auth_info["jwt"],
crypto_serialization.load_der_public_key(
b64decode(environ.get("PHARUS_OIDC_PUBLIC_KEY").encode())
Expand All @@ -256,12 +256,12 @@ def login() -> dict:
)
)
connect_creds = request.json
if connect_creds.keys() < {"databaseAddress", "username", "password"}:
if connect_creds.keys() < {"host", "user", "password"}:
return dict(error="Invalid Request, check headers and/or json body")
try:
dj.Connection(
host=connect_creds["databaseAddress"],
user=connect_creds["username"],
host=connect_creds["host"],
user=connect_creds["user"],
password=connect_creds["password"],
)
except pymysql.err.OperationalError as e:
Expand All @@ -276,8 +276,8 @@ def login() -> dict:
password=root_password,
).query("FLUSH PRIVILEGES")
dj.Connection(
host=connect_creds["databaseAddress"],
user=connect_creds["username"],
host=connect_creds["host"],
user=connect_creds["user"],
password=connect_creds["password"],
)
else:
Expand Down
2 changes: 1 addition & 1 deletion pharus/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Package metadata."""
__version__ = "0.6.4"
__version__ = "0.7.0"
8 changes: 4 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def token(client):
yield client.post(
"/login",
json=dict(
databaseAddress=getenv("TEST_DB_SERVER"),
username=getenv("TEST_DB_USER"),
host=getenv("TEST_DB_SERVER"),
user=getenv("TEST_DB_USER"),
password=getenv("TEST_DB_PASS"),
),
).json["jwt"]
Expand All @@ -47,8 +47,8 @@ def group1_token(client, connection):
yield client.post(
"/login",
json=dict(
databaseAddress=getenv("TEST_DB_SERVER"),
username="group1",
host=getenv("TEST_DB_SERVER"),
user="group1",
password="group1",
),
).json["jwt"]
Expand Down