dfp-lib
Version:
This project hosts the Node.JS client library for the SOAP-based DFP API at Google.
167 lines (166 loc) • 6.52 kB
JavaScript
"use strict";
const logger_1 = require("../util/logger");
const settings_1 = require("../../settings");
const validationException_1 = require("./validationException");
class AdsUser {
constructor() {
this.requestHeaderElements = {};
this.libVersion = settings_1.Settings.LIB_VERSION;
this.libName = settings_1.Settings.LIB_NAME;
this.defined = {};
}
getAuthVarValue(authVar, authVarName, authObject) {
if (authVar) {
return authVar;
}
if (authObject[authVarName]) {
return authObject[authVarName];
}
return null;
}
getHeaderNames() {
return Object.keys(this.requestHeaderElements);
}
getHeaderValue(key) {
if (this.requestHeaderElements.hasOwnProperty(key)) {
return this.requestHeaderElements[key];
}
else {
return undefined;
}
}
setHeaderValue(key, value) {
if (value === undefined) {
delete this.requestHeaderElements[key];
}
else {
this.requestHeaderElements[key] = value;
}
}
initLogs() {
logger_1.Logger.logToFile(logger_1.Logger.SOAP_XML_LOG, this.logsDirectory + "/soap_xml.log");
logger_1.Logger.logToFile(logger_1.Logger.REQUEST_INFO_LOG, this.logsDirectory + "/request_info.log");
logger_1.Logger.setLogLevel(logger_1.Logger.SOAP_XML_LOG, logger_1.Logger.Level.FATAL);
logger_1.Logger.setLogLevel(logger_1.Logger.REQUEST_INFO_LOG, logger_1.Logger.Level.FATAL);
}
logDefaults() {
logger_1.Logger.setLogLevel(logger_1.Logger.SOAP_XML_LOG, logger_1.Logger.Level.ERROR);
logger_1.Logger.setLogLevel(logger_1.Logger.REQUEST_INFO_LOG, logger_1.Logger.Level.INFO);
}
logErrors() {
logger_1.Logger.setLogLevel(logger_1.Logger.SOAP_XML_LOG, logger_1.Logger.Level.ERROR);
logger_1.Logger.setLogLevel(logger_1.Logger.REQUEST_INFO_LOG, logger_1.Logger.Level.ERROR);
}
logAll() {
logger_1.Logger.setLogLevel(logger_1.Logger.SOAP_XML_LOG, logger_1.Logger.Level.INFO);
logger_1.Logger.setLogLevel(logger_1.Logger.REQUEST_INFO_LOG, logger_1.Logger.Level.INFO);
}
loadSettings(settings, defaultVersion, defaultServer, defaultLogsDir) {
const libLogDirPath = settings['LIB_LOG_DIR_PATH'] || defaultLogsDir;
this.logsDirectory = libLogDirPath;
this.initLogs();
this.defaultVersion = settings['VERSION'] || defaultVersion;
this.defaultServer = settings['SERVER'] || defaultServer;
this.wsdlCache = this.getSetting(settings, 'SOAP', 'WSDL_CACHE', 0);
if (this.wsdlCache < 0 || this.wsdlCache > 3) {
this.wsdlCache = 0;
}
let proxyHost = this.getSetting(settings, 'PROXY', 'HOST');
if (proxyHost) {
this.define('HTTP_PROXY_HOST', proxyHost);
}
let proxyPort = this.getSetting(settings, 'PROXY', 'PORT');
if (proxyPort) {
this.define('HTTP_PROXY_PORT', proxyPort);
}
let proxyUser = this.getSetting(settings, 'PROXY', 'USER');
if (proxyUser) {
this.define('HTTP_PROXY_USER', proxyUser);
}
let proxyPassword = this.getSetting(settings, 'PROXY', 'PASSWORD');
if (proxyPassword) {
this.define('HTTP_PROXY_PASSWORD', proxyPassword);
}
this.authServer = this.getSetting(settings, 'AUTH', 'AUTH_SERVER', 'https://accounts.google.com');
this.oauth2Handler = this.getDefaultOAuth2Handler(this.getSetting(settings, 'AUTH', 'OAUTH2_HANDLER_CLASS'));
this.requestHandler = this.getDefaultRequestHandler(this.getSetting(settings, 'SOAP', 'REQUEST_HANDLER_CLASS'));
}
getSetting(settings, section, name, def = null) {
return (!settings || !settings[section] || !settings[section][name]) ? def : settings[section][name];
}
define(name, value) {
if (this.defined.hasOwnProperty(name) && this.defined[name] != value) {
throw new Error("Cannot redefine defined constant '" + name + "'");
}
this.defined[name] = value;
}
getDefined(name) {
return this.defined[name];
}
getDefaultServer() {
return this.defaultServer;
}
setDefaultServer(defaultServer) {
this.defaultServer = defaultServer;
}
getDefaultVersion() {
return this.defaultVersion;
}
setDefaultVersion(defaultVersion) {
this.defaultVersion = defaultVersion;
}
getLogsDirectory() {
return this.logsDirectory;
}
getWsdlCacheType() {
return this.wsdlCache;
}
getAuthServer() {
return this.authServer;
}
getOAuth2Info() {
return this.oauth2Info;
}
setOAuth2Info(oauth2Info) {
this.oauth2Info = oauth2Info;
}
getOAuth2Handler() {
return this.oauth2Handler;
}
setOAuth2Handler(oauth2Handler) {
this.oauth2Handler = oauth2Handler;
}
getRequestHandler() {
return this.requestHandler;
}
setRequestHandler(requestHandler) {
this.requestHandler = requestHandler;
}
getClientLibraryUserAgent() {
return this.getHeaderValue(this.getUserAgentHeaderName());
}
getAllClientLibraryUserAgentParts() {
let allUserAgentParts = [this.getClientLibraryNameAndVersion().join('/')];
return allUserAgentParts;
}
setClientLibraryUserAgent(applicationName) {
const userAgentParts = this.getAllClientLibraryUserAgentParts().join(',');
this.setHeaderValue(this.getUserAgentHeaderName(), `${applicationName} (${userAgentParts})`);
}
validateOAuth2Info() {
if (!this.oauth2Info) {
throw new validationException_1.ValidationException('OAuth2Info', null, 'OAuth 2.0 configuration is required.');
}
if (!this.oauth2Info['client_id']) {
throw new validationException_1.ValidationException('client_id', null, 'client_id is required.');
}
if (!this.oauth2Info['client_secret']) {
throw new validationException_1.ValidationException('client_secret', null, 'client_secret is required.');
}
if (!this.oauth2Info['access_token'] && !this.oauth2Info['refresh_token']) {
throw new validationException_1.ValidationException('refresh_token', null, 'Either the refresh_token or the access_token is required.');
}
}
}
exports.AdsUser = AdsUser;
;