@@ -9,7 +9,7 @@ Java client for accessing Algorithmia's algorithm marketplace and data APIs.
99
1010[ ![ Latest Release] ( https://img.shields.io/maven-central/v/com.algorithmia/algorithmia-client.svg )] ( http://repo1.maven.org/maven2/com/algorithmia/algorithmia-client/ )
1111
12- # Getting started
12+ ## Getting started
1313
1414The Algorithmia java client is published to Maven central and can be added as a dependency via:
1515
@@ -31,70 +31,99 @@ Notes:
3131- API key may be omitted only when making calls from algorithms running on the Algorithmia cluster
3232- Using version range ` [,1.1.0) ` is recommended as it implies using the latest backward-compatible bugfixes.
3333
34+ Now you are ready to call algorithms.
35+
3436## Calling Algorithms
3537
36- Algorithms are called with the ` pipe ` method using
37- any input that can be serialized to JSON, or binary byte data.
38+ The following examples of calling algorithms are organized by type of input/output which vary between algorithms.
39+
40+ 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.
41+
42+ ### Text input/output
43+
44+ Call an algorithm with text input by simply passing a string into its ` pipe ` method.
45+ If the algorithm output is text, call the ` asString ` method on the response.
3846
3947``` java
40- Algorithm addOne = client. algo(" docs/JavaAddOne " );
41- AlgoResponse response = addOne . pipe(41 );
42- Integer result = response . as( new TypeToken< Integer > (){} );
43- Double durationInSeconds = response . getMetadata() . duration;
48+ Algorithm algo = client. algo(" algo://demo/Hello/0.1.1 " );
49+ AlgoResponse result = algo . pipe(" HAL 9000 " );
50+ System . out . println( result. asString() );
51+ // -> Hello HAL 9000
4452```
4553
46- If you already have serialzied JSON, you can call call ` pipeJson ` instead:
54+ ### JSON input/output
55+
56+ Call an algorithm with JSON input by simply passing in a type that can be serialized to JSON,
57+ including most plain old java objects and collection types.
58+ If the algorithm output is JSON, call the ` as ` method on the response with a ` TypeToken `
59+ containing the type that it should be deserialized into:
4760
4861``` java
49- Algorithm foo = client. algo(" " )
50- String jsonWords = " [\" transformer\" , \" terraforms\" , \" retransform\" ]"
51- AlgoResponse response = addOne. pipeJson(jsonWords)
62+ Algorithm algo = client. algo(" algo://WebPredict/ListAnagrams/0.1.0" );
63+ List<String > words = Arrays . asList((" transformer" , " terraforms" , " retransform" );
64+ AlgoResponse result = algo. pipe(words);
65+ // WebPredict/ListAnagrams returns an array of strings, so cast the result:
66+ List<String > anagrams = result. as(new TypeToken<List<String > > (){});
67+ // -> List("transformer", "retransform")
5268```
5369
54- You can also set options (query parameters in the API spec) on calls. There are several approaches to do this, and they are all equivalent
70+ Alternatively , you may work with raw JSON input by calling `pipeJson`,
71+ and raw JSON output by calling `asJsonString` on the response:
72+
5573```java
56- // Helper methods for specific parameters in the API spec:
57- Algorithm addOne = client . algo( " docs/JavaAddOne " )
58- .setTimeout( 5 , TimeUnit . MINUTES )
59- .setStdout( false );
74+ String jsonWords = " [ \" transformer \" , \" terraforms \" , \" retransform \" ] "
75+ AlgoResponse result2 = algo. pipeJson(jsonWords);
76+ String anagrams = result2 . asJsonString();
77+ // -> "[\"transformer\", \"retransform\"]"
6078
61- AlgoResponse response = addOne . pipe( 41 ) ;
79+ Double durationInSeconds = response . getMetadata() . duration ;
6280```
6381
64- ### Casting results in Java
6582
83+ ### Binary input/ output
6684
67- > For an algorithm that returns a string:
85+ Call an algorithm with binary input by passing a `byte []` into the `pipe` method.
86+ If the algorithm response is binary data, then call the `as` method on the response with a `byte []` `TypeToken `
87+ to obtain the raw byte array.
6888
6989```java
70- stringResult. as(new TypeToken<String > (){});
90+ byte [] input = Files . readAllBytes(new File (" /path/to/bender.jpg" ). toPath());
91+ AlgoResponse result = client. algo(" opencv/SmartThumbnail/0.1" ). pipe(input);
92+ byte [] buffer = result. as(new TypeToken<byte[]> (){});
93+ // -> [byte array]
7194```
7295
73- > For an algorithm that returns an array of strings:
96+ ### Error handling
7497
75- ``` java
76- stringArrayResult. as(new TypeToken<List<String > > (){});
77- ```
78-
79- > For an algorithm that returns a custom class, cast the result to that class:
98+ API errors will result in the call to `pipe` throwing `APIException `.
99+ Errors that occur durring algorithm execution will result in `AlgorithmException ` when attempting to read the response.
80100
81101```java
82- class CustomClass {
83- int maxCount;
84- List<String > items;
102+ Algorithm algo = client. algo(' util/whoopsWrongAlgo' )
103+ try {
104+ AlgoResponse result = algo. pipe(' Hello, world!' );
105+ String output = result. asString();
106+ } catch (APIException ex) {
107+ System . out. println(" API Exception: " ex. getMessage());
108+ } catch (AlgorithmException ex) {
109+ System . out. println(" Algorithm Exception: " ex. getMessage() + " \n " + ex. stacktrace);
85110}
86- customClassResult. as(new TypeToken<CustomClass > (){});
87111```
88112
89- > For debugging, it is often helpful to get the JSON String representation of the result:
113+ ### Request options
114+
115+ The client exposes options that can configure algorithm requests.
116+ This includes support for changing the timeout or indicating that the API should include stdout in the response. :
90117
91118```java
92- anyResult. asJsonString();
119+ Algorithm algo = client. algo(" algo://demo/Hello/0.1.1" )
120+ .setTimeout(1 , TimeUnit . MINUTES )
121+ .setStdout(true );
122+ AlgoResponse result = algo. pipe(" HAL 9000" );
123+ Double stdout = response. getMetadata(). stdout;
93124```
94125
95- In order to cast the result to a specific type, call ` .as() ` with a TypeToken.
96- On the right pane, you'll find examples of how to do this to return a string, an array of strings, and a custom class.
97-
126+ Note : `setStdout(true )` is ignored if you do not have access to the algorithm source.
98127
99128## Working with Data
100129
0 commit comments