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

Add http(s) proxy support #78

Open
wants to merge 3 commits 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ npm install apiai
```javascript
var apiai = require('apiai');

var app = apiai("<your client access token>");
var app = apiai("<your client access token>", {
proxy: 'http://IP:PORT', // comment this out if proxy is not needed
});

var request = app.textRequest('<Your text query>', {
sessionId: '<unique session id>'
Expand Down
9 changes: 8 additions & 1 deletion module/apiai.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var https = require('https');
var http = require('http');
var ProxyAgent = require('proxy-agent');

var ContextsRequest = require('./contexts_request').ContextsRequest;
var GetContextsRequest = require('./get_contexts_request').GetContextsRequest;
Expand All @@ -34,6 +35,7 @@ var language = 'en';
var hostname = 'api.api.ai';
var endpoint = '/v1/';
var defaultSource = 'node';
var proxy = null;

/**
* Module exports.
Expand Down Expand Up @@ -108,6 +110,7 @@ function Application (clientAccessToken, options) {
self.version = options.version || version;

self.endpoint = options.endpoint || endpoint;
self.proxy = options.proxy || proxy;
self.requestSource = options.requestSource || defaultSource;

if ('secure' in options) {
Expand All @@ -117,7 +120,11 @@ function Application (clientAccessToken, options) {
}

var _http = self.secure ? https : http;
self._agent = new _http.Agent({ keepAlive: true });
if (self.proxy) {
self._agent = new ProxyAgent(self.proxy);
} else {
self._agent = new _http.Agent({ keepAlive: true });
}
}

Application.prototype.contextsRequest = function(contexts, options) {
Expand Down
Loading