-
Notifications
You must be signed in to change notification settings - Fork 38
Supporting Algo Management API's #18
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
Changes from all commits
12874eb
b9ed7a5
aab14ab
80df536
3961280
90ab3ca
f3fb7da
04d3352
c4f7798
aa81ef8
0ccdd72
a30991d
27f8872
6f71f8c
314e8e2
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,11 +1,15 @@ | ||
| 'Algorithmia Algorithm API Client (python)' | ||
|
|
||
| import base64 | ||
| import json | ||
| import re | ||
| from Algorithmia.async_response import AsyncResponse | ||
| from Algorithmia.algo_response import AlgoResponse | ||
| from Algorithmia.errors import ApiError, ApiInternalError | ||
| from enum import Enum | ||
| from algorithmia_api_client.rest import ApiException | ||
| from algorithmia_api_client import CreateRequest, UpdateRequest, VersionRequest, Details, Settings, SettingsMandatory, SettingsPublish, \ | ||
| CreateRequestVersionInfo, VersionInfo, VersionInfoPublish | ||
|
|
||
| OutputType = Enum('OutputType','default raw void') | ||
|
|
||
|
|
@@ -17,6 +21,10 @@ def __init__(self, client, algoRef): | |
| if m is not None: | ||
| self.client = client | ||
| self.path = m.group(1) | ||
| self.username = self.path.split("/")[0] | ||
| self.algoname = self.path.split("/")[1] | ||
| if len(self.path.split("/")) > 2: | ||
| self.version = self.path.split("/")[2] | ||
| self.url = '/v1/algo/' + self.path | ||
| self.query_parameters = {} | ||
| self.output_type = OutputType.default | ||
|
|
@@ -29,6 +37,97 @@ def set_options(self, timeout=300, stdout=False, output=OutputType.default, **qu | |
| self.query_parameters.update(query_parameters) | ||
| return self | ||
|
|
||
| # Create a new algorithm | ||
| def create(self, details={}, settings={}, version_info={}): | ||
| detailsObj = Details(**details) | ||
| settingsObj = SettingsMandatory(**settings) | ||
| createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info) | ||
| create_parameters = {"name": self.algoname, "details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj} | ||
kennydaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| create_request = CreateRequest(**create_parameters) | ||
| try: | ||
| # Create Algorithm | ||
| api_response = self.client.manageApi.create_algorithm(self.username, create_request) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
|
||
| # Update the settings in an algorithm | ||
| def update(self, details={}, settings={}, version_info={}): | ||
| detailsObj = Details(**details) | ||
| settingsObj = Settings(**settings) | ||
| createRequestVersionInfoObj = CreateRequestVersionInfo(**version_info) | ||
| update_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": createRequestVersionInfoObj} | ||
| update_request = UpdateRequest(**update_parameters) | ||
|
Contributor
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. Small nit: I think the python default is always snake case?
Contributor
Author
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. Good point. Also as far as I can tell, the client has used both :/ Look at line 19-30. I could change the style for the code I wrote. I personally prefer using underscores. It makes reading the code easier. 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. Per python style guide variables should be snake case: https://www.python.org/dev/peps/pep-0008/#function-and-variable-names |
||
| try: | ||
| # Update Algorithm | ||
| api_response = self.client.manageApi.update_algorithm(self.username, self.algoname, update_request) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
|
||
| # Publish an algorithm | ||
| def publish(self, details={}, settings={}, version_info={}): | ||
| detailsObj = Details(**details) | ||
| settingsObj = SettingsPublish(**settings) | ||
| versionRequestObj = VersionInfoPublish(**version_info) | ||
| publish_parameters = {"details": detailsObj, "settings": settingsObj, "version_info": versionRequestObj} | ||
| version_request = VersionRequest(**publish_parameters) # VersionRequest | Publish Version Request | ||
| try: | ||
| # Publish Algorithm | ||
| api_response = self.client.manageApi.publish_algorithm(self.username, self.algoname, version_request) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
|
||
| # Get info on an algorithm | ||
| def info(self, algo_hash=None): | ||
| try: | ||
| # Get Algorithm | ||
| if algo_hash: | ||
| api_response = self.client.manageApi.get_algorithm_hash_version(self.username, self.algoname, algo_hash) | ||
| else: | ||
| api_response = self.client.manageApi.get_algorithm(self.username, self.algoname) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
Contributor
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 way we're handling exceptions is throwing away information. What about the error code, etc? Probably doesn't belong in this PR, but something to think about. |
||
|
|
||
| # Get all versions of the algorithm, with the given filters | ||
| def versions(self, limit=None, marker=None, published=None, callable=None): | ||
| kwargs = {} | ||
| bools = ["True", "False"] | ||
| if limit: | ||
| kwargs["limit"] = limit | ||
| if marker: | ||
| kwargs["marker"] = marker | ||
| if published: | ||
| p = published | ||
| kwargs["published"] = str(p).lower() if str(p) in bools else p | ||
| if callable: | ||
| c = callable | ||
| kwargs["callable"] = str(c).lower() if str(c) in bools else c | ||
| try: | ||
| # Get Algorithm versions | ||
| api_response = self.client.manageApi.get_algorithm_versions(self.username, self.algoname, **kwargs) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
|
||
|
|
||
| # Compile an algorithm | ||
| def compile(self): | ||
| try: | ||
| # Compile algorithm | ||
| api_response = self.client.manageApi.algorithms_username_algoname_compile_post(self.username, self.algoname) | ||
| return api_response | ||
| except ApiException as e: | ||
| error_message = json.loads(e.body)["error"]["message"] | ||
| raise ApiError(error_message) | ||
|
|
||
| # Pipe an input into this algorithm | ||
| def pipe(self, input1): | ||
|
|
||
|
|
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.
Is it safe to assume here that the algorithm URI structure will always remain the same? Is there a world where this is brittle(supporting unicode algorithm names/usernames?)
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 would assume that if the URI structure ever changes, line 19 would also break (which has been part of the client forever now).
Other than that point, I don't think we'll ever change it.