Code Snippet for GCD - JIRA Connector
/**
* Mandatory function required by Google Data Studio that should
* return the authentication method required by the connector
* to authorize the third-party service.
* @return {Object} AuthType
*/
function getAuthType() {
var cc = DataStudioApp.createCommunityConnector();
return cc.newAuthTypeResponse()
.setAuthType(cc.AuthType.KEY)
.setHelpUrl('https://www.example.org/connector-auth-help')
.build();
}
/**
* Mandatory function required by Google Data Studio that should
* clear user credentials for the third-party service.
* This function does not accept any arguments and
* the response is empty.
*/
function resetAuth() {
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('dscc.key');
}
/**
* Mandatory function required by Google Data Studio that should
* determine if the authentication for the third-party service is valid.
* @return {Boolean}
*/
function isAuthValid() {
var userProperties = PropertiesService.getUserProperties();
var key = userProperties.getProperty('dscc.key');
return checkForValidKey(key);
}
/**
* Mandatory function required by Google Data Studio that should
* set the credentials after the user enters either their
* credential information on the community connector configuration page.
* @param {Object} request The set credentials request.
* @return {object} An object with an errorCode.
*/
function setCredentials(request) {
var key = request.key;
var validKey = checkForValidKey(key);
if (!validKey) {
return {
errorCode: 'INVALID_CREDENTIALS'
};
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('dscc.key', key);
return {
errorCode: 'NONE'
};
}
/**
* Mandatory function required by Google Data Studio that should
* return the user configurable options for the connector.
* @param {Object} request
* @return {Object} fields
*/
function getConfig(request) {
var cc = DataStudioApp.createCommunityConnector();
var config = cc.getConfig();
config.setDateRangeRequired(true);
return config.build();
}
/**
* Supports the getSchema() function
* @param {Object} request
* @return {Object} fields
*/
function getFields(request) {
var cc = DataStudioApp.createCommunityConnector();
var fields = cc.getFields();
var types = cc.FieldType;
var aggregations = cc.AggregationType;
fields.newDimension()
.setId('boardName')
.setType(types.TEXT);
return fields;
}
/**
* Mandatory function required by Google Data Studio that should
* return the schema for the given request.
* This provides the information about how the connector's data is organized.
* @param {Object} request
* @return {Object} fields
*/
function getSchema(request) {
var fields = getFields(request).build();
return { schema: fields };
}
/**
* Takes the requested fields with the API response and
* return rows formatted for Google Data Studio.
* @param {Object} requestedFields
* @param {Object} response
* @return {Array} values
*/
function responseToRows(requestedFields, response) {
return response.map(function(board) {
var row = [];
requestedFields.asArray().forEach(function (field) {
switch (field.getId()) {
case 'boardName':
return row.push(board.name);
default:
return row.push(field.getId());
}
});
return { values: row };
});
}
/**
* Mandatory function required by Google Data Studio that should
* return the tabular data for the given request.
* @param {Object} request
* @return {Object}
*/
function getData(request) {
var requestedFieldIds = request.fields.map(function(field) {
return field.name;
});
var requestedFields = getFields().forIds(requestedFieldIds);
var userProperties = PropertiesService.getUserProperties();
var token = userProperties.getProperty('dscc.key');
var baseURL = 'https://kodevelop.atlassian.net/rest/agile/latest/board';
var options = {
'method' : 'GET',
'headers': {
'Authorization': 'Basic ' + token,
'Content-Type': 'application/json'
},
'muteHttpExceptions':true
};
var response = UrlFetchApp.fetch(baseURL, options);
if (response.getResponseCode() == 200) {
var parsedResponse = JSON.parse(response).values;
var rows = responseToRows(requestedFields, parsedResponse);
return {
schema: requestedFields.build(),
rows: rows
};
} else {
DataStudioApp.createCommunityConnector()
.newUserError()
.setDebugText('Error fetching data from API. Exception details: ' + response)
.setText('Error fetching data from API. Exception details: ' + response)
.throwException();
}
}
/**
* Checks if the Key/Token provided by the user is valid
* @param {String} key
* @return {Boolean}
*/
function checkForValidKey(key) {
//return false;
var token = key;
var baseURL = 'https://example.atlassian.net/rest/agile/latest/board';
var options = {
'method' : 'GET',
'headers': {
'Authorization': 'Basic ' + token,
'Content-Type': 'application/json'
},
'muteHttpExceptions':true
};
var response = UrlFetchApp.fetch(baseURL, options);
if (response.getResponseCode() == 200) {
return true;
} else {
return false;
}
}