feat(Brandfetch Node): Update to use new API (#10877)

This commit is contained in:
Jon
2024-09-23 21:19:16 +01:00
committed by GitHub
parent d74cff2030
commit 08ba9a36a4
4 changed files with 130 additions and 69 deletions

View File

@@ -2,10 +2,11 @@ import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
IRequestOptions,
ILoadOptionsFunctions,
INodeExecutionData,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
@@ -13,22 +14,17 @@ export async function brandfetchApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
try {
const credentials = await this.getCredentials('brandfetchApi');
let options: IRequestOptions = {
headers: {
'x-api-key': credentials.apiKey,
},
method,
method: method as IHttpRequestMethods,
qs,
body,
uri: uri || `https://api.brandfetch.io/v1${resource}`,
url: uri || `https://api.brandfetch.io/v2${resource}`,
json: true,
};
@@ -45,7 +41,11 @@ export async function brandfetchApiRequest(
delete options.qs;
}
const response = await this.helpers.request(options);
const response = await this.helpers.requestWithAuthentication.call(
this,
'brandfetchApi',
options,
);
if (response.statusCode && response.statusCode !== 200) {
throw new NodeApiError(this.getNode(), response as JsonObject);
@@ -56,3 +56,22 @@ export async function brandfetchApiRequest(
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
export async function fetchAndPrepareBinaryData(
this: IExecuteFunctions,
imageType: string,
imageFormat: string,
logoFormats: IDataObject,
domain: string,
newItem: INodeExecutionData,
) {
const data = await brandfetchApiRequest.call(this, 'GET', '', {}, {}, logoFormats.src as string, {
json: false,
encoding: null,
});
newItem.binary![`${imageType}_${imageFormat}`] = await this.helpers.prepareBinaryData(
Buffer.from(data),
`${imageType}_${domain}.${imageFormat}`,
);
}