Proxy setting for platform-client-sdk-javascript?

Hi,

Just wondering if in the new platform client sdk for javascript if there is any implementation for being able to use a proxy? Have searching the documentation but can't find anything.

(Did a hack on the older javascript SDK to get around this ... but hoping it might be built in by now. :slight_smile: )

Thanks,

Ant

There isn't currently proxy support. I tried using superagent-proxy, but adding that dependency added 1.2 MB to the package, so I left it out until I could find a better solution. What was the hack you implemented previously?

Previous hack was actually a change that someone had made to the code but then had somehow got overwritten and then never got pushed back in (can't access the github page anymore though) which I borrowed the changes from ... basically ...

in purecloudsession.js

require('superagent-proxy')(superagent);

and then modded the prototype._baseRequest section with :

if (this.options.proxy) {
request = request.proxy(this.options.proxy);
}

And then in the pieces of code themselves

var session = purecloud.PureCloudSession({
proxy: 'http://xxxxxxxxxxxxxxxxxxxx',

Even if you don't change the SDK, are you able to give some guidance on where to make the changes in the new version? Still on a learning curve with javascript and unfortunately stuck behind a corporate proxy.

I don't have the code where I was trying the proxy anymore, but I believe you can set the proxy in the callApi function here. If you're generating the SDK yourself, you would make the change to the ApiClient template.

I've opened API-2820 to take a look at allowing the client to set the proxy lib so it's not a required dependency.

Just an update on this ... one of my colleagues ( tuttinator ) has managed to get the new javascript platform client working through a proxy by making some changes, so listing here in case anymore else is in need.

We are using Node.Js for our queries.

The main changes are that in node_modules/purecloud-platform-client-v2/src/purecloud-platform-client-v2/ApiClient.js they added two convenience functions:

  /* Request mutator */
  exports.prototype.setProxy = function(wrapperFunc, proxyUrl) {
    console.log("setProxy called...")
    console.info(this);
    this._proxyRequests = true;
    this._proxyWrapperFunc = wrapperFunc;
    this._proxyUrl = proxyUrl || process.env.http_proxy;
  };

  exports.prototype.unsetProxy = function() {
this._proxyRequests = undefined;
this._proxyWrapperFunc = undefined;
this._proxyUrl = undefined;
  };

And then in the callApi and loginClientCredentialsGrant methods, they add a pattern of using wrapper function as the superagent-proxy documentation seemed to indicate:
…

exports.prototype.callApi = function callApi(path, httpMethod, pathParams,
      queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts) {

    var _this = this;
var url = this.buildUrl(path, pathParams);

    var requestHandler;
    if(this._proxyRequests == true) {
      console.log("Proxying through ", this._proxyUrl);
      requestHandler = this._proxyWrapperFunc(superagent);
    } else {
      requestHandler = superagent;
    }

    var request = requestHandler(httpMethod, url);

…


    if(this._proxyRequests == true) {
      request.proxy(this._proxyUrl)
}

…

};

We were testing using a basic authentication request ... to get that working there were some additions at the very top :

const platformClient = require('purecloud-platform-client-v2');
const superagentProxy = require('superagent-proxy');

var client = platformClient.ApiClient.instance;
//client.setReturnExtendedResponses(true);
client.setEnvironment('mypurecloud.com.au');
client.setProxy(superagentProxy, 'http://yourproxyhere.com:80');

The changes have been saved as a fork in the code here - https://github.com/tuttinator/platform-client-sdk-javascript .

He also noted a possible error in the following documentation (https://developer.mypurecloud.com/api/rest/client-libraries/javascript/index.html ... Making Requests ... Node.js) :

.catch(function(response) {
    // Handle failure response
    console.log(`${response.status} - ${response.error.message}`);
    console.log(response.error);
  });

"That catch block receives a flat object which doesn’t have a status, error, or error.message property (which was that error you saw) – at least in the case of a proxy error."

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.

@Ant_Dempster Proxy support has been added for the node.js SDK in v14.0.0. Usage:

const platformClient = require('purecloud-platform-client-v2');

var client = purecloud-platform-client-v2.ApiClient.instance;
require('superagent-proxy')(client.superagent);
// Configure settings for your proxy here
// Documentation: https://www.npmjs.com/package/proxy-agent
client.proxy = {
	host: '172.1.1.100',
	port: 443,
	protocol: 'https',
};

Note that you must include superagent-proxy as a dependency of your package as it is not included with the SDK.