UNPKG

gads

Version:

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

79 lines 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../common/util"); const constants_1 = require("./constants"); const dfpHeaderHandler_1 = require("./dfpHeaderHandler"); const googleAdsValueError_1 = require("../common/googleAdsValueError"); const soapClientFactory_1 = require("../soap/soapClientFactory"); // A central location to set headers and create web service clients class DfpClient { // Initialize a DFP Client // For more information on these arguments, see our SOAP headers guide: // https://developers.google.com/doubleclick-publishers/docs/soap_xml constructor( // A GoogleOAuth2Client used to authorize your requests oauth2Client, // A string of your choosing to identify your application to DFP applicationName, // The network code of the network being queried, required for every request // except NetworkService.makeTestNetwork and NetworkService.getAllNetworks. // Be sure that you only use a network code available to your account login. // You can find the code on the DFP website for your account, on the // Admin > Network Settings page next to "Network code". networkCode, // A boolean specifiying whether to use the // 'soap' library's client's built-in WSDL cache cache = true, // A Proxy instance if a proxy is being used. proxy, // A boolean indicating if you want to enable compression // of the SOAP response. If True, the SOAP response will use gzip // compression, and will be decompressed for you automatically. enableCompression = false, // A string identifying the webserver hosting the DFP API server, logger) { this.oauth2Client = oauth2Client; this.applicationName = applicationName; this.networkCode = networkCode; this.cache = cache; this.proxy = proxy; this.enableCompression = enableCompression; this.server = server; this.logger = logger; // Sanitize application name if (!applicationName || applicationName === constants_1.DEFAULT_APPLICATION_NAME) { throw new googleAdsValueError_1.GoogleAdsValueError('Application name must be set and not contain' + ` the default [${constants_1.DEFAULT_APPLICATION_NAME}]`); } // Set headers this.headerHandler = new dfpHeaderHandler_1.DfpHeaderHandler(this); // Enable compression if (enableCompression) { this.applicationName = this.applicationName + ' (gzip)'; } // Sanitize server this.server = this.server || constants_1.DEFAULT_API_ENDPOINT; if (this.server[this.server.length - 1] === '/') { this.server = this.server.substring(0, this.server.length - 1); } // Set soap client factory this.soapClientFactory = new soapClientFactory_1.SoapClientFactory(); } getService(serviceName, cb) { const wsdlUrl = `${this.server}/apis/ads/publisher/${constants_1.VERSION}/${serviceName}?wsdl`; // Get the SOAP client const promise = this.soapClientFactory.getSoapClient(wsdlUrl, this) // Throw if service could not be created .catch((err) => { if (constants_1.SERVICES.hasOwnProperty(serviceName)) { err = new googleAdsValueError_1.GoogleAdsValueError('Unrecognized service for the DFP API.' + ` Service given: ${serviceName}. Supported` + ` services: ${Object.keys(constants_1.SERVICES).join(',')}`); } throw err; }); return util_1.callbackOrPromise(cb, promise); } } exports.DfpClient = DfpClient; //# sourceMappingURL=dfpClient.js.map