Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing token into the constructor to reuse the same token #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 58 additions & 54 deletions src/dfp.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
import { BearerSecurity, Client, createClient } from 'soap';
import { BearerSecurity, Client, createClient } from "soap";
import { promiseFromCallback } from "./utils";

export type DFPOptions = {
networkCode: string;
apiVersion: string;
networkCode: string;
apiVersion: string;
token?: string;
};

export interface DFPClient extends Client {
setToken(token: string): void;
setToken(token: string): void;
}

export class DFP {
private readonly options: DFPOptions;

private readonly options: DFPOptions;
constructor(options: DFPOptions) {
this.options = options;
}

constructor(options: DFPOptions) {
this.options = options;
}

public async getService(service: string, token?: string): Promise<DFPClient> {
const {apiVersion} = this.options;
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${apiVersion}/${service}?wsdl`;
const client = await promiseFromCallback((cb) => createClient(serviceUrl, cb));
public async getService(service: string, token?: string): Promise<DFPClient> {
const { apiVersion, token: otoken = token } = this.options;
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${apiVersion}/${service}?wsdl`;
const client = await promiseFromCallback((cb) =>
createClient(serviceUrl, cb)
);

client.addSoapHeader(this.getSoapHeaders());
client.addSoapHeader(this.getSoapHeaders());

client.setToken = function setToken(token: string) {
client.setSecurity(new BearerSecurity(token));
};

if (token) {
client.setToken(token);
}
client.setToken = function setToken(token: string) {
client.setSecurity(new BearerSecurity(token));
};

return new Proxy(client, {
get: function get(target, propertyKey) {
const method = propertyKey.toString();
if (target.hasOwnProperty(method) && !['setToken'].includes(method)) {
return async function run(dto: any = {}) {
const res = await promiseFromCallback((cb) => client[method](dto, cb));
return DFP.parse(res);
};
} else {
return target[method];
}
}
}) as DFPClient;
if (otoken) {
client.setToken(otoken);
}

public static parse(res: any) {
return res.rval;
}
return new Proxy(client, {
get: function get(target, propertyKey) {
const method = propertyKey.toString();
if (target.hasOwnProperty(method) && !["setToken"].includes(method)) {
return async function run(dto: any = {}) {
const res = await promiseFromCallback((cb) =>
client[method](dto, cb)
);
return DFP.parse(res);
};
} else {
return target[method];
}
},
}) as DFPClient;
}

private getSoapHeaders() {
const {apiVersion, networkCode} = this.options;
public static parse(res: any) {
return res.rval;
}

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/" + apiVersion,
'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
'xmlns:soapenv': "http://schemas.xmlsoap.org/soap/envelope/"
},
'ns1:networkCode': networkCode,
'ns1:applicationName': 'content-api'
}
};
}
private getSoapHeaders() {
const { apiVersion, networkCode } = this.options;

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/" + apiVersion,
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
},
"ns1:networkCode": networkCode,
"ns1:applicationName": "content-api",
},
};
}
}