Skip to content
Merged
9 changes: 3 additions & 6 deletions Algorithmia/CLI.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Algorithmia
import os
from Algorithmia.errors import DataApiError
from Algorithmia.errors import DataApiError, AlgorithmException
from Algorithmia.algo_response import AlgoResponse
import json, re, requests, six
import toml
import shutil
from time import time

class CLI:
def __init__(self):
Expand Down Expand Up @@ -309,12 +308,10 @@ def list_languages(self, client):
return table

def getBuildLogs(self, user, algo, client):
api_response = client.algo(user + '/' + algo).build_logs()

if "error" in api_response:
Copy link
Contributor

Choose a reason for hiding this comment

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

what will happen here if there is an error, now that you remove error handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it will just raise the error as per the build_logs() endpoint, previously there was no error handling in the internal build_logs() endpoint, if it was a 500 we needed that to be handled here

return json.dumps(api_response)
api_response = client.algo(user + '/' + algo).get_builds()
return json.dumps(api_response['results'], indent=1)


def getconfigfile(self):
if (os.name == "posix"):
# if!windows
Expand Down
149 changes: 55 additions & 94 deletions Algorithmia/algorithm.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'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, raiseAlgoApiError
from Algorithmia.errors import ApiError, ApiInternalError, raiseAlgoApiError, AlgorithmException
from enum import Enum
from algorithmia_api_client.rest import ApiException
from algorithmia_api_client import CreateRequest, UpdateRequest, VersionRequest, Details, Settings, SettingsMandatory, \
Expand Down Expand Up @@ -40,105 +39,73 @@ def set_options(self, timeout=300, stdout=False, output=OutputType.default, **qu
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}
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)
raise raiseAlgoApiError(error_message)
def create(self, details={}, settings={}, version_info={}, source={}, scmsCredentials={}):
url = "/v1/algorithms/" + self.username
create_parameters = {"name": self.algoname, "details": details, "settings": settings,
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}

api_response = self.client.postJsonHelper(url, create_parameters, parse_response_as_json=True)
return api_response

# 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)
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)
raise raiseAlgoApiError(error_message)
def update(self, details={}, settings={}, version_info={}, source={}, scmsCredentials={}):
url = "/v1/algorithms/" + self.username + "/" + self.algoname
update_parameters = {"details": details, "settings": settings,
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}
api_response = self.client.putHelper(url, update_parameters)
return api_response

# Publish an algorithm
def publish(self, details={}, settings={}, version_info={}):
publish_parameters = {"details": details, "settings": settings, "version_info": version_info}
def publish(self, details={}, settings={}, version_info={}, source={}, scmsCredentials={}):
url = "/v1/algorithms/" + self.username + "/" + self.algoname + "/versions"
print(publish_parameters)
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True,
**self.query_parameters)
publish_parameters = {"details": details, "settings": settings,
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True)
return api_response
# except ApiException as e:
# error_message = json.loads(e.body)
# raise raiseAlgoApiError(error_message)

def builds(self, limit=56, marker=None):
try:
if marker is not None:
api_response = self.client.manageApi.get_algorithm_builds(self.username, self.algoname, limit=limit,
marker=marker)
else:
api_response = self.client.manageApi.get_algorithm_builds(self.username, self.algoname, limit=limit)
return api_response
except ApiException as e:
error_message = json.loads(e.body)
raise raiseAlgoApiError(error_message)
def get_builds(self, limit=56, marker=None):
kwargs = {"limit": limit, "marker": marker}
url = "/v1/algorithms/" + self.username + "/" + self.algoname + '/builds'
response = self.client.getJsonHelper(url, **kwargs)
return response

def get_build(self, build_id):
# Get the build object for a given build_id
# The build status can have one of the following value: succeeded, failed, in-progress
try:
api_response = self.client.manageApi.get_algorithm_build_by_id(self.username, self.algoname, build_id)
return api_response
except ApiException as e:
error_message = json.loads(e.body)
raise raiseAlgoApiError(error_message)
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/builds/' + build_id
response = self.client.getJsonHelper(url)
return response

def get_build_logs(self, build_id):
# Get the algorithm build logs for a given build_id
try:
api_response = self.client.manageApi.get_algorithm_build_logs(self.username, self.algoname, build_id)
return api_response
except ApiException as e:
error_message = json.loads(e.body)
raise raiseAlgoApiError(error_message)

def build_logs(self):
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/builds'
response = json.loads(self.client.getHelper(url).content.decode('utf-8'))
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/builds/' + build_id + '/logs'
response = self.client.getJsonHelper(url)
return response

def get_scm_status(self):
try:
api_response = self.client.manageApi.get_algorithm_scm_connection_status(self.username, self.algoname)
return api_response
except ApiException as e:
error_message = json.loads(e.body)
raise raiseAlgoApiError(error_message)
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/scm/status'
response = self.client.getJsonHelper(url)
return response

# Get info on an algorithm
def info(self, algo_hash=None):
# Get Algorithm
if algo_hash:
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/versions/' + algo_hash
else:
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/versions'
response = self.client.getJsonHelper(url)
return response

# Check if an Algorithm exists
def exists(self):
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)
raise raiseAlgoApiError(error_message)
url = '/v1/algorithms/' + self.username + '/' + self.algoname
_ = self.client.getJsonHelper(url)
return True
except AlgorithmException as e:
print(e)
return False

# Get all versions of the algorithm, with the given filters
def versions(self, limit=None, marker=None, published=None, callable=None):
Expand All @@ -154,23 +121,17 @@ def versions(self, limit=None, marker=None, published=None, callable=None):
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)
raise raiseAlgoApiError(error_message)
# Get Algorithm versions
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/versions'
response = self.client.getJsonHelper(url)
return response

# Compile an algorithm
def compile(self):
try:
# Compile algorithm
api_response = self.client.manageApi.compile_algorithm(self.username, self.algoname)
return api_response
except ApiException as e:
error_message = json.loads(e.body)
raise raiseAlgoApiError(error_message)
# Compile algorithm
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/compile'
response = self.client.postJsonHelper(url, {}, parse_response_as_json=True)
return response

# Pipe an input into this algorithm
def pipe(self, input1):
Expand Down
44 changes: 38 additions & 6 deletions Algorithmia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Algorithmia
from Algorithmia.insights import Insights
from Algorithmia.errors import raiseAlgoApiError
from Algorithmia.algorithm import Algorithm
from Algorithmia.datafile import DataFile, LocalDataFile, AdvancedDataFile
from Algorithmia.datadirectory import DataDirectory, LocalDataDirectory, AdvancedDataDirectory
Expand All @@ -15,6 +16,7 @@
from time import time



class Client(object):
'Algorithmia Common Library'

Expand Down Expand Up @@ -243,10 +245,17 @@ def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query

response = self.requestSession.post(self.apiAddress + url, data=input_json, headers=headers,
params=query_parameters)

if parse_response_as_json and response.status_code == 200:
return response.json()
return response
if 200 <= response.status_code <= 299:
if parse_response_as_json:
response = response.json()
if 'error' in response:
raise raiseAlgoApiError(response)
else:
return response
else:
return response
else:
raise raiseAlgoApiError(response)

# Used internally to http get a file
def getHelper(self, url, **query_parameters):
Expand All @@ -257,6 +266,23 @@ def getHelper(self, url, **query_parameters):
headers['Authorization'] = 'Bearer ' + self.bearerToken
return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters)

def getJsonHelper(self, url, **query_parameters):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
elif self.bearerToken is not None:
headers['Authorization'] = 'Bearer ' + self.bearerToken
response = self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters)
if 200 <= response.status_code <= 299:
response = response.json()
if 'error' in response:
raise raiseAlgoApiError(response)
else:
return response
else:
raise raiseAlgoApiError(response)


def getStreamHelper(self, url, **query_parameters):
headers = {}
if self.apiKey is not None:
Expand Down Expand Up @@ -291,11 +317,17 @@ def putHelper(self, url, data):
headers['Authorization'] = 'Bearer ' + self.bearerToken
if isJson(data):
headers['Content-Type'] = 'application/json'

response = self.requestSession.put(self.apiAddress + url, data=data, headers=headers)
if response._content == b'':
return response
return response.json()
if 200 <= response.status_code <= 299:
response = response.json()
if 'error' in response:
raise raiseAlgoApiError(response)
else:
return response
else:
raise raiseAlgoApiError(response)

# Used internally to http delete a file
def deleteHelper(self, url):
Expand Down
Loading