-
Notifications
You must be signed in to change notification settings - Fork 1
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
modification for reaxflow-workflow-service #70
base: main
Are you sure you want to change the base?
Changes from all commits
822ed8c
0f01a52
0581c4e
d79d052
4be4c9f
a5f11e4
8dd7267
c2907eb
f1d6efe
ee34679
832aeaf
b579abe
4a57eed
a83c96c
293ed92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
from marketplace.data_sink_client.session import MPSession | ||
from marketplace.datasink_client.session import MPSession | ||
|
||
marketplace_url = "https://materials-marketplace.eu/" | ||
access_token = "PASTE_TOKEN_HERE" | ||
|
||
client_id = "edb56699-9377-4f41-b1c7-ef2f46dac707" | ||
|
||
query = """SELECT ?subject ?predicate ?object WHERE {{ ?subject ?predicate ?object . }} LIMIT 5""" | ||
with MPSession() as test: | ||
with MPSession( | ||
marketplace_host_url=marketplace_url, access_token=access_token, client_id=client_id | ||
) as test: | ||
objects = test.query(query=query) | ||
print(objects) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
from marketplace.data_sink_client.session import MPSession | ||
from marketplace.datasink_client.session import MPSession | ||
|
||
query = "SELECT ?subject ?predicate ?object WHERE {{ ?subject ?predicate ?object . }} LIMIT 5" | ||
with MPSession() as test: | ||
objects = test.query_dataset(collection_name="c1", dataset_name="d1", query=query) | ||
marketplace_url = "https://materials-marketplace.eu/" | ||
access_token = "PASTE_TOKEN_HERE" | ||
client_id = "edb56699-9377-4f41-b1c7-ef2f46dac707" | ||
|
||
query = """SELECT ?subject ?predicate ?object WHERE {{ ?subject ?predicate ?object . }} LIMIT 5""" | ||
with MPSession( | ||
marketplace_host_url=marketplace_url, access_token=access_token, client_id=client_id | ||
) as test: | ||
objects = test.query_dataset( | ||
collection_name="c1", dataset_name="data_test", query=query | ||
) | ||
print(objects) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,33 @@ def get_transformation_list( | |
|
||
@check_capability_availability | ||
def new_transformation( | ||
self, new_transformation: transformation.NewTransformationModel | ||
self, | ||
new_transformation: transformation.NewTransformationModel, | ||
config: dict = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To send additional query parameters which are not defined in Marketplace. For example reaxpro-service needs model_name an additional query parameter to create the transformation. Any key values that are passed in config will be send as query parameters to the application by marketplace. An alternative solution can be to send inside the body. I thought sending it as query will make it less restriction at the application side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think needs a proper docstring (so users can see it through contextual help in their IDEs) would be specially benefitial in this instance. |
||
) -> transformation.TransformationCreateResponse: | ||
""" | ||
Creates a new transformation. | ||
|
||
Args: | ||
- new_transformation (NewTransformationModel): A dictionary representing | ||
parameters to create a new transformation. | ||
- config (dict): A dictionary representing query parameters. | ||
Any key-value passed inside this dictionary are sent as query parameters | ||
to the application. | ||
|
||
Retruns: | ||
TransformationCreateResponse: A dictionary containing id of the | ||
created transformation. | ||
""" | ||
params = {} | ||
# send additional key value as query parameters if some app needs it | ||
if config is not None: | ||
params.update(config) | ||
return transformation.TransformationCreateResponse.parse_obj( | ||
self._client.post( | ||
self._proxy_path("newTransformation"), json=new_transformation | ||
self._proxy_path("newTransformation"), | ||
json=new_transformation, | ||
params=params, | ||
).json() | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,70 @@ | |
""" | ||
|
||
import os | ||
from functools import wraps | ||
from urllib.parse import urljoin | ||
|
||
import requests | ||
from keycloak import KeycloakOpenID | ||
from requests import Response | ||
|
||
from .version import __version__ | ||
|
||
MP_DEFAULT_HOST = "https://materials-marketplace.eu/" | ||
|
||
|
||
def configure_token(func): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these methods should have proper docstrings that can help users, rather than inline comments |
||
@wraps(func) | ||
def func_(self, *arg, **kwargs): | ||
r = func(self, *arg, **kwargs) | ||
if r.status_code == 401: | ||
response = configure() | ||
if not response["status"] == "success": | ||
raise Exception( | ||
"User authentication failure. Reason:" + response["message"] | ||
) | ||
token = response["token"] | ||
os.environ["MP_ACCESS_TOKEN"] = token | ||
self.access_token = token | ||
|
||
r = func(self, *arg, **kwargs) | ||
if r.status_code > 400: | ||
raise Exception("Server returned with an Exception. Details: " + r.text) | ||
return r | ||
|
||
return func_ | ||
|
||
|
||
def configure(): | ||
""" | ||
Authenticates a user with marketplace username and password using keycloak | ||
authentication module. If the authentication is succesfull then this method returns | ||
user token within a dictionary. Otherwise, an exception is caught and the | ||
resaon for failure is returned as dict. In order to use keycloak authentication | ||
with username and password we have to configure all the necessary keycloak | ||
environment variables. Configurations can be obtained from market place admin. | ||
""" | ||
# Configure client | ||
server_url = os.environ.get("KEYCLOAK_SERVER_URL") | ||
client_id = os.environ.get("KEYCLOAK_CLIENT_ID") | ||
realm_name = os.environ.get("KEYCLOAK_REALM_NAME") | ||
client_key = os.environ.get("KEYCLOAK_CLIENT_SECRET_KEY") | ||
user = os.environ.get("MARKETPLACE_USERNAME") | ||
passwd = os.environ.get("MARKETPLACE_PASSWORD") | ||
keycloak_openid = KeycloakOpenID( | ||
server_url=server_url, | ||
client_id=client_id, | ||
realm_name=realm_name, | ||
client_secret_key=client_key, | ||
) | ||
try: | ||
token = keycloak_openid.token(user, passwd) | ||
token = token["access_token"] | ||
return {"status": "success", "token": token, "message": ""} | ||
except Exception as e: | ||
return {"status": "failure", "token": None, "message": str(e)} | ||
|
||
|
||
class MarketPlaceClient: | ||
"""Interact with the MarketPlace platform.""" | ||
|
||
|
@@ -24,7 +78,7 @@ def __init__(self, marketplace_host_url=None, access_token=None): | |
"MP_HOST", | ||
MP_DEFAULT_HOST, | ||
) | ||
access_token = access_token or os.environ["MP_ACCESS_TOKEN"] | ||
access_token = access_token or os.environ.get("MP_ACCESS_TOKEN") | ||
|
||
self.marketplace_host_url = marketplace_host_url | ||
self.access_token = access_token | ||
|
@@ -50,6 +104,7 @@ def userinfo(self): | |
userinfo.raise_for_status() | ||
return userinfo.json() | ||
|
||
@configure_token | ||
def _request(self, op, path, **kwargs) -> Response: | ||
kwargs.setdefault("headers", {}).update(self.default_headers) | ||
full_url = urljoin(self.marketplace_host_url, path) | ||
|
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.
Why isn't the default defined at the Standard app API level?
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.
I thought it would be easy to integrate if in case we need a name change for default collection. SInce standardapp requires rebuilding production. But anyway now I think it soesnot make sense to change the name once datasink application is established. I can move it to standard app if needed.