Skip to content

Commit

Permalink
feat(api): Output type added to http api: binary, json,txt
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Apr 3, 2019
1 parent 3fdfa27 commit 9305c7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/ha-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ class HaHttp {
}

renderTemplate(templateString) {
return this._post('template', { template: templateString });
return this._post('template', { template: templateString }, 'text');
}

_post(path, data = {}) {
_post(path, data = {}, responseType = 'json') {
debug(`POST: ${this.config.baseUrl}/${path}`);

this.client.defaults.responseType = responseType;

return this.client
.post(path, data)
.then(res => res.data || '')
Expand All @@ -126,9 +128,11 @@ class HaHttp {
});
}

_get(path, params = {}) {
_get(path, params = {}, responseType = 'json') {
debug(`GET: ${this.config.baseUrl}/${path}`);

this.client.defaults.responseType = responseType;

return this.client
.request({ url: path, params: params })
.then(res => res.data || '')
Expand Down
12 changes: 11 additions & 1 deletion nodes/api/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
path: { value: "" },
data: { value: "" },
location: { value: "payload" },
locationType: { value: "msg" }
locationType: { value: "msg" },
responseType: { value: "json" }
},
oneditprepare: function() {
const node = this;
Expand Down Expand Up @@ -116,6 +117,15 @@
<input type="text" id="node-input-location">
<input type="hidden" id="node-input-locationType">
</div>

<div class="form-row http">
<label for="node-input-responseType">Return</label>
<select id="node-input-responseType" style="width:70%;">
<option value="text">a UTF-8 string</option>
<option value="arraybuffer">a binary buffer</option>
<option value="json">a parsed JSON object</option>
</select>
</div>
</script>

<script type="text/x-red" data-help-name="ha-api">
Expand Down
16 changes: 14 additions & 2 deletions nodes/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = function(RED) {
path: {},
data: {},
location: {},
locationType: {}
locationType: {},
responseType: {}
},
input: {
protocol: {
Expand Down Expand Up @@ -69,6 +70,16 @@ module.exports = function(RED) {
.valid('msg', 'flow', 'global', 'none')
.label('locationType')
}
},
responseType: {
messageProp: 'payload.responseType',
configProp: 'responseType',
default: 'json',
validation: {
schema: Joi.string()
.valid('json', 'text', 'arraybuffer')
.label('responseType')
}
}
}
};
Expand Down Expand Up @@ -120,7 +131,8 @@ module.exports = function(RED) {
apiCall = config.server.http[`_${method}`].bind(
config.server.http,
path,
data
data,
parsedMessage.responseType.value
);
} else {
try {
Expand Down

0 comments on commit 9305c7d

Please sign in to comment.