2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function nasaApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
option: IDataObject = {},
|
|
|
|
uri?: string | undefined,
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('nasaApi');
|
2020-12-10 02:30:13 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.api_key = credentials.api_key as string;
|
2020-12-10 02:30:13 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
uri: uri || `https://api.nasa.gov${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(option)) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-12-10 02:30:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function nasaApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-12-10 02:30:13 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.size = 20;
|
|
|
|
|
|
|
|
let uri: string | undefined = undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await nasaApiRequest.call(this, method, resource, query, {}, uri);
|
|
|
|
uri = responseData.links.next;
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.links.next !== undefined);
|
2020-12-10 02:30:13 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|