Created
February 15, 2018 21:07
-
-
Save dmfenton/24bf5c320cba231672b81df21bfdccbf to your computer and use it in GitHub Desktop.
This file contains 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
const fetch = require('node-fetch') | |
const generateToken = require('./lib/token').generate | |
module.exports = async function (args) { | |
const dcatUrl = `${args.site}/data.json` | |
const catalog = await fetch(dcatUrl).then(r => { return r.json() }) | |
const formatted = catalog.dataset.map(d => { | |
return { | |
title: d.title, | |
url: d.webService, | |
description: d.description, | |
tags: d.keyword.join(', '), | |
type: 'Feature Service' | |
} | |
}) | |
const token = await generateToken({ username: args.username, password: args.password, host: args.portal }) | |
for (const dataset of formatted) { | |
await fetch(`${args.portal}/sharing/rest/content/users/${args.username}/${args.folder || 'null'}/addItem?token=${token}&f=json`, { | |
body: encodeForm(dataset), | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json, application/xml, text/plain, text/html, *.*', | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}) | |
.then(r => { | |
return r.json() | |
}) | |
} | |
} | |
function encodeForm (form = {}) { | |
if (typeof form === 'string') { return form } | |
return Object.keys(form).reduce((acc, key) => { | |
acc.push([key, form[key]].map(encodeURIComponent).join('=')) | |
return acc | |
}, []).join('&') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment