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

Enable Proxy Support #92

Merged
merged 3 commits into from
Feb 1, 2018
Merged
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Optional callback function where the first parameter is an error and the second
* [API](#api)
* [Promises](#promises)
* [Callbacks](#callbacks)
* [Proxy Server](#proxy-server)
* [Examples](#examples)
* [API Methods](#api-methods)
* [autoComplete](#autocomplete)
Expand Down Expand Up @@ -98,6 +99,27 @@ googleTrends.interestOverTime({keyword: 'Women\'s march'}, function(err, results
})
```

### Proxy Server
A proxy server can be used by specifying an http agent as part of the query.

```js
const HttpsProxyAgent = require('https-proxy-agent');

let proxyAgent = new HttpsProxyAgent('http://proxy-host:8888/');

let query = {
keyword: 'Women\'s march',
agent: proxyAgent
};

googleTrends.interestOverTime(query)
.then(function(results){
console.log('These proxied results are incredible', results);
})
.catch(function(err){
console.error('Oh no there was an error, double check your proxy settings', err);
});
```

### Multiple Keywords
Compare multiple keywords with any of the api methods by supplying an `array` instead of a single `string`
Expand Down
4 changes: 3 additions & 1 deletion src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import https from 'https';
import querystring from 'querystring';

export default function request({method, host, path, qs}) {
export default function request({method, host, path, qs, agent}) {
const options = {
host,
method,
path: `${path}?${querystring.stringify(qs)}`,
};

if (agent) options.agent = agent;

return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let chunk = '';
Expand Down
2 changes: 2 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export function getResults(request) {
},
};

if (obj.agent) options.agent = obj.agent;

const { path, resolution, _id } = map[searchType];

return request(options)
Expand Down