Guest User

Untitled

a guest
Feb 27th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. const fetch = require('node-fetch')
  2. const generateToken = require('./lib/token').generate
  3.  
  4. module.exports = async function (args) {
  5. const dcatUrl = `${args.site}/data.json`
  6. const catalog = await fetch(dcatUrl).then(r => { return r.json() })
  7.  
  8. const formatted = catalog.dataset.map(d => {
  9. return {
  10. title: d.title,
  11. url: d.webService,
  12. description: d.description,
  13. tags: d.keyword.join(', '),
  14. type: 'Feature Service'
  15. }
  16. })
  17.  
  18. const token = await generateToken({ username: args.username, password: args.password, host: args.portal })
  19.  
  20. for (const dataset of formatted) {
  21. await fetch(`${args.portal}/sharing/rest/content/users/${args.username}/${args.folder || 'null'}/addItem?token=${token}&f=json`, {
  22. body: encodeForm(dataset),
  23. method: 'POST',
  24. headers: {
  25. 'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
  26. 'Content-Type': 'application/x-www-form-urlencoded'
  27. }
  28. })
  29. .then(r => {
  30. return r.json()
  31. })
  32. }
  33. }
  34.  
  35. function encodeForm (form = {}) {
  36. if (typeof form === 'string') { return form }
  37. return Object.keys(form).reduce((acc, key) => {
  38. acc.push([key, form[key]].map(encodeURIComponent).join('='))
  39. return acc
  40. }, []).join('&')
  41. }
Add Comment
Please, Sign In to add comment