UNPKG

nodejs-google-adwords

Version:
86 lines (85 loc) 3.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = __importDefault(require("lodash")); var pretty_data_1 = require("pretty-data"); var core_1 = require("../core"); var registry_1 = require("./registry"); var ReportService_1 = require("./ReportService"); var AdWordsService = /** @class */ (function () { function AdWordsService(options) { this.registryService = registry_1.registryService; this.verbose = true; this.soapHeader = { clientCustomerId: options.clientCustomerId, developerToken: options.developerToken, userAgent: options.userAgent, validateOnly: options.validateOnly || false, partialFailure: options.partialFailure || false, }; this.authService = core_1.AuthService.getInstance({ clientId: options.clientId, clientSecret: options.clientSecret, }); this.authService.setCredentials(options.credentials); } AdWordsService.prototype.setVerbose = function (verbose) { this.verbose = verbose; }; /** * get each google adwords service: CampaignService, AdGroupService, BudgetService etc... * * @author dulin * @template K * @param {K} key * @param {Partial<IServiceOpts & IServiceDeps>} [options] * @returns {IServiceMap[K]} * @memberof AdWordsService */ AdWordsService.prototype.getService = function (key, options) { var ServiceClass = this.registryService.get(key); if (!ServiceClass) { throw new Error("Service: " + key + " has not been registered yet."); } var serviceName = key; var ver = lodash_1.default.get(options, ['version'], AdWordsService.version); var namespace = lodash_1.default.get(options, ['namespace'], lodash_1.default.get(ServiceClass, ['namespace'], AdWordsService.namespace)); var xmlns = namespace + "/" + ver; var url = xmlns + "/" + serviceName + AdWordsService.suffix; var verbose = lodash_1.default.get(options, ['verbose'], this.verbose); this.soapHeader.clientCustomerId = options ? options.clientCustomerId : this.soapHeader.clientCustomerId; this.soapHeader.partialFailure = !!(options ? options.partialFailure : this.soapHeader.partialFailure); var soapServiceOptions = { authService: this.authService, header: this.soapHeader, verbose: verbose, url: url, serviceName: serviceName, xmlns: xmlns, }; if (options) { soapServiceOptions = lodash_1.default.merge(soapServiceOptions, options); } if (verbose) { console.log('soapServiceOptions: ', pretty_data_1.pd.json(soapServiceOptions)); } var soapService = new core_1.SoapService(soapServiceOptions); var httpServiceOpts = { headers: { clientCustomerId: this.soapHeader.clientCustomerId, developerToken: this.soapHeader.developerToken, }, authService: this.authService, }; var httpService = new core_1.HttpService(httpServiceOpts); var reportService = new ReportService_1.ReportService({ httpService: httpService }); return new ServiceClass(Object.assign({}, { soapService: soapService, httpService: httpService, reportService: reportService }, options)); }; AdWordsService.namespace = 'https://adwords.google.com/api/adwords/cm'; AdWordsService.version = 'v201809'; AdWordsService.suffix = '?wsdl'; return AdWordsService; }()); exports.AdWordsService = AdWordsService;