2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-10-03 02:50:57 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2020-10-03 02:50:57 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-10-03 02:50:57 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function coinGeckoApiRequest(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-10-03 02:50:57 -07:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Accept: 'application/json',
|
2020-10-03 02:50:57 -07:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.coingecko.com/api/v3${endpoint}`,
|
2020-10-03 02:50:57 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-10-03 02:50:57 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.request.call(this, options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-10-03 02:50:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function coinGeckoRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-10-03 02:50:57 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let respData;
|
|
|
|
query.per_page = 250;
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await coinGeckoApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
query.page++;
|
|
|
|
respData = responseData;
|
|
|
|
if (propertyName !== '') {
|
|
|
|
respData = responseData[propertyName];
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, respData as IDataObject[]);
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (respData.length !== 0);
|
2020-10-03 02:50:57 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|