Skip to content

Instantly share code, notes, and snippets.

@dmfenton
Created February 15, 2018 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmfenton/24bf5c320cba231672b81df21bfdccbf to your computer and use it in GitHub Desktop.
Save dmfenton/24bf5c320cba231672b81df21bfdccbf to your computer and use it in GitHub Desktop.
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