Last active
October 24, 2019 13:04
-
-
Save ao-picterra/c77a304a61f26a2d3f94317b1e655d46 to your computer and use it in GitHub Desktop.
medium-api-article-1-gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function upload(filePath, fileName) { | |
| const sleep = s => new Promise(resolve=> setTimeout(resolve, s * 1000)) | |
| const apiKey = "123456789" | |
| const apiServer = "https://app.picterra.ch/public/api/v1" | |
| let options = { | |
| method: 'POST', | |
| headers: new Headers({ | |
| 'Content-Type': 'application/json', | |
| 'X-Api-Key': apiKey}), | |
| body: JSON.stringify({'name': fileName})} | |
| let response = await fetch(`${apiServer}/rasters/upload/file/`, options) | |
| const data = await response.json() | |
| const uploadUrl = data.upload_url | |
| const rasterId = data.raster_id | |
| const file = await fs.createReadStream(filePath) | |
| const size = fs.statSync(filePath)["size"] | |
| options = { | |
| method: 'PUT', | |
| headers: new Headers({ | |
| 'Content-Type': 'multipart/form-data', | |
| 'Content-Length': size}), | |
| body: file} | |
| response = await fetch(uploadUrl, options) | |
| return rasterId | |
| } | |
| async function commit(rasterId) { | |
| const sleep = s => new Promise(resolve=> setTimeout(resolve, s * 1000)) | |
| const apiKey = "123456789" | |
| let options = { | |
| method: 'POST', | |
| headers: new Headers({ 'X-Api-Key': apiKey })} | |
| let response = await fetch(`${apiServer}/rasters/${rasterId}/commit/`, options) | |
| let data = await response.json() | |
| const pollInterval = data.poll_interval | |
| options = { headers: new Headers({ 'X-Api-Key': apiKey})} | |
| isReady = false | |
| const timeout = Date.now() + (5 * 60 * 1000) // 5 minutes timeout | |
| do { | |
| await sleep(pollInterval * 1000 ) | |
| response = await fetch(`${apiServer}/rasters/${rasterId}/`, options) | |
| data = await response.json() | |
| isReady = (data.status == "ready")} | |
| while (!isReady && (Date.now() < timeout)) | |
| return isReady | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment