Simple files howto

This is a simple walkthrough demonstrating how to:

1) Upload a file

2) Retrieve a file

3) Retrieve all files

3) Retrieve all files

4) Request file conversions

5) Handle JsonNode results

First we need to create a CanorisResourceManager object. This object performs all communication with the Canoris API.

CanorisResourceManager manager = new CanorisResourceManager();

1) Upload a file

To upload a file simply do:

CanorisFile file = (CanorisFile) manager.createFile(filePath, null)

filePath: the path where the file is located. For the moment you can ignore the second parameter since it is not used.

This call will return a CanFile object, you will have to cast it since the method returns a CanorisResource object. Refer to ADD_LINK_TO_JAVADOC for the structure of the CanorisFile object.

2) Retrieve a file

To retrieve a single file you can either use it’s file key as an identifier or pass the whole file to be retrieved (containing already the fileKey). 3) Retrieve all files Create a file using it’s file key constructor

CanorisFile file = new CanorisFile("<file_key>");

Then use the CanorisResourceManager to retrieve/populate the file like this:

CanorisFile canFile = manager.getFile(file);

Else you can just use the same method like this:

CanorisFile canFile = manager.getFile(<fileKey>);

If the operation is succesful you can access the various file properties in this manner.

Double fs = (Double) canFile.getProperties().get("samplerate");

Please refer to the JAVADOC_LINK for a more detailed description of the CanorisFile class.

3) Retrieve all files

To get the all files that we have uploaded we simply do:

Page pager = manager.getFiles();

The pager object can be used to access the files and navigate to other pages (if any).

4) Request file conversions

To request a conversion we have to of course first obtain the file. Refer to previous sections for that. Once we have the file we can get its conversions like this:

Map<String,String> conversions = canorisFile.getConversions();

We can then ask for a specific conversion using the following code:

InputStream in = manager.getConversion(file, "mp3_128kb");

5) Handle JsonNode results

In some cases the response contains a deeply nested Json response. In those we return a JsonNode object. Here’s an example:

String template = "[{\"operation\": \"vocaloid\", \"parameters\": {\"input\": \"{{ test_input }}\"}}]";
JsonNode jsonResponse = manager.updateTemplate(template, "test");

Of course the jsonResponse will be null if template with the given name is found. If a template is found you can access its properties in the following way:

jsonResponse.findValue("input").toString();

This of course leaves open the possibility of a null pointer exception so you should always check for the existence of the property you are trying to access.

Table Of Contents

Previous topic

Common Setup

Next topic

Exception Handling

This Page