Skip to content

Instantly share code, notes, and snippets.

@coffee-mug
Created September 17, 2021 15:42
Show Gist options
  • Save coffee-mug/5330673822c28bfa953af206efed2279 to your computer and use it in GitHub Desktop.
Save coffee-mug/5330673822c28bfa953af206efed2279 to your computer and use it in GitHub Desktop.
// config holds the required headers (authorization and content-type) to be passed to each request.
// To get a working token, just:
// 1- Go to oneTrust websites page and open your network
// 2- Click "Add Website"
// 3- Locate the request to the "token" endpoint in your network
// 4- In the Preview tab, locate the "access_token" property and copy/paste its value next to "Bearer" in the "authorization"
// header from the config object. Alternatively, if you have already instantiated an OTDomainAPI manager, use the method updateToken.
const config = {
headers: {
"accept": "application/json, text/plain, */*",
"authorization": "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJwY3MiOiJhNmI1Mjc1MGVmN2FiN2FjOGQwOGVjNTc1NDEzOTg2MTNlOGM1NDhlMTc2NzZlOTYyMWNjMGU3OTQ4OThiYzg0IiwidmlydHVhbFJvbGUiOmZhbHNlLCJyb2xlIjoiU2l0ZSBBZG1pbiIsInVzZXJfbmFtZSI6InJvaGl0LmxvYm9AZmlmdHktZml2ZS5jb20iLCJsYW5ndWFnZUlkIjoxLCJmZ3B0IjoiJDJhJDEwJDRpOGgxT3BnNkpYdGhQT1V0WmpZb2VucWY5ZDJwR1lkdG91U1FYYVQwcHFxZlBLL0NlWHNPIiwic2Vzc2lvbklkIjoiMmM0MjZiY2ItMTVjNi00YjIxLWJhNjMtOTY1YmI2MDY0Y2Q4IiwidGVuYW50R3VpZCI6IjEwNThlMGI5LWVlOTUtNGQ0My04MjkyLTNkYWU0MGNlNWMzYyIsImF1dGhvcml0aWVzIjpbIlNpdGUgQWRtaW4iXSwiY2xpZW50X2lkIjoib25ldHJ1c3QiLCJvcmdHcm91cElkIjoiMSIsIm9yZ0dyb3VwR3VpZCI6IjliNzQ5M2U2LWY2MzgtNDY0Ny05NTliLThjNTljZGY2NjhkYSIsInBlcm1pc3Npb25zIjoiSDRzSUFBQ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) {
let conf = config;
return {
// updateToken replaces existing token in headers with newToken
updateToken(newToken) {
conf.headers.authorization = `Bearer ${newToken}`;
},
// newDomainInfos return the JSON body expected by OT for creating a new domain.
// For now only the domain name is taken into account, feel free to update the paramaeters list if needed.
newDomainInfos(domain) {
if (!domain) throw new Error("domain can't be empty")
return {
"domain": domain,
"keepExistingData": true,
"numberOfPages": 1000,
"independentPaths": false,
"includedQueryParams": "",
"secondaryUris": "",
"isRescan": false,
"siteMapsUris": "",
"slowScan": false
}
},
// addDomain performs the API call, using the headers defined in config and the domainInfos passed.
// It logs the API call response fro you to check that everything ran fine (expected HTTP status code: 201)
addDomain(domainInfos) {
fetch("https://app-fr.onetrust.com/api/cookiemanager/v1/websitescans?organization=9b7493e6-f638-4647-959b-8c59cdf668da", {
"headers": conf.headers,
"body": JSON.stringify(domainInfos),
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(response => console.log(`Response: ${response}`))
},
// batchUpdate performs domainsList.length API call to create domains.
async batchUpdate(domainsList) {
domainsList.forEach(async function(domainInfos){
await this.addDomain(domainInfos);
}.bind(this))
}
}
}
const domainsManager = OTDomainAPI(config);
// List of domains to create
const domains = [
domainsManager.newDomainInfos("https://fetch2.fr"),
domainsManager.newDomainInfos("https://fetch3.fr"),
domainsManager.newDomainInfos("https://fetch4.fr"),
domainsManager.newDomainInfos("https://fetch5.fr"),
]
domainsManager.batchUpdate(domains);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment