dfp-lib
Version:
This project hosts the Node.JS client library for the SOAP-based DFP API at Google.
66 lines (65 loc) • 2.35 kB
JavaScript
"use strict";
const url = require("url");
class SoapClientFactory {
constructor(user, server, productName, headerOverrides) {
this.user = user;
this.server = server;
this.productName = productName;
this.headerOverrides = headerOverrides;
}
getSoapClientConfig(options, endpoint) {
options['encoding'] = 'utf-8';
options['timeout'] = 0;
const contextOptions = {
ssl: {}
};
const user = this.getAdsUser();
options['cache_wsdl'] = user.getWsdlCacheType();
this.setIfDefined(options, 'proxy_host', user.getDefined('HTTP_PROXY_HOST'));
this.setIfDefined(options, 'proxy_port', user.getDefined('HTTP_PROXY_PORT'));
this.setIfDefined(options, 'proxy_login', user.getDefined('HTTP_PROXY_USER'));
this.setIfDefined(options, 'proxy_password', user.getDefined('HTTP_PROXY_PASSWORD'));
this.setIfDefined(contextOptions['ssl'], 'verify_peer', user.getDefined('SSL_VERIFY_PEER'));
if (user.getDefined('SSL_VERIFY_HOST')) {
contextOptions['ssl']['CN_match'] = url.parse(endpoint).hostname;
}
this.setIfDefined(contextOptions['ssl'], 'capath', user.getDefined('SSL_CA_PATH'));
this.setIfDefined(contextOptions['ssl'], 'cafile', user.getDefined('SSL_CA_FILE'));
options['stream_context'] = contextOptions;
const headers = {};
user.getHeaderNames().forEach(function (key) {
headers[key] = user.getHeaderValue(key);
});
if (this.headerOverrides) {
Object.assign(headers, this.headerOverrides);
}
return {
options: options,
headers: headers
};
}
setIfDefined(obj, key, val) {
if (val) {
obj[key] = val;
}
}
getServiceLocation(endpoint) {
const urlObj = url.parse(endpoint);
const serverObj = url.parse(this.getServer());
urlObj.protocol = serverObj.protocol;
urlObj.host = serverObj.host;
urlObj.hostname = serverObj.hostname;
return url.format(urlObj);
}
getAdsUser() {
return this.user;
}
getServer() {
return this.server;
}
getProductName() {
return this.productName;
}
}
exports.SoapClientFactory = SoapClientFactory;
;