Remove "Read File From Url" node and add functionality to

"HTTP Request" node
This commit is contained in:
Jan Oberhauser
2019-09-04 13:24:44 +02:00
parent 7108320297
commit c1e753e9d1
4 changed files with 123 additions and 102 deletions

View File

@@ -27,7 +27,7 @@ export class HttpRequest implements INodeType {
version: 1,
description: 'Makes a HTTP request and returns the received data',
defaults: {
name: 'Http Request',
name: 'HTTP Request',
color: '#2200DD',
},
inputs: ['main'],
@@ -143,6 +143,10 @@ export class HttpRequest implements INodeType {
name: 'responseFormat',
type: 'options',
options: [
{
name: 'File',
value: 'file'
},
{
name: 'JSON',
value: 'json'
@@ -151,14 +155,43 @@ export class HttpRequest implements INodeType {
name: 'String',
value: 'string'
},
// {
// name: 'XML (not implemented)',
// value: 'xml'
// },
],
default: 'json',
description: 'The format in which the data gets returned from the URL.',
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
responseFormat: [
'string',
],
},
},
description: 'Name of the property to which to write the response data.',
},
{
displayName: 'Binary Property',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
responseFormat: [
'file',
],
},
},
description: 'Name of the binary property to which to<br />write the data of the read file.',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
@@ -350,12 +383,13 @@ export class HttpRequest implements INodeType {
// TODO: Should have a setting which makes clear that this parameter can not change for each item
const requestMethod = this.getNodeParameter('requestMethod', 0) as string;
const parametersAreJson = this.getNodeParameter('jsonParameters', 0) as boolean;
const responseFormat = this.getNodeParameter('responseFormat', 0) as string;
const httpBasicAuth = this.getCredentials('httpBasicAuth');
const httpDigestAuth = this.getCredentials('httpDigestAuth');
const httpHeaderAuth = this.getCredentials('httpHeaderAuth');
let url: string, responseFormat: string;
let url: string;
let requestOptions: OptionsWithUri;
let setUiParameter: IDataObject;
@@ -383,7 +417,6 @@ export class HttpRequest implements INodeType {
const returnItems: INodeExecutionData[] = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
url = this.getNodeParameter('url', itemIndex) as string;
responseFormat = this.getNodeParameter('responseFormat', itemIndex) as string;
requestOptions = {
headers: {},
@@ -432,7 +465,6 @@ export class HttpRequest implements INodeType {
}
}
}
requestOptions.json = true;
// Add credentials if any are set
if (httpBasicAuth !== undefined) {
@@ -452,18 +484,54 @@ export class HttpRequest implements INodeType {
};
}
if (responseFormat === 'file') {
requestOptions.encoding = null;
} else {
requestOptions.json = true;
}
// Now that the options are all set make the actual http request
const response = await this.helpers.request(requestOptions);
if (responseFormat === 'json') {
returnItems.push({ json: response });
// } else if (responseFormat === 'xml') {
// // TODO: Not implemented yet
if (responseFormat === 'file') {
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
const newItem: INodeExecutionData = {
json: items[itemIndex].json,
binary: {},
};
if (items[itemIndex].binary !== undefined) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, items[itemIndex].binary);
}
items[itemIndex] = newItem;
const fileName = (url).split('/').pop();
items[itemIndex].binary![dataPropertyName] = await this.helpers.prepareBinaryData(response, fileName);
} else if (responseFormat === 'json') {
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
returnItems.push({
json: {
[dataPropertyName]: response,
}
});
} else {
returnItems.push({ json: { response } });
}
}
return this.prepareOutputData(returnItems);
if (responseFormat === 'file') {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
// For all other ones does the output items get replaced
return [this.helpers.returnJsonArray(returnItems)];
}
}
}

View File

@@ -1,88 +0,0 @@
import { IExecuteSingleFunctions } from 'n8n-core';
import {
INodeTypeDescription,
INodeExecutionData,
INodeType,
} from 'n8n-workflow';
import { UriOptions } from 'request';
export class ReadFileFromUrl implements INodeType {
description: INodeTypeDescription = {
displayName: 'Read File From Url',
name: 'readFileFromUrl',
icon: 'fa:file-download',
group: ['input'],
version: 1,
description: 'Reads a file from an URL',
defaults: {
name: 'Read File URL',
color: '#119955',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
required: true,
placeholder: 'http://example.com/index.html',
description: 'URL of the file to read.',
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
description: 'Name of the binary property to which to<br />write the data of the URL.',
},
{
displayName: 'Ignore SSL Issues',
name: 'allowUnauthorizedCerts',
type: 'boolean',
default: false,
description: 'Still download the file even if SSL certificate validation is not possible.',
},
]
};
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
const url = this.getNodeParameter('url') as string;
const dataPropertyName = this.getNodeParameter('dataPropertyName') as string;
const options: UriOptions = {
uri: url,
// @ts-ignore
encoding: null,
rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', false) as boolean,
};
// TODO: Should have a catch here
const data = await this.helpers.request(options);
// Get the filename and also add it to the binary data
const fileName = (url).split('/').pop();
// Get the current item and add the binary data
const item = this.getInputData();
const newItem: INodeExecutionData = {
json: item.json,
binary: {},
};
if (item.binary !== undefined) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, item.binary);
}
newItem.binary![dataPropertyName as string] = await this.helpers.prepareBinaryData(data, fileName);
return newItem;
}
}