UNPKG

gads

Version:

An unofficial JS client library for the SOAP-based DFP Ads API

75 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../common/util"); const constants_1 = require("./constants"); // Handler which sets the headers for a DFP SOAP call class DfpHeaderHandler { // Initializes a DfpHeaderHandler constructor( // The DfpClient whose data will be used to fill in the headers. // We retain a reference to this object so that the header handler picks // up changes to the client dfpClient) { this.dfpClient = dfpClient; this.httpHeaders = {}; this.soapHeaders = []; } // Allows configuration of HTTP headers // tslint:disable-next-line:no-any addHttpHeader(key, value) { this.httpHeaders[key] = value; } // Allows configuration of SOAP headers addSoapHeader(header) { return this.soapHeaders.push(header) - 1; } generateHttpHeaders( // tslint:disable-next-line:no-any cb) { // Get OAuth2 HTTP header new Promise((resolve, reject) => { this.dfpClient.oauth2Client.createHttpHeaders((err, oauth2Header) => err ? reject(err) : resolve(oauth2Header)); }) // Add other HTTP headers .then(oauth2Header => { const headers = { Connection: 'Keep-Alive' }; if (this.dfpClient.enableCompression) { headers['accept-encoding'] = 'gzip'; } return Object.assign(headers, oauth2Header, this.httpHeaders); }) // Callback .then(headers => cb(null, headers), (err) => cb(err)); } generateSoapHeaders(cb) { cb(null, [{ value: { RequestHeader: { applicationName: this.dfpClient.applicationName + DfpHeaderHandler.LIB_SIG, networkCode: this.dfpClient.networkCode } }, xmlns: `${constants_1.DEFAULT_XMLNS_ENDPOINT}/apis/ads/publisher/${constants_1.VERSION}` }].concat(this.soapHeaders)); } // tslint:disable-next-line:no-any getHttpHeader(key) { return this.httpHeaders[key]; } getSoapHeader(index) { return this.soapHeaders[index]; } // tslint:disable-next-line:no-any removeHttpHeader(key) { const val = this.httpHeaders[key]; delete this.httpHeaders[key]; return val; } clearSoapHeaders() { this.soapHeaders = []; } } // The library signature for DFP, to be appended to all application_names. DfpHeaderHandler.LIB_SIG = util_1.generateLibSig(constants_1.LIB_SIG); exports.DfpHeaderHandler = DfpHeaderHandler; //# sourceMappingURL=dfpHeaderHandler.js.map