From 6cf0eda3fd2c3943daa74820f4d3e9a745be3f6f Mon Sep 17 00:00:00 2001 From: Michael Newman Date: Thu, 26 Aug 2021 22:10:29 -0400 Subject: [PATCH] Using the soap async instead of the promise implementation, reducing code maintenance --- src/dfp.ts | 108 +++++++++++++++++++++++++-------------------------- src/utils.ts | 12 ------ 2 files changed, 53 insertions(+), 67 deletions(-) delete mode 100644 src/utils.ts diff --git a/src/dfp.ts b/src/dfp.ts index 40d2104..0bf813d 100644 --- a/src/dfp.ts +++ b/src/dfp.ts @@ -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 { - 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 { + 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", + }, + }; + } } diff --git a/src/utils.ts b/src/utils.ts deleted file mode 100644 index b63499c..0000000 --- a/src/utils.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function promiseFromCallback (fn: (callback: (err: Error, result: any) => void) => void): Promise { - return new Promise((resolve, reject) => { - fn((err, result) => { - if (err) { - reject(err); - return; - } - - resolve(result); - }); - }); -} \ No newline at end of file