|
| 1 | +;(function() { |
| 2 | + let Algorithm |
| 3 | + let Client |
| 4 | + let Promise |
| 5 | + let algoPattern |
| 6 | + let getDefaultApiAddress |
| 7 | + |
| 8 | + window.Algorithmia = window.Algorithmia || {} |
| 9 | + |
| 10 | + if (Algorithmia.query) { |
| 11 | + console.error('Warning: algorithmia.js loaded twice') |
| 12 | + } |
| 13 | + |
| 14 | + if (Algorithmia.client) { |
| 15 | + console.error('Warning: algorithmia.js loaded twice') |
| 16 | + } |
| 17 | + |
| 18 | + if (Algorithmia.apiAddress) { |
| 19 | + console.log(`Using alternate API server: ${Algorithmia.apiAddress}`) |
| 20 | + } |
| 21 | + |
| 22 | + getDefaultApiAddress = function() { |
| 23 | + if (Algorithmia.apiAddress !== void 0) { |
| 24 | + return Algorithmia.apiAddress |
| 25 | + } |
| 26 | + return 'https://api.algorithmia.com/v1/web/algo' |
| 27 | + } |
| 28 | + |
| 29 | + algoPattern = /^(?:algo:\/\/|\/|)(\w+\/.+)$/ |
| 30 | + |
| 31 | + Algorithmia.query = function(algo_uri, api_key, input, cb) { |
| 32 | + return Algorithmia.client(api_key) |
| 33 | + .algo(algo_uri) |
| 34 | + .pipe(input, function(result) { |
| 35 | + if (result.error) { |
| 36 | + return cb(result.error.message || result.error) |
| 37 | + } |
| 38 | + return cb(void 0, result.result) |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + Algorithmia.client = function(api_key, api_address) { |
| 43 | + api_key = api_key || Algorithmia.apiKey |
| 44 | + api_address = api_address || getDefaultApiAddress() |
| 45 | + return new Client(api_key, api_address) |
| 46 | + } |
| 47 | + |
| 48 | + Client = function(api_key, api_address) { |
| 49 | + this.api_key = api_key |
| 50 | + this.api_address = api_address |
| 51 | + this.algo = function(algo_uri) { |
| 52 | + if (algo_uri && typeof algo_uri === 'string') { |
| 53 | + return new Algorithm(this, algo_uri) |
| 54 | + } |
| 55 | + console.error(`Invalid algorithm url: ${algo_uri}`) |
| 56 | + return null |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + Algorithmia.algo = function(algo_uri) { |
| 61 | + return Algorithmia.client().algo(algo_uri) |
| 62 | + } |
| 63 | + |
| 64 | + Algorithm = function(client, algo_uri) { |
| 65 | + if (!(typeof algo_uri === 'string' && algo_uri.match(algoPattern))) { |
| 66 | + throw 'Invalid Algorithm URI (expected /owner/algo)' |
| 67 | + } |
| 68 | + this.client = client |
| 69 | + this.algo_uri = algo_uri.match(algoPattern)[1] |
| 70 | + this.pipe = function(input, cb) { |
| 71 | + let promise |
| 72 | + Algorithmia.startTask() |
| 73 | + promise = new Promise( |
| 74 | + (function(_this) { |
| 75 | + return function(resolve) { |
| 76 | + let endpoint_url |
| 77 | + let xhr |
| 78 | + xhr = new XMLHttpRequest() |
| 79 | + xhr.onreadystatechange = function() { |
| 80 | + let error |
| 81 | + let responseJson |
| 82 | + if (xhr.readyState === 4) { |
| 83 | + Algorithmia.finishTask() |
| 84 | + if (xhr.status === 0) { |
| 85 | + if (xhr.responseText) { |
| 86 | + resolve({ |
| 87 | + error: `API connection error: ${xhr.responseText}`, |
| 88 | + }) |
| 89 | + } else { |
| 90 | + resolve({ |
| 91 | + error: 'API connection error', |
| 92 | + }) |
| 93 | + } |
| 94 | + } else if (xhr.status === 502) { |
| 95 | + resolve({ |
| 96 | + error: 'API error, bad gateway', |
| 97 | + }) |
| 98 | + } else if (xhr.status === 503) { |
| 99 | + resolve({ |
| 100 | + error: 'API error, service unavailable', |
| 101 | + }) |
| 102 | + } else if (xhr.status === 504) { |
| 103 | + resolve({ |
| 104 | + error: 'API error, server timeout', |
| 105 | + }) |
| 106 | + } else { |
| 107 | + try { |
| 108 | + responseJson = JSON.parse(xhr.responseText) |
| 109 | + resolve(responseJson) |
| 110 | + } catch (_error) { |
| 111 | + error = _error |
| 112 | + resolve({ |
| 113 | + error: `API error (status ${xhr.status}): ${error}`, |
| 114 | + }) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + endpoint_url = `${client.api_address}/${_this.algo_uri}` |
| 120 | + xhr.open('POST', endpoint_url, true) |
| 121 | + xhr.setRequestHeader('Content-Type', 'application/json') |
| 122 | + xhr.setRequestHeader('Accept', 'application/json, text/javascript') |
| 123 | + if (client.api_key) { |
| 124 | + xhr.setRequestHeader('Authorization', `Simple ${client.api_key}`) |
| 125 | + } |
| 126 | + return xhr.send(JSON.stringify(input)) |
| 127 | + } |
| 128 | + })(this) |
| 129 | + ) |
| 130 | + if (cb) { |
| 131 | + promise.then(cb) |
| 132 | + } |
| 133 | + return promise |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + Promise = function(runner) { |
| 138 | + let resolve |
| 139 | + this.isComplete = false |
| 140 | + this.listeners = [] |
| 141 | + this.result = void 0 |
| 142 | + resolve = (function(_this) { |
| 143 | + return function(result) { |
| 144 | + let cb |
| 145 | + let _i |
| 146 | + let _len |
| 147 | + let _ref |
| 148 | + _this.result = result |
| 149 | + _this.isComplete = true |
| 150 | + _ref = _this.listeners |
| 151 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 152 | + cb = _ref[_i] |
| 153 | + try { |
| 154 | + cb(result) |
| 155 | + } catch (_error) {} |
| 156 | + } |
| 157 | + } |
| 158 | + })(this) |
| 159 | + this.then = (function(_this) { |
| 160 | + return function(cb) { |
| 161 | + if (_this.isComplete) { |
| 162 | + try { |
| 163 | + return cb(_this.result) |
| 164 | + } catch (_error) {} |
| 165 | + } else { |
| 166 | + return _this.listeners.push(cb) |
| 167 | + } |
| 168 | + } |
| 169 | + })(this) |
| 170 | + runner(resolve) |
| 171 | + } |
| 172 | + |
| 173 | + Algorithmia.tasksInProgress = 0 |
| 174 | + |
| 175 | + Algorithmia.startTask = function() { |
| 176 | + let spinner |
| 177 | + let _i |
| 178 | + let _len |
| 179 | + let _ref |
| 180 | + if (Algorithmia.tasksInProgress === 0) { |
| 181 | + _ref = document.getElementsByClassName('algo-spinner') |
| 182 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 183 | + spinner = _ref[_i] |
| 184 | + spinner.style.visibility = 'visible' |
| 185 | + } |
| 186 | + } |
| 187 | + Algorithmia.tasksInProgress++ |
| 188 | + } |
| 189 | + |
| 190 | + Algorithmia.finishTask = function() { |
| 191 | + let spinner |
| 192 | + let _i |
| 193 | + let _len |
| 194 | + let _ref |
| 195 | + Algorithmia.tasksInProgress-- |
| 196 | + if (Algorithmia.tasksInProgress === 0) { |
| 197 | + _ref = document.getElementsByClassName('algo-spinner') |
| 198 | + for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 199 | + spinner = _ref[_i] |
| 200 | + spinner.style.visibility = 'hidden' |
| 201 | + } |
| 202 | + } else if (Algorithmia.tasksInProgress < 0) { |
| 203 | + console.error('Algorithmia task error (unknown task finished)') |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (Algorithmia.onload && typeof Algorithmia.onload === 'function') { |
| 208 | + Algorithmia.onload() |
| 209 | + } |
| 210 | +}.call(this)) |
| 211 | + |
0 commit comments