Skip to content

Commit 1031393

Browse files
author
Pavel Petroshenko
authored
Merge pull request #15 from electricimp/develop
v1.2.0
2 parents f80137b + 7242ac9 commit 1031393

4 files changed

Lines changed: 601 additions & 5 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ Each of these steps are described in the following sections.
2323
npm install -g imp-central-api
2424
```
2525

26+
#### Proxy Setup ####
27+
28+
If *imp-central-api* is going to connect to the impCentral API via a proxy, one of the following environment variables should be set to a value in URL format:
29+
- `HTTPS_PROXY` (or `https_proxy`) — for the proxy which passes HTTPs requests.
30+
- `HTTP_PROXY` (or `http_proxy`) — for the proxy which passes HTTP requests.
31+
32+
Note, the default impCentral API base endpoint is working over HTTPs.
33+
2634
### Instantiation ###
2735

2836
To instantiate this library, call the [ImpCentralApi class](./lib/ImpCentralApi.js) constructor.
2937

30-
By default, *imp-central-api* works with the following impCentral API base endpoint: *api.electricimp.com/v5*. You can optionally pass an alternative impCentral API base endpoint into the constructor. This can be used to connect to Private impCloud™ installations. The class method *apiEndpoint()* can be used to obtain the current impCentral API base endpoint.
38+
By default, *imp-central-api* works with the following impCentral API base endpoint: `https://api.electricimp.com/v5`. You can optionally pass an alternative impCentral API base endpoint into the constructor. This can be used to connect to Private impCloud™ installations. The class method *apiEndpoint()* can be used to obtain the current impCentral API base endpoint.
3139

3240
After instantiation, use [ImpCentralApi class](./lib/ImpCentralApi.js) methods to obtain the subclasses which provide the methods you will use to access specific impCentral API resources. For example:
3341

lib/LogStreams.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
const EventSource = require('eventsource');
2828
const Util = require('util');
29+
const Url = require('url');
2930
const ParamsChecker = require('./util/ParamsChecker');
3031
const HttpHelper = require('./util/HttpHelper');
3132
const Errors = require('./Errors');
@@ -97,7 +98,9 @@ class LogStreams {
9798
return new Promise((resolve, reject) => {
9899
const logStreamId = result.data.id;
99100
const formatQuery = '?format=' + format;
100-
const eventSource = new EventSource(HttpHelper.apiEndpoint + this._getPath(logStreamId) + formatQuery);
101+
const eventSource = new EventSource(
102+
HttpHelper.apiEndpoint + this._getPath(logStreamId) + formatQuery,
103+
this._getEventSourceOptions());
101104

102105
eventSource.onopen = function() {
103106
this._logStreams[logStreamId] = eventSource;
@@ -110,7 +113,7 @@ class LogStreams {
110113

111114
eventSource.onerror = function(error) {
112115
if (error.status) {
113-
const err = new Errors.ImpCentralApiError(null, error.status);
116+
const err = new Errors.ImpCentralApiError('impCentral API error HTTP/' + error.status, error.status);
114117
if (errorHandler) {
115118
errorHandler(err);
116119
}
@@ -191,6 +194,20 @@ class LogStreams {
191194
}
192195
}
193196

197+
_getEventSourceOptions() {
198+
let endpoint = Url.parse(HttpHelper.apiEndpoint);
199+
let proxy = null;
200+
if (endpoint.protocol === 'https:') {
201+
proxy = process.env.HTTPS_PROXY || process.env.https_proxy || null;
202+
}
203+
else if (endpoint.protocol === 'http:') {
204+
proxy = process.env.HTTP_PROXY || process.env.http_proxy || null;
205+
}
206+
return {
207+
proxy : proxy
208+
};
209+
}
210+
194211
_getPath(logStreamId = null) {
195212
if (logStreamId) {
196213
return '/logstream/' + logStreamId;

0 commit comments

Comments
 (0)