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

Using the soap async instead of the promise implementation #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
108 changes: 53 additions & 55 deletions src/dfp.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,72 @@
import { BearerSecurity, Client, createClient } from 'soap';
import { promiseFromCallback } from "./utils";
import { BearerSecurity, Client, createClientAsync } from "soap";

export type DFPOptions = {
networkCode: string;
apiVersion: string;
networkCode: string;
apiVersion: 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 } = this.options;
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${apiVersion}/${service}?wsdl`;
const client = await createClientAsync(serviceUrl);

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 (token) {
client.setToken(token);
}

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 [result, ..._] = await client[`${method}Async`](dto);
return DFP.parse(result);
};
} 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",
},
};
}
}
12 changes: 0 additions & 12 deletions src/utils.ts

This file was deleted.