Last active
December 19, 2019 16:42
-
-
Save mintsoft/f1a9cc6a60e81cba81c2e454a16aa599 to your computer and use it in GitHub Desktop.
GetDellWarranty V5 From TechDirect for GScript/Google Sheets
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
/* | |
* Returns Dell Warranty for service tag using APIKey | |
* | |
* @param {apiKey} API Key from techdirect | |
* @param {serviceTag} the ServiceTag desired | |
* @param {headers} Include headers in output | |
* @return The description and warranty status | |
*/ | |
function getDellWarranty(clientId, clientSecret, serviceTag, headers) { | |
var cache = CacheService.getDocumentCache(); | |
var bearerToken = cache.get("dell_access_token"); | |
if(!bearerToken) { | |
//auth and get our token thing | |
var options = { | |
'method' : 'post', | |
'payload' : { | |
'client_id': clientId, | |
'client_secret': clientSecret, | |
'grant_type': 'client_credentials' | |
} | |
}; | |
var authenticationResponseHttp = UrlFetchApp.fetch("https://apigtwb2c.us.dell.com/auth/oauth/v2/token", options); | |
var authenticationResponse = JSON.parse(authenticationResponseHttp); | |
//store the token thing in the cache for the duration that it is valid - a bit | |
cache.put("dell_access_token", authenticationResponse.access_token, authenticationResponse.expires_in / 2); | |
bearerToken = authenticationResponse.access_token; | |
} | |
var getOptions = { | |
'method': 'get', | |
'headers' : { | |
'Authorization': 'Bearer ' + bearerToken, | |
} | |
}; | |
var jsondata = UrlFetchApp.fetch("https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags=" + serviceTag, getOptions); | |
var apiResponse = JSON.parse(jsondata); | |
var shipDate = apiResponse[0].shipDate; | |
var description = apiResponse[0].systemDescription; | |
var warranties = apiResponse[0].entitlements; | |
var earliestWarranty = {startDate:'3000-01-01T00:00:00', endDate:'9999-99-99T99:99:99', serviceLevelDescription:'ServiceLevelDescription', serviceLevelCode: "ND"}; | |
var latestExpiringWarranty = {startDate:'1970-01-01T00:00:00', endDate:'1970-01-01T00:00:00', serviceLevelDescription:'ServiceLevelDescription', serviceLevelCode: "ND"}; | |
for(var index=0; index < warranties.length; ++index) { | |
if(warranties[index].startDate <= earliestWarranty.startDate) { | |
if(warranties[index].serviceLevelGroup == 5 || warranties[index].serviceLevelGroup == 8) | |
earliestWarranty = warranties[index]; | |
} | |
if(warranties[index].endDate >= latestExpiringWarranty.endDate) { | |
if(warranties[index].serviceLevelGroup == 5 || warranties[index].serviceLevelGroup == 8) | |
latestExpiringWarranty = warranties[index]; | |
} | |
} | |
var returnValue = []; | |
if(headers) { | |
returnValue.push(["ShipDate", "Description", "WarrantyStartDate","InitialServiceLevel", "WarrantyEndDate", "FinalServiceLevel"]); | |
} | |
returnValue.push([ | |
shipDate, | |
description, | |
earliestWarranty.startDate, | |
earliestWarranty.serviceLevelDescription, | |
latestExpiringWarranty.endDate, | |
latestExpiringWarranty.serviceLevelDescription | |
]); | |
return returnValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment