Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
99 changes: 99 additions & 0 deletions 99 Algorithmia/algorithm.py
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')

Expand All @@ -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]
Copy link
Contributor

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?)

Copy link
Contributor Author

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.

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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit:
Some of your variables are camel case, and some are snake case - I think most of the time you snake case for dicts, but not always. Might be useful to be consistent here.

I think the python default is always snake case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

@kennydaniel kennydaniel Feb 27, 2019

Choose a reason for hiding this comment

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

Expand Down
5 changes: 5 additions & 0 deletions 5 Algorithmia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from Algorithmia.algorithm import Algorithm
from Algorithmia.datafile import DataFile
from Algorithmia.datadirectory import DataDirectory
from algorithmia_api_client import Configuration, DefaultApi, ApiClient

import json, re, requests, six
import os
Expand All @@ -23,6 +24,10 @@ def __init__(self, apiKey = None, apiAddress = None):
self.apiAddress = apiAddress
else:
self.apiAddress = Algorithmia.getApiAddress()
config = Configuration()
config.api_key['Authorization'] = self.apiKey
config.host = "{}/v1".format(self.apiAddress)
self.manageApi = DefaultApi(ApiClient(config))

def algo(self, algoRef):
return Algorithm(self, algoRef)
Expand Down
3 changes: 2 additions & 1 deletion 3 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
install_requires=[
'requests',
'six',
'enum34'
'enum34',
'algorithmia-api-client>=1.0.0,<2.0'
],
include_package_data=True,
classifiers=[
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.