Created
February 17, 2012 17:31
-
-
Save gasi/1854498 to your computer and use it in GitHub Desktop.
Purge Akamai CDN using Node.js
This file contains hidden or 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
var request = require('request'); | |
var settings = { | |
AKAMAI_USER: 'test', | |
AKAMAI_PASSWORD: '...', | |
AKAMAI_NOTIFICATION_EMAIL: '' | |
}; | |
var akamai = exports; | |
var SOAP_URL = 'https://ccuapi.akamai.com:443/soap/servlet/soap/purge'; | |
akamai.purge = function (urls, callback) { | |
var body = '<?xml version="1.0" encoding="UTF-8"?>' + | |
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + | |
'xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" ' + | |
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' + | |
'soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ' + | |
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + | |
'<soap:Body>' + | |
'<purgeRequest xmlns="http://ccuapi.akamai.com/purge">' + | |
'<name xsi:type="xsd:string">' + settings.AKAMAI_USER + '</name>' + | |
'<pwd xsi:type="xsd:string">' + settings.AKAMAI_PASSWORD + '</pwd>' + | |
'<network xsi:type="xsd:string"></network>' + | |
'<opt soapenc:arrayType="xsd:string[3]" xsi:type="soapenc:Array">' + | |
'<item xsi:type="xsd:string">type=arl</item>' + | |
'<item xsi:type="xsd:string">action=remove</item>' + | |
'<item xsi:type="xsd:string">email-notification=' + settings.AKAMAI_NOTIFICATION_EMAIL + '</item>' + | |
'</opt>' + | |
'<uri soapenc:arrayType="xsd:string[' + urls.length + ']" xsi:type="soapenc:Array">'; | |
urls.forEach(function (url) { | |
body += '<item xsi:type="xsd:string">' + url + '</item>'; | |
}); | |
body += '</uri>' + | |
'</purgeRequest>' + | |
'</soap:Body>' + | |
'</soap:Envelope>'; | |
request({ | |
method: 'POST', | |
url: SOAP_URL, | |
headers: { | |
'SOAPAction': 'http://ccuapi.akamai.com/purge#purgeRequest', | |
'Content-Type': 'text/xml; charset="UTF-8"' | |
}, | |
body: body | |
}, function (error, response, body) { | |
if (error) { | |
callback(error); | |
return; | |
} | |
if (response.statusCode === 200) { | |
callback(null); | |
} else { | |
callback(body); | |
} | |
}); | |
}; | |
// Sample call: | |
// | |
// akamai.purge(['http://gasi.ch/kitten.jpg', 'http://gasi.ch/script.js'], function (error) { | |
// console.log(error); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment