Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AFAS RestConnector Tableau Web Data Connector
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://connectors.tableau.com/libs/tableauwdc-2.3.latest.js" type="text/javascript"></script>
<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js?flags=gated%7Calways"></script>
<script src="https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.min.js"></script>
<title>Tableau AFAS Rest Services WDC</title>
</head>
<body>
<div class="container">
<h1>Tableau AFAS Rest Services WDC</h1>
<div class="form-group">
<label for="environment">AOL Environment ID (eg. 12345)</label>
<input type="text" class="form-control" id="environment" placeholder="AFAS Online license no. eg. 12345" value="">
</div>
<div class="form-group">
<label for="token">AppConnector Token</label>
<input type="text" class="form-control" id="token" placeholder="Token, eg. &lt;token&gt;&lt;version&gt;1&lt;/version&gt;&lt;data&gt;XXXXXXX&lt;/data&gt;&lt;/token&gt;
" value="">
</div>
<div class="form-group">
<label for="connector">GetConnector ID</label>
<input type="text" class="form-control" id="connector" placeholder="Connector ID, eg. ProfitCountries" value="">
</div>
<div id="error" class="alert alert-danger text-center"></div>
<button id="goBtn" class="btn-primary btn">Go</button>
<footer>
<hr />
<p class="text-muted">
<small>By Wietse Wind - https://wietse.com</small>
</p>
</footer>
</div>
<script type="text/javascript">
/* WDC Script */
function handleError (message) {
var r = document.getElementById('error')
r.style.display = 'block'
r.innerHTML = message
}
(function () {
document.getElementById('error').style.display = 'none'
var afasRestConnetor = tableau.makeConnector()
afasRestConnetor.getSchema = function (schemaCallback) {
var url = 'https://' + document.getElementById('environment').value.trim() + '.rest.afas.online/ProfitRestServices/metainfo/get/' + document.getElementById('connector').value.trim()
window.fetch(url, {
headers: {
Authorization: 'AfasToken ' + btoa(document.getElementById('token').value.trim())
}
}).then(function (r) {
tableau.log('HTTP code: ' + r.status + ' (' + r.statusText + ')')
return r.json()
}).then(function (r) {
schemaCallback([
{
id: document.getElementById('connector').value.trim().toLowerCase(),
alias: r.description,
columns: r.fields.map(function (f) {
// [f.dataType]
var d = {
id: f.id,
alias: f.label,
dataType: tableau.dataTypeEnum.string
}
// tableau.log(d)
return d
})
}
])
}).catch(function (e) {
tableau.log('Could not fetch data from ' + url)
})
}
afasRestConnetor.getData = function (table, doneCallback) {
var url = 'https://' + document.getElementById('environment').value.trim() + '.rest.afas.online/ProfitRestServices/connectors/' + document.getElementById('connector').value.trim() + '?skip=0&take=10000000'
window.fetch(url, {
headers: {
Authorization: 'AfasToken ' + btoa(document.getElementById('token').value.trim())
}
}).then(function (r) {
tableau.log('HTTP code: ' + r.status + ' (' + r.statusText + ')')
return r.json()
}).then(function (r) {
var tableData = []
r.rows.forEach(function (d) {
tableData.push(d)
})
table.appendRows(tableData)
doneCallback()
}).catch(function (e) {
tableau.log('Could not fetch data from ' + url)
})
}
tableau.registerConnector(afasRestConnetor)
})()
document.getElementById('goBtn').onclick = function () {
var errorStatus = ''
var url = 'https://' + document.getElementById('environment').value.trim() + '.rest.afas.online/ProfitRestServices/metainfo'
window.fetch(url, {
headers: {
Authorization: 'AfasToken ' + btoa(document.getElementById('token').value.trim())
}
}).then(function (r) {
errorStatus = 'HTTP code: ' + r.status + ' (' + r.statusText + ')'
return r.json()
}).then(function (r) {
// console.log(r.info.envid)
// console.log(r.info.appName)
if (typeof r.getConnectors !== 'undefined') {
var getConnectors = r.getConnectors.map(function (c) {
return c.id.toLowerCase()
})
if (getConnectors.indexOf(document.getElementById('connector').value.trim().toLowerCase()) < 0) {
handleError('GetConnector <code class="text-primary">' + document.getElementById('connector').value.trim() + '</code> not in the list with available GetConnectors. Possibly permissions / no GetConnectors assigned to AppConnector')
} else {
// Get Connector info
// https://43854.rest.afas.online/ProfitRestServices/metainfo/get/Profit_Employees
try {
tableau.connectionName = 'AFAS GetConnector ' + document.getElementById('connector').value.trim()
tableau.submit()
} catch (e) {
handleError(e.message || e)
}
}
} else {
handleError('Could not fetch GetConnector list. Possibly permissions / no GetConnectors assigned to AppConnector')
}
}).catch(function (e) {
handleError('Could not fetch data from <code class="text-primary">' + url + '</code> (' + errorStatus + ')')
})
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.