Giter Club home page Giter Club logo

tut-algorithmia-swift's Introduction

#algorithmia

A Swift/iOS client library for the Algorithmia API

Note: This client was written with Swift 3.0 (Xcode 8, iOS 10)

For API documentation, see the SwiftDocs

CocoaPods CocoaPods

Getting started

Installation

CocoaPods

You can install via CocoaPods by adding it to your Podfile:

use_frameworks!

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'

pod 'algorithmia'

And run pod install.

Manual

You can clone the repo and drag & copy files in Algorithmia

API Key

Instantiate a client using your API Key:

let client = Algorithmia.client(simpleKey: apiKey)

Notes:

  • API key may be omitted only when making calls from algorithms running on the Algorithmia cluster

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, then call getText method to get result as string.

let foo = client.algo(algoUri: "algo://demo/Hello/0.1.1")
foo.pipe(text: "foo") { resp, error in
    if (error == nil) {
        let data = resp.getText()
        let metadata = resp.getMetadata()
    } else {
        print(error)
    }
}

JSON input/output

Call an algorithm with JSON input by simply passing in a type that can be serialized to JSON: most notably python dicts and arrays. For algorithms that return JSON, call getJson method of the response to get the appropriate deserialized type.

let foo = client.algo(algoUri: "algo://WebPredict/ListAnagrams/0.1.0")
foo.pipe(json: jsonData) { resp, error in
    if (error == nil) {
        let data = resp.getJSON()
        let metadata = resp.getMetadata()
    } else {
        print(error)
    }
}

If you already have serialzied JSON, you can call as follows:

let foo = client.algo("")
let jsonWords = "[\"transformer\", \"terraforms\", \"retransform\"]"
foo.pipe(rawJson: jsonWords) { resp, error in

}

Binary input/output

Call an algorithm with Binary input by passing a Data Object into the pipe method. Similarly, if the algorithm response is binary data, then result of getData method will be a Data object from byte array.

let foo = client.algo(algoUri: "algo://WebPredict/ListAnagrams/0.1.0")
foo.pipe(data: data) { resp, error in
    if (error == nil) {
        let data = resp.getData()
        let metadata = resp.getMetadata()
    } else {
        print(error)
    }
}

Error handling

API errors and Algorithm exceptions will return AlgoError in callback to pipe method:

let foo = client.algo(algoUri: "algo://util/whoopsWrongAlgo")
foo.pipe(text: "foo") { resp, error in
    if let error = error as? AlgoError {
        switch error {
        case .ProcessError:
            print("Algorithmia Error:",error)
            break;
        case .DataError:
            print("Data Error:",error)
            break;
        default:
            break;
        }
    }
}
// Algorithmia Error: algorithm algo://util/whoopsWrongAlgo not found

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 AlgoDataFile type (generally created by client.file(uri)) and a AlgoDataDirectory type (generally created by client.dir(uri)) that provide methods for managing your data.

Create directories

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

let robots = client.dir("data://.my/robots")
robots.create { error in
...
}

let dbxRobots = client.dir("dropbox://robots")
dbxRobots.create { error in
...
}

Upload files to a directory

Upload files by calling put on a AlgoDataFile object, or by calling putFile on a AlgoDataDirectory object.

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

// Upload local file
robots.putFile(file:fileURL) { file, error in
...
}

// Write a text file
robots.file("Optimus_Prime.txt").put(string:"Leader of the Autobots") { error in
...
}

Download contents of file

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

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

// Download file and get the file handle
robots.file("T-800.png").getFile { url, error in
...
}

// Get the file's contents as a string
robots.file("T-800.txt").getString { text, error in
	...
}
// Get the file's contents as a byte array
robots.file("T-800.dat").getData(completion: { (text, error) in
	...
})

Delete files and directories

Delete files and directories by calling delete on their respective AlgoDataFile or AlgoDataDirectory object. AlgoDataDirectory 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() { error in
...
}
client.dir("data://.my/robots").delete(force: false) { result, error in
...
}

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
let myRoot = client.dir("data://.my")
myRoot.forEach(file: { file in
        ...
    }, completion: { error in
        ...
})

// List files in the 'robots' directory
myRoot.forEach(dir: { file in
        ...
    }, completion: { error in
        ...
})

Manage directory permissions

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

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

// Create the directory as private
fooLimited.create(readACL:.PRIVATE) { error in
	...
}

// Update a directory to be public
fooLimited.update(readACL:.PUBLIC) { error in
	...
}

tut-algorithmia-swift's People

Contributors

erik-ilyin avatar anowell avatar peckjon avatar panzerstadt avatar

Watchers

James Cloos avatar  avatar  avatar

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.