Giter Site home page Giter Site logo

algorithmia-java's Introduction

algorithmia-java

Java client for accessing Algorithmia's algorithm marketplace and data APIs.

Algorithmia Client Java Docs

Latest Release

Getting started

The Algorithmia java client is published to Maven central and can be added as a dependency via:

<dependency>
  <groupId>com.algorithmia</groupId>
  <artifactId>algorithmia-client</artifactId>
  <version>[,1.1.0)</version>
</dependency>

Instantiate a client using your API Key:

AlgorithmiaClient client = Algorithmia.client(apiKey);

Notes:

  • API key may be omitted only when making calls from algorithms running on the Algorithmia cluster
  • Using version range [,1.1.0) is recommended as it implies using the latest backward-compatible bugfixes.

Now you are ready to call algorithms.

Calling Algorithms

The following examples of calling algorithms are organized by type of input/output which vary between algorithms.

Note: a single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm's description for usage examples specific to that algorithm.

Text input/output

Call an algorithm with text input by simply passing a string into its pipe method. If the algorithm output is text, call the asString method on the response.

Algorithm algo = client.algo("algo://demo/Hello/0.1.1");
AlgoResponse result = algo.pipe("HAL 9000");
System.out.println(result.asString());
// -> Hello HAL 9000

JSON input/output

Call an algorithm with JSON input by simply passing in a type that can be serialized to JSON, including most plain old java objects and collection types. If the algorithm output is JSON, call the as method on the response with a TypeToken containing the type that it should be deserialized into:

Algorithm algo = client.algo("algo://WebPredict/ListAnagrams/0.1.0");
List<String> words = Arrays.asList(("transformer", "terraforms", "retransform");
AlgoResponse result = algo.pipe(words);
// WebPredict/ListAnagrams returns an array of strings, so cast the result:
List<String> anagrams = result.as(new TypeToken<List<String>>(){});
// -> List("transformer", "retransform")

Alternatively, you may work with raw JSON input by calling pipeJson, and raw JSON output by calling asJsonString on the response:

String jsonWords = "[\"transformer\", \"terraforms\", \"retransform\"]";
AlgoResponse result2 = algo.pipeJson(jsonWords);
String anagrams = result2.asJsonString();
// -> "[\"transformer\", \"retransform\"]"

Double durationInSeconds = response.getMetadata().duration;

Binary input/output

Call an algorithm with binary input by passing a byte[] into the pipe method. If the algorithm response is binary data, then call the as method on the response with a byte[] TypeToken to obtain the raw byte array.

byte[] input = Files.readAllBytes(new File("/path/to/bender.jpg").toPath());
AlgoResponse result = client.algo("opencv/SmartThumbnail/0.1").pipe(input);
byte[] buffer = result.as(new TypeToken<byte[]>(){});
// -> [byte array]

Error handling

API errors will result in the call to pipe throwing APIException. Errors that occur durring algorithm execution will result in AlgorithmException when attempting to read the response.

Algorithm algo = client.algo('util/whoopsWrongAlgo')
try {
    AlgoResponse result = algo.pipe('Hello, world!');
    String output = result.asString();
} catch (APIException ex) {
    System.out.println("API Exception: " ex.getMessage());
} catch (AlgorithmException ex) {
    System.out.println("Algorithm Exception: " ex.getMessage());
    System.out.println(ex.stacktrace);
}

Request options

The client exposes options that can configure algorithm requests. This includes support for changing the timeout or indicating that the API should include stdout in the response.:

Algorithm algo = client.algo("algo://demo/Hello/0.1.1")
                         .setTimeout(1, TimeUnit.MINUTES)
                         .setStdout(true);
AlgoResponse result = algo.pipe("HAL 9000");
Double stdout = response.getMetadata().stdout;

Note: setStdout(true) is ignored if you do not have access to the algorithm source.

Working with Data

The Algorithmia Java client also provides a way to manage both Algorithmia hosted data and data from Dropbox or S3 accounts that you've connected to you Algorithmia account.

This client provides a DataFile type (generally created by client.file(uri)) and a DataDir type (generally created by client.dir(uri)) that provide methods for managing your data.

Create directories

Create directories by instantiating a DataDirectory object and calling create():

DataDirectory robots = client.dir("data://.my/robots");
robots.create();

DataDirectory dbxRobots = client.dir("dropbox://robots");
dbxRobots.create();

Upload files to a directory

Upload files by calling put on a DataFile object, or by calling putFile on a DataDirectory object.

DataDirectory robots = client.dir("data://.my/robots");

// Upload local file
robots.putFile(new File("/path/to/Optimus_Prime.png"));
// Write a text file
robots.file("Optimus_Prime.txt").put("Leader of the Autobots");
// Write a binary file
robots.file("Optimus_Prime.key").put(new byte[] { (byte)0xe0, 0x4f, (byte)0xd0, 0x20 });

Download contents of file

Download files by calling getString, getBytes, or getFile on a DataFile object:

DataDirectory robots = client.dir("data://.my/robots");

// Download file and get the file handle
File t800File = robots.file("T-800.png").getFile();

// Get the file's contents as a string
String t800Text = robots.file("T-800.txt").getString();

// Get the file's contents as a byte array
byte[] t800Bytes = robots.file("T-800.png").getBytes();

Delete files and directories

Delete files and directories by calling delete on their respective DataFile or DataDirectory object. DataDirectories take an optional force parameter that indicates whether the directory should be deleted if it contains files or other directories.

client.file("data://.my/robots/C-3PO.txt").delete();
client.dir("data://.my/robots").delete(false);

List directory contents

Iterate over the contents of a directory using the iterator returned by calling files, or dirs on a DataDirectory object:

// List top level directories
DataDirectory myRoot = client.dir("data://.my");
for(DataDirectory dir : myRoot.dirs()) {
    System.out.println("Directory " + dir + " at URL " + dir.url());
}

// List files in the 'robots' directory
DataDirectory robots = client.dir("data://.my/robots");
for(DataFile file : robots.files()) {
    System.out.println("File " + file + " at URL: " + file.url());
}

Manage directory permissions

Directory permissions may be set when creating a directory, or may be updated on already existing directories.

DataDirectory fooLimited = client.dir("data://.my/fooLimited");

// Create the directory as private
fooLimited.create(DataAcl.PRIVATE);

// Update a directory to be public
fooLimited.updatePermissions(DataAcl.PUBLIC);

// Check a directory's permissions
if (fooLimited.getPermissions().getReadPermissions() == DataAclType.PRIVATE) {
    System.out.println("fooLimited is private");
}

Java Algo development category API's

Name Parameters Example
Create Algorithm String userName - Your Algorithmia user name.
String requestString - JSON payload for the Algorithm you wish to create.
Algorithm newAlgorithm = Algorithmia.client(key).createAlgo(userName, requestString);
Get Algorithm String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
Algorithm algorithm = Algorithmia.client(key).getAlgo(userName, algoName);
Compile Algorithm String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
Algorithm algorithm = Algorithmia.client(key).compileAlgo(userName, algoName);
Publish Algorithm String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
String requestString - JSON payload for the Algorithm you wish to publish.
Algorithm newAlgorithm = Algorithmia.client(key).publishAlgo(userName, algoName, requestString);
List Algorithm Versions String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
Boolean callable - Whether to return only public or private algorithm versions.
Integer limit - Items per page.
Boolean published - Whether to return only versions that have been published.
String marker - Marker for pagination.
AlgorithmVersionsList algoList = Algorithmia.client(key).listAlgoVersions(userName, algoName, callable, limit, published, marker)
Update Algorithm String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
String requestString - JSON payload for the Algorithm you wish to create.
Algorithm newAlgorithm = Algorithmia.client(key).updateAlgo(userName, algoName, requestString);
Execute Algorithm String algoName - The name address of the algorithm. Algorithm algo = client.algo("algo://demo/Hello/0.1.1");
AlgoResponse result = algo.pipe("HAL 9000");
Get Algorithm Build Logs String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
String buildId - The id of the build to retrieve logs.
BuildLogs buildLogs = Algorithmia.client(key).getAlgoBuildLogs(userName, algoName, buildId)
Create Directory String path - Path to a data directory. DataDirectory robots = client.dir("data://.my/robots");
robots.create();
List Directory Contents String path - Path to a data directory. DataDirectory myRoot = client.dir("data://.my");
for(DataDirectory dir : myRoot.dirs()) { System.out.println("Directory " + dir + " at URL " + dir.url()); }
Update Directory File file - A file to put into this data directory. DataDirectory robots = client.dir("data://.my/robots");
robots.putFile(new File("/path/to/Optimus_Prime.png"));
Delete Directory boolean forceDelete - Forces deletion of the directory if it contains files. client.dir("data://.my/robots").delete(false);
Upload File File file - file the file to upload data from. robots.putFile(new File("/path/to/Optimus_Prime.png"));
Verify File Existence - if(file.exists()) { file.delete(); }
Download File - File t800File = robots.file("T-800.png").getFile();
Report Insights String input - JSON payload key-value pairs AlgorithmiaInsights insightsResponse = Algorithmia.client(defaultKey).reportInsights(input);

Java CICD Automation and Admin Automation category API's

Name Parameters Example
List Algorithm Builds String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
Integer limit - Items per page.
String marker - Marker for pagination.
AlgorithmBuildsList algoList = Algorithmia.client(defaultKey).listAlgoBuilds(userName, algoName, ?, ?);
Get Algorithm Build String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
String buildId - The id of the build to retrieve.
Algorithm.Build returnedBuild = Algorithmia.client(defaultKey).getAlgoBuild(userName, algoName, buildId);
Delete Algorithm String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
HttpResponse response = Algorithmia.client(defaultKey).deleteAlgo(userName, algoName);
Get Algorithm SCM status String userName - Your Algorithmia user name.
String algoName - The name address of the algorithm.
AlgorithmSCMStatus scmStatus = Algorithmia.client(defaultKey).getAlgoSCMStatus(userName, algoName);
Get SCM String scmId - The id of scm to retrive Algorithm.SCM scm = Algorithmia.client(defaultKey).getSCM(scmId);
List Cluster SCM’s - AlgorithmSCMsList algorithmSCMsList = Algorithmia.client(defaultKey).listSCMs();
Query SCM Authorization Status String scmId - The id of scm status to retrive AlgorithmSCMAuthorizationStatus algorithmSCMAuthorizationStatus = Algorithmia.client(defaultKey).querySCMStatus("github");
Create User String requestString - JSON payload for the user to be created. User newUser = Algorithmia.client(adminKey, testAddress).createUser(json);
Create Organization String requestString - JSON payload for the organization to be created. Organization newOrganization = Algorithmia.client(adminKey, testAddress).createOrganization(json);
Add Organization Member String orgName - The organization name.
String userName - the users algorithmia user name.
HttpResponse response = Algorithmia.client(adminKey, testAddress).addOrganizationMember(orgName, userName);

algorithmia-java's People

Contributors

anowell avatar asaydin avatar besirkurtulmus avatar daleherring avatar dinoboy197 avatar jamesatha avatar john-bragg avatar kennydaniel avatar platypii avatar pmcq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithmia-java's Issues

Algorithmia.client() call fails

I have been unable to connect to Algorithmia after many different attempts. I even created a new key with full access. The call below throws the exception below.

final AlgorithmiaClient client = Algorithmia.client("MY_KEY");

Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT
at org.apache.http.impl.nio.client.HttpAsyncClientBuilder.build(HttpAsyncClientBuilder.java:666)
at com.algorithmia.client.HttpClient.(HttpClient.java:79)
at com.algorithmia.AlgorithmiaClient.(AlgorithmiaClient.java:19)
at com.algorithmia.Algorithmia.client(Algorithmia.java:49)
at com.steele.algorithmia.DataAPI.main(DataAPI.java:19)

com.algorithmia.APIException: 308 unexpected API response

I'm getting this exception while executing the example code found on your website.

Exception in thread "main" com.algorithmia.APIException: 308 unexpected API response: 
	at com.algorithmia.algo.Algorithm.pipeRequest(Algorithm.java:145)
	at com.algorithmia.algo.Algorithm.pipe(Algorithm.java:96)

Have tried the same using cURL and everything works fine, even with python so my guessing it has something to do with HttpClient or Java itself.

[Details]

  • IDE: IntelliJ ULTIMATE 2019.1
    Build #IU-191.6183.87, built on March 27, 2019
    JRE: 1.8.0_202-release-1483-b39 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 10 10.0

Thanks for any help you can provide.
Tomás.

useSystemProperties breaks custom connection pooling

In this builder chain, the call to useSystemProperties() effectively results in ignoring the values set by setMaxConnTotal(maxConnections) and setMaxConnPerRoute(maxConnections). And this isn't order-dependent, because the actual properties used by the underlying client are determined during the call to build with this code in the upstream Apache client

So I see basically 3 choices:

  1. Stop calling useSystemProperties(). This will require figuring out how to go back and add proxy support that motivated useSystemProperties.

  2. Explicitly call System.setProperty("http.keepAlive", "true") and System.setProperty("http.maxConnections", maxConnections) before building the client. (Somebody with a bit more java mastery wanna weigh in on the side affects of setting properties like this? Any risks to creating multiple AlgorithmiClient instances with different connection pooling?). This also means that we have to accept that maxConnTotal is always 2x the maxConnPerRoute.

  3. PR to try and convince upstream to replace that block with one where it only uses the system properties for those values if they weren't explicitly set. (I presume we could use the 2nd option in the meantime of waiting for such a PR to land.)

Wanna weigh in @kennydaniel, since I believe you green-lighted the useSystemProperties change?

Broken Maven deployment dependencies - sbt-pgp and sbt-sonatype

I get these errors when including the algorithmia jar in my pom file:

[WARNING] The POM for org.xerial.sbt:sbt-sonatype:jar:1.1 is missing, no dependency information available
[WARNING] The POM for com.jsuereth:sbt-pgp:jar:1.0.0 is missing, no dependency information available

This causes any maven build (compile/install/package etc) to fail when Algorithmia is included in the JAR, on machines with empty local .m2 repositories.

Upon further inspection, those two jars seem to be intended for deploying/publishing jar files to various repos. I don't think they're actually meant to be part of the library?

The main issue is that these libraries are unavailable on Maven's central repo. In fact xerial/sbt-pack#37 explicitly mentions that the library is not intended to support maven integration. So fresh Maven builds are unable to find the JAR and thus can't finish the build.

Currently I'm able to bypass this issue by manually excluding the two libraries:

                <exclusion>
                    <groupId>org.xerial.sbt</groupId>
                    <artifactId>sbt-sonatype</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.jsuereth</groupId>
                    <artifactId>sbt-pgp</artifactId>
                </exclusion>

This doesn't seem to break anything and disables the warning. Again, my assumption is these two libraries aren't actually used in the Algorithmia code itself, and are only used in the deploy process. Hence, when they are mistakenly included as actual dependencies, maven builds will crash after being unable to find them. But, manually excluding them doesn't cause any problems since the classes aren't actually used.

I'm not sure if anything is actually wrong here, maybe this is intended. But I would really appreciate a second look since the exclusion workaround is not ideal!

DataFile.put Documentation and Usage

It would be nice if the documentation for DataFile mentioned that these methods are blocking.

Also, I'm not sure what the intended usage of the put methods are. InputStreams are typically read from, whereas with the put method we are trying to write to the file. It seems much more "usual" to use an OutputStream, but you're clearly using an API that wants an InputStream.

Not sure how to write to a file using this API, and the documentation doesn't explain much.

* Upload raw data to this file as an input stream

Prefix string too short exception when getting "data://"

Better error message needed for:

File file = Algorithmia.file(imageUrl).getFile();

throws:

Error: java.lang.IllegalArgumentException: Prefix string too short
java.lang.IllegalArgumentException: Prefix string too short
    at java.io.File.createTempFile(File.java:2001)
    at java.io.File.createTempFile(File.java:2070)
    at com.algorithmia.data.DataFile.getFile(DataFile.java:47)
    at algorithmia.sfw.NudityDetectionHelper.ObjectDetectionWithModels.apply(ObjectDetectionWithModels.java:39)
    at algorithmia.sfw.NudityDetectionHelper.NudityDetectionHelper.apply(NudityDetectionHelper.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at algorithmia.runners.JavaRunner$.applyInput(JavaRunner.scala:234)
    at algorithmia.runners.JavaRunner$.algorithmia$runners$JavaRunner$$tryNativeApply(JavaRunner.scala:185)
    at algorithmia.runners.JavaRunner$$anonfun$applyString$1.apply(JavaRunner.scala:30)
    at algorithmia.runners.JavaRunner$$anonfun$applyString$1.apply(JavaRunner.scala:27)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
    at algorithmia.runners.JavaRunner$.applyString(JavaRunner.scala:27)
    at algorithmia.runners.JarRunner.run(JarRunner.scala:52)
    at algorithmia.runners.AlgoRunner$$anonfun$invoke$1.apply$mcV$sp(AlgoRunner.scala:27)
    at algorithmia.runners.AlgoRunner$$anonfun$invoke$1.apply(AlgoRunner.scala:23)
    at algorithmia.runners.AlgoRunner$$anonfun$invoke$1.apply(AlgoRunner.scala:23)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Missing URL encoding of data_uri

This line fails:
Algorithmia.client().file("data://.my/This is a valid filename ☘.txt").put("hi");

The client should perform a URLEncode operation prior to sending HTTP requests for data_uris

AlgoFailure interface

Current:
if(result.isFailure(){
AlgorithmException e = ((AlgoFailure)result).error
e.getMessage();
e.printStackTrace();
}


This isn't a good experience and does not surface all of the available data in the response

Needed:
if(result.isFailure(){
AlgoFailure fail = (AlgoFailure)result;
fail.getMessage();
fail.getStacktrace();
fail.getMetadata().getDuration();
fail.getMetadata().getStdout();
}

AlgorithmiaClient cli = Algorithmia.client(GlobalVariables.ALGORITHMIA_KEY); fails

this simple call doesn't seem to work anymore:

AlgorithmiaClient cli = Algorithmia.client(GlobalVariables.ALGORITHMIA_KEY);

getting an error:

com.algorithmia.APIException: Host name 'api.algorithmia.com' does not match the certificate subject provided by the peer (CN=*.algorithmia.com)

at com.algorithmia.client.HttpClient.execute(HttpClient.java:244)
at com.algorithmia.client.HttpClient.execute(HttpClient.java:211)
at com.algorithmia.client.HttpClient.put(HttpClient.java:163)
at com.algorithmia.data.DataFile.put(DataFile.java:154)
at com.algorithmia.data.DataFile.put(DataFile.java:165)
at com.algorithmia.data.DataDirectory.putFile(DataDirectory.java:90)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.