Ant_Dempster | 2017-08-29 04:17:27 UTC | #1
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
tim.smith | 2017-08-29 13:07:19 UTC | #2
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?
Ant_Dempster | 2017-08-29 21:21:01 UTC | #3
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.
tim.smith | 2017-08-29 21:36:09 UTC | #4
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.
Ant_Dempster | 2017-09-04 22:22:13 UTC | #5
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.httpproxy; };
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."
system | 2017-10-06 16:08:38 UTC | #6
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
tim.smith | 2017-11-27 18:06:11 UTC | #7
tim.smith | 2017-11-27 18:10:50 UTC | #8
@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.
tim.smith | 2018-09-25 21:06:18 UTC | #10
This post was migrated from the old Developer Forum.
ref: 1727