@guardian/google-admanager-api
Version:
Google Ad Manager API Client Library for NodeJs
67 lines • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleSoapService = void 0;
const soap_1 = require("soap");
const constants_1 = require("../common/constants");
const utils_1 = require("../common/utils");
class GoogleSoapService {
constructor(service, options) {
this.service = service;
this.applicationName = options.applicationName;
this.networkCode = options.networkCode;
this.token = options.token;
this.apiVersion = options.apiVersion;
}
async createClient(logRequests = false, logResponses = false) {
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${this.apiVersion}/${this.service}?wsdl`;
const client = await (0, soap_1.createClientAsync)(serviceUrl);
client.addSoapHeader(this.getSoapHeaders());
client.setToken = function setToken(token) {
client.setSecurity(new soap_1.BearerSecurity(token));
};
if (this.token)
client.setToken(this.token);
if (logRequests) {
client.addListener("request", (req) => {
console.info("SOAP Request:", req);
});
}
if (logResponses) {
client.addListener("response", (res) => {
console.info("SOAP Response:", res);
});
}
this._client = new Proxy(client, {
get: function get(target, propertyKey) {
const method = propertyKey.toString();
if (target.hasOwnProperty(method) && !["setToken"].includes(method)) {
return async function run(dto = {}) {
const res = await (0, utils_1.promiseFromCallback)((cb) => client[method](dto, cb));
return (res === null || res === void 0 ? void 0 : res.rval) || null;
};
}
return target[method];
},
});
const services = constants_1.SERVICE_MAP;
return new services[this.service](this._client);
}
getSoapHeaders() {
return {
RequestHeader: {
attributes: {
"soapenv:actor": "http://schemas.xmlsoap.org/soap/actor/next",
"soapenv:mustUnderstand": 0,
"xsi:type": "ns1:SoapRequestHeader",
"xmlns:ns1": "https://www.google.com/apis/ads/publisher/" + this.apiVersion,
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
},
"ns1:networkCode": this.networkCode,
"ns1:applicationName": this.applicationName,
},
};
}
}
exports.GoogleSoapService = GoogleSoapService;
//# sourceMappingURL=googleSoap.service.js.map